2021-08-09 20:06:21 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { join } from 'path';
|
|
|
|
import { Worker } from 'worker_threads';
|
2022-03-29 17:06:42 +00:00
|
|
|
import { app } from 'electron';
|
2021-08-09 20:06:21 +00:00
|
|
|
|
|
|
|
export type WrappedWorkerRequest = {
|
|
|
|
readonly uuid: string;
|
2021-09-24 00:49:05 +00:00
|
|
|
readonly data: Uint8Array;
|
2021-08-09 20:06:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type WrappedWorkerResponse = {
|
|
|
|
readonly uuid: string;
|
|
|
|
readonly error: string | undefined;
|
|
|
|
readonly response?: File;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getHeicConverter(): (
|
|
|
|
uuid: string,
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Uint8Array
|
2021-08-09 20:06:21 +00:00
|
|
|
) => Promise<WrappedWorkerResponse> {
|
2022-03-29 17:06:42 +00:00
|
|
|
const scriptDir = join(
|
|
|
|
app.getAppPath(),
|
|
|
|
'ts',
|
|
|
|
'workers',
|
|
|
|
'heicConverterWorker.js'
|
2021-08-09 20:06:21 +00:00
|
|
|
);
|
2022-03-29 17:06:42 +00:00
|
|
|
const worker = new Worker(scriptDir);
|
2021-08-09 20:06:21 +00:00
|
|
|
|
|
|
|
const ResponseMap = new Map<
|
|
|
|
string,
|
|
|
|
(response: WrappedWorkerResponse) => void
|
|
|
|
>();
|
|
|
|
|
|
|
|
worker.on('message', (wrappedResponse: WrappedWorkerResponse) => {
|
|
|
|
const { uuid } = wrappedResponse;
|
|
|
|
|
|
|
|
const resolve = ResponseMap.get(uuid);
|
|
|
|
if (!resolve) {
|
|
|
|
throw new Error(`Cannot find resolver for ${uuid}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(wrappedResponse);
|
|
|
|
});
|
|
|
|
|
|
|
|
return async (uuid, data) => {
|
|
|
|
const wrappedRequest: WrappedWorkerRequest = {
|
|
|
|
uuid,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = new Promise<WrappedWorkerResponse>(resolve => {
|
|
|
|
ResponseMap.set(uuid, resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
worker.postMessage(wrappedRequest);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|