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
import { IWorkerDefinition } from 'worker-factory';
import { IBrokerDefinition, IDefaultBrokerDefinition } from '../interfaces';
import { TBrokerImplementation } from '../types';
import { PORT_MAP } from './port-map';
 
export const extendBrokerImplementation = <T extends IBrokerDefinition, U extends IWorkerDefinition>(
    partialBrokerImplementation: TBrokerImplementation<T, U>
): TBrokerImplementation<T & IDefaultBrokerDefinition, U> =>
    <TBrokerImplementation<T & IDefaultBrokerDefinition, U>>{
        ...partialBrokerImplementation,
        connect: ({ call }) => {
            return async (): Promise<MessagePort> => {
                const { port1, port2 } = new MessageChannel();
 
                const portId = <number>await call('connect', { port: port1 }, [port1]);
 
                PORT_MAP.set(port2, portId);
 
                return port2;
            };
        },
        disconnect: ({ call }) => {
            return async (port: MessagePort): Promise<void> => {
                const portId = PORT_MAP.get(port);
 
                if (portId === undefined) {
                    throw new Error('The given port is not connected.');
                }
 
                await call('disconnect', { portId });
            };
        },
        isSupported: ({ call }) => {
            return () => call('isSupported');
        }
    };