chenyc
2025-12-09 65e034683b28d799e73c7d7e5e4769fab5b9bc9c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { generateUniqueNumber } from 'fast-unique-numbers';
import { IDefaultWorkerDefinition, IReceiver, IWorkerDefinition } from '../interfaces';
import { TDestroyWorkerFunction, TWorkerImplementation } from '../types';
import { renderUnknownPortIdError } from './error-renderers';
import { isSupportingTransferables } from './is-supporting-transferables';
 
const DESTROY_WORKER_FUNCTIONS: Map<number, TDestroyWorkerFunction> = new Map();
 
export const extendWorkerImplementation = <WorkerDefinition extends IWorkerDefinition>(
    createWorker: (receiver: IReceiver, workerImplementation: TWorkerImplementation<WorkerDefinition>) => TDestroyWorkerFunction,
    partialWorkerImplementation: TWorkerImplementation<WorkerDefinition>,
    isSupportedFunction: () => boolean | Promise<boolean>
): TWorkerImplementation<WorkerDefinition> & TWorkerImplementation<IDefaultWorkerDefinition> => ({
    ...partialWorkerImplementation,
    connect: ({ port }: { port: MessagePort }) => {
        port.start();
 
        const destroyWorker = createWorker(port, partialWorkerImplementation);
        const portId = generateUniqueNumber(DESTROY_WORKER_FUNCTIONS);
 
        DESTROY_WORKER_FUNCTIONS.set(portId, () => {
            destroyWorker();
            port.close();
            DESTROY_WORKER_FUNCTIONS.delete(portId);
        });
 
        return { result: portId };
    },
    disconnect: ({ portId }: { portId: number }) => {
        const destroyWorker = DESTROY_WORKER_FUNCTIONS.get(portId);
 
        if (destroyWorker === undefined) {
            throw renderUnknownPortIdError(portId);
        }
 
        destroyWorker();
 
        return { result: null };
    },
    isSupported: async () => {
        const isSelfSupported = await isSupportingTransferables();
 
        if (isSelfSupported) {
            const result = isSupportedFunction();
            const synchronousResult = result instanceof Promise ? await result : result;
 
            return { result: synchronousResult };
        }
 
        return { result: false };
    }
});