broker-factory 3.1.10 → 3.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/es2019/factories/create-broker.d.ts +9 -0
- package/build/es2019/factories/create-broker.d.ts.map +1 -0
- package/build/es2019/factories/create-broker.js +43 -0
- package/build/es2019/factories/create-broker.js.map +1 -0
- package/build/es2019/factories/create-or-get-ongoing-requests.d.ts +8 -0
- package/build/es2019/factories/create-or-get-ongoing-requests.d.ts.map +1 -0
- package/build/es2019/factories/create-or-get-ongoing-requests.js +10 -0
- package/build/es2019/factories/create-or-get-ongoing-requests.js.map +1 -0
- package/build/es2019/factories/extend-broker-implementation.d.ts +5 -0
- package/build/es2019/factories/extend-broker-implementation.d.ts.map +1 -0
- package/build/es2019/{helpers → factories}/extend-broker-implementation.js +3 -4
- package/build/es2019/factories/extend-broker-implementation.js.map +1 -0
- package/build/es2019/module.d.ts +1 -4
- package/build/es2019/module.d.ts.map +1 -1
- package/build/es2019/module.js +4 -53
- package/build/es2019/module.js.map +1 -1
- package/build/es5/bundle.js +146 -140
- package/package.json +14 -26
- package/src/factories/create-broker.ts +69 -0
- package/src/factories/create-or-get-ongoing-requests.ts +14 -0
- package/src/factories/extend-broker-implementation.ts +37 -0
- package/src/module.ts +9 -73
- package/build/es2019/helpers/extend-broker-implementation.d.ts +0 -5
- package/build/es2019/helpers/extend-broker-implementation.d.ts.map +0 -1
- package/build/es2019/helpers/extend-broker-implementation.js.map +0 -1
- package/build/es2019/helpers/port-map.d.ts +0 -2
- package/build/es2019/helpers/port-map.d.ts.map +0 -1
- package/build/es2019/helpers/port-map.js +0 -2
- package/build/es2019/helpers/port-map.js.map +0 -1
- package/src/helpers/extend-broker-implementation.ts +0 -36
- package/src/helpers/port-map.ts +0 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { generateUniqueNumber as generateUniqueNumberFunction } from 'fast-unique-numbers';
|
|
2
|
+
import { IWorkerDefinition } from 'worker-factory';
|
|
3
|
+
import type { isMessagePort as isMessagePortFunction } from '../guards/message-port';
|
|
4
|
+
import { IBrokerDefinition, IDefaultBrokerDefinition } from '../interfaces';
|
|
5
|
+
import { TBrokerImplementation } from '../types';
|
|
6
|
+
import type { createCreateOrGetOngoingRequests } from './create-or-get-ongoing-requests';
|
|
7
|
+
import type { createExtendBrokerImplementation } from './extend-broker-implementation';
|
|
8
|
+
export declare const createBrokerFactory: (createOrGetOngoingRequests: ReturnType<typeof createCreateOrGetOngoingRequests>, extendBrokerImplementation: ReturnType<typeof createExtendBrokerImplementation>, generateUniqueNumber: typeof generateUniqueNumberFunction, isMessagePort: typeof isMessagePortFunction) => <T extends IBrokerDefinition, U extends IWorkerDefinition>(brokerImplementation: TBrokerImplementation<T, U>) => ((sender: MessagePort | Worker) => T & IDefaultBrokerDefinition);
|
|
9
|
+
//# sourceMappingURL=create-broker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-broker.d.ts","sourceRoot":"","sources":["../../../src/factories/create-broker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,IAAI,4BAA4B,EAAE,MAAM,qBAAqB,CAAC;AAChG,OAAO,EAAE,iBAAiB,EAA6C,MAAM,gBAAgB,CAAC;AAC9F,OAAO,KAAK,EAAE,aAAa,IAAI,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAgB,MAAM,eAAe,CAAC;AAC1F,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,KAAK,EAAE,gCAAgC,EAAE,MAAM,kCAAkC,CAAC;AACzF,OAAO,KAAK,EAAE,gCAAgC,EAAE,MAAM,gCAAgC,CAAC;AAEvF,eAAO,MAAM,mBAAmB,GAExB,4BAA4B,UAAU,CAAC,OAAO,gCAAgC,CAAC,EAC/E,4BAA4B,UAAU,CAAC,OAAO,gCAAgC,CAAC,EAC/E,sBAAsB,OAAO,4BAA4B,EACzD,eAAe,OAAO,qBAAqB,MAE9C,CAAC,SAAS,iBAAiB,EAAE,CAAC,SAAS,iBAAiB,EACrD,sBAAsB,qBAAqB,CAAC,CAAC,EAAE,CAAC,CAAC,KAClD,CAAC,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,KAAK,CAAC,GAAG,wBAAwB,CAmDjE,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const createBrokerFactory = (createOrGetOngoingRequests, extendBrokerImplementation, generateUniqueNumber, isMessagePort) => (brokerImplementation) => {
|
|
2
|
+
const fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
|
|
3
|
+
return (sender) => {
|
|
4
|
+
const ongoingRequests = createOrGetOngoingRequests(sender);
|
|
5
|
+
sender.addEventListener('message', (({ data: message }) => {
|
|
6
|
+
const { id } = message;
|
|
7
|
+
if (id !== null && ongoingRequests.has(id)) {
|
|
8
|
+
const { reject, resolve } = ongoingRequests.get(id);
|
|
9
|
+
ongoingRequests.delete(id);
|
|
10
|
+
if (message.error === undefined) {
|
|
11
|
+
resolve(message.result);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
reject(new Error(message.error.message));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}));
|
|
18
|
+
if (isMessagePort(sender)) {
|
|
19
|
+
sender.start();
|
|
20
|
+
}
|
|
21
|
+
const call = (method, params = null, transferables = []) => {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
const id = generateUniqueNumber(ongoingRequests);
|
|
24
|
+
ongoingRequests.set(id, { reject, resolve });
|
|
25
|
+
if (params === null) {
|
|
26
|
+
sender.postMessage({ id, method }, transferables);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
sender.postMessage({ id, method, params }, transferables);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
const notify = (method, params, transferables = []) => {
|
|
34
|
+
sender.postMessage({ id: null, method, params }, transferables);
|
|
35
|
+
};
|
|
36
|
+
let functions = {};
|
|
37
|
+
for (const [key, handler] of Object.entries(fullBrokerImplementation)) {
|
|
38
|
+
functions = { ...functions, [key]: handler({ call, notify }) };
|
|
39
|
+
}
|
|
40
|
+
return { ...functions };
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=create-broker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-broker.js","sourceRoot":"","sources":["../../../src/factories/create-broker.ts"],"names":[],"mappings":"AAQA,MAAM,CAAC,MAAM,mBAAmB,GAC5B,CACI,0BAA+E,EAC/E,0BAA+E,EAC/E,oBAAyD,EACzD,aAA2C,EAC7C,EAAE,CACJ,CACI,oBAAiD,EACe,EAAE;IAClE,MAAM,wBAAwB,GAAG,0BAA0B,CAAC,oBAAoB,CAAC,CAAC;IAElF,OAAO,CAAC,MAA4B,EAAE,EAAE;QACpC,MAAM,eAAe,GAAG,0BAA0B,CAAC,MAAM,CAAC,CAAC;QAE3D,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAiB,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAgB,EAAE,EAAE;YACnF,MAAM,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC;YAEvB,IAAI,EAAE,KAAK,IAAI,IAAI,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;gBACzC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAA4C,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;gBAE7F,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBAE3B,IAA0B,OAAQ,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;oBACrD,OAAO,CAAwB,OAAQ,CAAC,MAAM,CAAC,CAAC;gBACpD,CAAC;qBAAM,CAAC;oBACJ,MAAM,CAAC,IAAI,KAAK,CAAuB,OAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;gBACpE,CAAC;YACL,CAAC;QACL,CAAC,CAAC,CAAC,CAAC;QAEJ,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;YACxB,MAAM,CAAC,KAAK,EAAE,CAAC;QACnB,CAAC;QAED,MAAM,IAAI,GAAG,CAAoB,MAAS,EAAE,SAAyB,IAAI,EAAE,gBAAuC,EAAE,EAAE,EAAE;YACpH,OAAO,IAAI,OAAO,CAA6B,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBAC/D,MAAM,EAAE,GAAG,oBAAoB,CAAC,eAAe,CAAC,CAAC;gBAEjD,eAAe,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;gBAE7C,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;oBAClB,MAAM,CAAC,WAAW,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,EAAkB,aAAa,CAAC,CAAC;gBACtE,CAAC;qBAAM,CAAC;oBACJ,MAAM,CAAC,WAAW,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,EAAkB,aAAa,CAAC,CAAC;gBAC9E,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC;QACF,MAAM,MAAM,GAAG,CAAoB,MAAS,EAAE,MAAsB,EAAE,gBAAuC,EAAE,EAAE,EAAE;YAC/G,MAAM,CAAC,WAAW,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,EAAkB,aAAa,CAAC,CAAC;QACpF,CAAC,CAAC;QAEF,IAAI,SAAS,GAAW,EAAE,CAAC;QAE3B,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,wBAAwB,CAAC,EAAE,CAAC;YACpE,SAAS,GAAG,EAAE,GAAG,SAAS,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC;QACnE,CAAC;QAED,OAAqC,EAAE,GAAG,SAAS,EAAE,CAAC;IAC1D,CAAC,CAAC;AACN,CAAC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const createCreateOrGetOngoingRequests: (ongoingRequestsMap: WeakMap<MessagePort | Worker, Map<number, {
|
|
2
|
+
reject: Function;
|
|
3
|
+
resolve: Function;
|
|
4
|
+
}>>) => (sender: MessagePort | Worker) => Map<number, {
|
|
5
|
+
reject: Function;
|
|
6
|
+
resolve: Function;
|
|
7
|
+
}>;
|
|
8
|
+
//# sourceMappingURL=create-or-get-ongoing-requests.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-or-get-ongoing-requests.d.ts","sourceRoot":"","sources":["../../../src/factories/create-or-get-ongoing-requests.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gCAAgC,GACxC,oBAAoB,OAAO,CAAC,WAAW,GAAG,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE;IAAE,MAAM,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,QAAQ,CAAA;CAAE,CAAC,CAAC,MACvG,QAAQ,WAAW,GAAG,MAAM,KAAG,GAAG,CAAC,MAAM,EAAE;IAAE,MAAM,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,QAAQ,CAAA;CAAE,CAWlF,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const createCreateOrGetOngoingRequests = (ongoingRequestsMap) => (sender) => {
|
|
2
|
+
if (ongoingRequestsMap.has(sender)) {
|
|
3
|
+
// @todo TypeScript needs to be convinced that has() works as expected.
|
|
4
|
+
return ongoingRequestsMap.get(sender);
|
|
5
|
+
}
|
|
6
|
+
const ongoingRequests = new Map();
|
|
7
|
+
ongoingRequestsMap.set(sender, ongoingRequests);
|
|
8
|
+
return ongoingRequests;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=create-or-get-ongoing-requests.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-or-get-ongoing-requests.js","sourceRoot":"","sources":["../../../src/factories/create-or-get-ongoing-requests.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gCAAgC,GACzC,CAAC,kBAAuG,EAAE,EAAE,CAC5G,CAAC,MAA4B,EAAwD,EAAE;IACnF,IAAI,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QACjC,uEAAuE;QACvE,OAA6D,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAChG,CAAC;IAED,MAAM,eAAe,GAAyD,IAAI,GAAG,EAAE,CAAC;IAExF,kBAAkB,CAAC,GAAG,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAEhD,OAAO,eAAe,CAAC;AAC3B,CAAC,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { IWorkerDefinition } from 'worker-factory';
|
|
2
|
+
import { IBrokerDefinition, IDefaultBrokerDefinition } from '../interfaces';
|
|
3
|
+
import { TBrokerImplementation } from '../types';
|
|
4
|
+
export declare const createExtendBrokerImplementation: (portMap: WeakMap<MessagePort, number>) => <T extends IBrokerDefinition, U extends IWorkerDefinition>(partialBrokerImplementation: TBrokerImplementation<T, U>) => TBrokerImplementation<T & IDefaultBrokerDefinition, U>;
|
|
5
|
+
//# sourceMappingURL=extend-broker-implementation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extend-broker-implementation.d.ts","sourceRoot":"","sources":["../../../src/factories/extend-broker-implementation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAEjD,eAAO,MAAM,gCAAgC,GACxC,SAAS,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,MACrC,CAAC,SAAS,iBAAiB,EAAE,CAAC,SAAS,iBAAiB,EACrD,6BAA6B,qBAAqB,CAAC,CAAC,EAAE,CAAC,CAAC,KACzD,qBAAqB,CAAC,CAAC,GAAG,wBAAwB,EAAE,CAAC,CA4BnD,CAAC"}
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
export const extendBrokerImplementation = (partialBrokerImplementation) => ({
|
|
1
|
+
export const createExtendBrokerImplementation = (portMap) => (partialBrokerImplementation) => ({
|
|
3
2
|
...partialBrokerImplementation,
|
|
4
3
|
connect: ({ call }) => {
|
|
5
4
|
return async () => {
|
|
6
5
|
const { port1, port2 } = new MessageChannel();
|
|
7
6
|
const portId = await call('connect', { port: port1 }, [port1]);
|
|
8
|
-
|
|
7
|
+
portMap.set(port2, portId);
|
|
9
8
|
return port2;
|
|
10
9
|
};
|
|
11
10
|
},
|
|
12
11
|
disconnect: ({ call }) => {
|
|
13
12
|
return async (port) => {
|
|
14
|
-
const portId =
|
|
13
|
+
const portId = portMap.get(port);
|
|
15
14
|
if (portId === undefined) {
|
|
16
15
|
throw new Error('The given port is not connected.');
|
|
17
16
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extend-broker-implementation.js","sourceRoot":"","sources":["../../../src/factories/extend-broker-implementation.ts"],"names":[],"mappings":"AAIA,MAAM,CAAC,MAAM,gCAAgC,GACzC,CAAC,OAAqC,EAAE,EAAE,CAC1C,CACI,2BAAwD,EACF,EAAE,CACxD,CAAwD;IACpD,GAAG,2BAA2B;IAC9B,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE;QAClB,OAAO,KAAK,IAA0B,EAAE;YACpC,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,IAAI,cAAc,EAAE,CAAC;YAE9C,MAAM,MAAM,GAAW,MAAM,IAAI,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;YAEvE,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAE3B,OAAO,KAAK,CAAC;QACjB,CAAC,CAAC;IACN,CAAC;IACD,UAAU,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE;QACrB,OAAO,KAAK,EAAE,IAAiB,EAAiB,EAAE;YAC9C,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAEjC,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACvB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;YACxD,CAAC;YAED,MAAM,IAAI,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QACzC,CAAC,CAAC;IACN,CAAC;IACD,WAAW,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE;QACtB,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IACrC,CAAC;CACJ,CAAA,CAAC"}
|
package/build/es2019/module.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import { IWorkerDefinition } from 'worker-factory';
|
|
2
|
-
import { IBrokerDefinition, IDefaultBrokerDefinition } from './interfaces';
|
|
3
|
-
import { TBrokerImplementation } from './types';
|
|
4
1
|
export * from './interfaces/index';
|
|
5
2
|
export * from './types/index';
|
|
6
|
-
export declare const createBroker: <T extends IBrokerDefinition, U extends IWorkerDefinition>(brokerImplementation: TBrokerImplementation<T, U>) => ((sender: MessagePort | Worker) => T & IDefaultBrokerDefinition);
|
|
3
|
+
export declare const createBroker: <T extends import("./interfaces/broker-definition").IBrokerDefinition, U extends import("worker-factory").IWorkerDefinition>(brokerImplementation: import("./types/broker-implementation").TBrokerImplementation<T, U>) => ((sender: MessagePort | Worker) => T & import("./interfaces/default-broker-definition").IDefaultBrokerDefinition);
|
|
7
4
|
//# sourceMappingURL=module.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"AAUA,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAE9B,eAAO,MAAM,YAAY,8UAKxB,CAAC"}
|
package/build/es2019/module.js
CHANGED
|
@@ -1,62 +1,13 @@
|
|
|
1
1
|
import { generateUniqueNumber } from 'fast-unique-numbers';
|
|
2
|
+
import { createBrokerFactory } from './factories/create-broker';
|
|
3
|
+
import { createCreateOrGetOngoingRequests } from './factories/create-or-get-ongoing-requests';
|
|
4
|
+
import { createExtendBrokerImplementation } from './factories/extend-broker-implementation';
|
|
2
5
|
import { isMessagePort } from './guards/message-port';
|
|
3
|
-
import { extendBrokerImplementation } from './helpers/extend-broker-implementation';
|
|
4
6
|
/*
|
|
5
7
|
* @todo Explicitly referencing the barrel file seems to be necessary when enabling the
|
|
6
8
|
* isolatedModules compiler option.
|
|
7
9
|
*/
|
|
8
10
|
export * from './interfaces/index';
|
|
9
11
|
export * from './types/index';
|
|
10
|
-
const
|
|
11
|
-
const createOrGetOngoingRequests = (sender) => {
|
|
12
|
-
if (ONGOING_REQUESTS.has(sender)) {
|
|
13
|
-
// @todo TypeScript needs to be convinced that has() works as expected.
|
|
14
|
-
return ONGOING_REQUESTS.get(sender);
|
|
15
|
-
}
|
|
16
|
-
const ongoingRequests = new Map();
|
|
17
|
-
ONGOING_REQUESTS.set(sender, ongoingRequests);
|
|
18
|
-
return ongoingRequests;
|
|
19
|
-
};
|
|
20
|
-
export const createBroker = (brokerImplementation) => {
|
|
21
|
-
const fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
|
|
22
|
-
return (sender) => {
|
|
23
|
-
const ongoingRequests = createOrGetOngoingRequests(sender);
|
|
24
|
-
sender.addEventListener('message', (({ data: message }) => {
|
|
25
|
-
const { id } = message;
|
|
26
|
-
if (id !== null && ongoingRequests.has(id)) {
|
|
27
|
-
const { reject, resolve } = ongoingRequests.get(id);
|
|
28
|
-
ongoingRequests.delete(id);
|
|
29
|
-
if (message.error === undefined) {
|
|
30
|
-
resolve(message.result);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
reject(new Error(message.error.message));
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}));
|
|
37
|
-
if (isMessagePort(sender)) {
|
|
38
|
-
sender.start();
|
|
39
|
-
}
|
|
40
|
-
const call = (method, params = null, transferables = []) => {
|
|
41
|
-
return new Promise((resolve, reject) => {
|
|
42
|
-
const id = generateUniqueNumber(ongoingRequests);
|
|
43
|
-
ongoingRequests.set(id, { reject, resolve });
|
|
44
|
-
if (params === null) {
|
|
45
|
-
sender.postMessage({ id, method }, transferables);
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
sender.postMessage({ id, method, params }, transferables);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
const notify = (method, params, transferables = []) => {
|
|
53
|
-
sender.postMessage({ id: null, method, params }, transferables);
|
|
54
|
-
};
|
|
55
|
-
let functions = {};
|
|
56
|
-
for (const [key, handler] of Object.entries(fullBrokerImplementation)) {
|
|
57
|
-
functions = { ...functions, [key]: handler({ call, notify }) };
|
|
58
|
-
}
|
|
59
|
-
return { ...functions };
|
|
60
|
-
};
|
|
61
|
-
};
|
|
12
|
+
export const createBroker = createBrokerFactory(createCreateOrGetOngoingRequests(new WeakMap()), createExtendBrokerImplementation(new WeakMap()), generateUniqueNumber, isMessagePort);
|
|
62
13
|
//# sourceMappingURL=module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,gCAAgC,EAAE,MAAM,4CAA4C,CAAC;AAC9F,OAAO,EAAE,gCAAgC,EAAE,MAAM,0CAA0C,CAAC;AAC5F,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD;;;GAGG;AACH,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAE9B,MAAM,CAAC,MAAM,YAAY,GAAG,mBAAmB,CAC3C,gCAAgC,CAAC,IAAI,OAAO,EAA8E,CAAC,EAC3H,gCAAgC,CAAC,IAAI,OAAO,EAAuB,CAAC,EACpE,oBAAoB,EACpB,aAAa,CAChB,CAAC"}
|
package/build/es5/bundle.js
CHANGED
|
@@ -1,160 +1,166 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/runtime/helpers/defineProperty'), require('@babel/runtime/helpers/slicedToArray'), require('
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', '@babel/runtime/helpers/defineProperty', '@babel/runtime/helpers/slicedToArray', '
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.brokerFactory = {}, global.
|
|
5
|
-
})(this, (function (exports, _defineProperty, _slicedToArray,
|
|
6
|
-
|
|
7
|
-
var isMessagePort = function isMessagePort(sender) {
|
|
8
|
-
return typeof sender.start === 'function';
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
var PORT_MAP = new WeakMap();
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('fast-unique-numbers'), require('@babel/runtime/helpers/defineProperty'), require('@babel/runtime/helpers/slicedToArray'), require('@babel/runtime/helpers/asyncToGenerator'), require('@babel/runtime/regenerator')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'fast-unique-numbers', '@babel/runtime/helpers/defineProperty', '@babel/runtime/helpers/slicedToArray', '@babel/runtime/helpers/asyncToGenerator', '@babel/runtime/regenerator'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.brokerFactory = {}, global.fastUniqueNumbers, global._defineProperty, global._slicedToArray, global._asyncToGenerator, global._regeneratorRuntime));
|
|
5
|
+
})(this, (function (exports, fastUniqueNumbers, _defineProperty, _slicedToArray, _asyncToGenerator, _regeneratorRuntime) { 'use strict';
|
|
12
6
|
|
|
13
7
|
function ownKeys$1(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
14
8
|
function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
15
|
-
var
|
|
16
|
-
return
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
9
|
+
var createBrokerFactory = function createBrokerFactory(createOrGetOngoingRequests, extendBrokerImplementation, generateUniqueNumber, isMessagePort) {
|
|
10
|
+
return function (brokerImplementation) {
|
|
11
|
+
var fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
|
|
12
|
+
return function (sender) {
|
|
13
|
+
var ongoingRequests = createOrGetOngoingRequests(sender);
|
|
14
|
+
sender.addEventListener('message', function (_ref) {
|
|
15
|
+
var message = _ref.data;
|
|
16
|
+
var id = message.id;
|
|
17
|
+
if (id !== null && ongoingRequests.has(id)) {
|
|
18
|
+
var _ongoingRequests$get = ongoingRequests.get(id),
|
|
19
|
+
reject = _ongoingRequests$get.reject,
|
|
20
|
+
resolve = _ongoingRequests$get.resolve;
|
|
21
|
+
ongoingRequests["delete"](id);
|
|
22
|
+
if (message.error === undefined) {
|
|
23
|
+
resolve(message.result);
|
|
24
|
+
} else {
|
|
25
|
+
reject(new Error(message.error.message));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
if (isMessagePort(sender)) {
|
|
30
|
+
sender.start();
|
|
31
|
+
}
|
|
32
|
+
var call = function call(method) {
|
|
33
|
+
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
34
|
+
var transferables = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
35
|
+
return new Promise(function (resolve, reject) {
|
|
36
|
+
var id = generateUniqueNumber(ongoingRequests);
|
|
37
|
+
ongoingRequests.set(id, {
|
|
38
|
+
reject: reject,
|
|
39
|
+
resolve: resolve
|
|
40
|
+
});
|
|
41
|
+
if (params === null) {
|
|
42
|
+
sender.postMessage({
|
|
43
|
+
id: id,
|
|
44
|
+
method: method
|
|
45
|
+
}, transferables);
|
|
46
|
+
} else {
|
|
47
|
+
sender.postMessage({
|
|
48
|
+
id: id,
|
|
49
|
+
method: method,
|
|
50
|
+
params: params
|
|
51
|
+
}, transferables);
|
|
36
52
|
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
var notify = function notify(method, params) {
|
|
56
|
+
var transferables = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
57
|
+
sender.postMessage({
|
|
58
|
+
id: null,
|
|
59
|
+
method: method,
|
|
60
|
+
params: params
|
|
61
|
+
}, transferables);
|
|
62
|
+
};
|
|
63
|
+
var functions = {};
|
|
64
|
+
for (var _i = 0, _Object$entries = Object.entries(fullBrokerImplementation); _i < _Object$entries.length; _i++) {
|
|
65
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
66
|
+
key = _Object$entries$_i[0],
|
|
67
|
+
handler = _Object$entries$_i[1];
|
|
68
|
+
functions = _objectSpread$1(_objectSpread$1({}, functions), {}, _defineProperty({}, key, handler({
|
|
69
|
+
call: call,
|
|
70
|
+
notify: notify
|
|
71
|
+
})));
|
|
72
|
+
}
|
|
73
|
+
return _objectSpread$1({}, functions);
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
var createCreateOrGetOngoingRequests = function createCreateOrGetOngoingRequests(ongoingRequestsMap) {
|
|
79
|
+
return function (sender) {
|
|
80
|
+
if (ongoingRequestsMap.has(sender)) {
|
|
81
|
+
// @todo TypeScript needs to be convinced that has() works as expected.
|
|
82
|
+
return ongoingRequestsMap.get(sender);
|
|
83
|
+
}
|
|
84
|
+
var ongoingRequests = new Map();
|
|
85
|
+
ongoingRequestsMap.set(sender, ongoingRequests);
|
|
86
|
+
return ongoingRequests;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
91
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
92
|
+
var createExtendBrokerImplementation = function createExtendBrokerImplementation(portMap) {
|
|
93
|
+
return function (partialBrokerImplementation) {
|
|
94
|
+
return _objectSpread(_objectSpread({}, partialBrokerImplementation), {}, {
|
|
95
|
+
connect: function connect(_ref) {
|
|
96
|
+
var call = _ref.call;
|
|
97
|
+
return /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
98
|
+
var _MessageChannel, port1, port2, portId;
|
|
99
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
|
100
|
+
while (1) switch (_context.prev = _context.next) {
|
|
47
101
|
case 0:
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
throw new Error('The given port is not connected.');
|
|
102
|
+
_MessageChannel = new MessageChannel(), port1 = _MessageChannel.port1, port2 = _MessageChannel.port2;
|
|
103
|
+
_context.next = 1;
|
|
104
|
+
return call('connect', {
|
|
105
|
+
port: port1
|
|
106
|
+
}, [port1]);
|
|
54
107
|
case 1:
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
});
|
|
108
|
+
portId = _context.sent;
|
|
109
|
+
portMap.set(port2, portId);
|
|
110
|
+
return _context.abrupt("return", port2);
|
|
59
111
|
case 2:
|
|
60
112
|
case "end":
|
|
61
|
-
return
|
|
113
|
+
return _context.stop();
|
|
62
114
|
}
|
|
63
|
-
},
|
|
115
|
+
}, _callee);
|
|
64
116
|
}));
|
|
65
|
-
|
|
66
|
-
|
|
117
|
+
},
|
|
118
|
+
disconnect: function disconnect(_ref3) {
|
|
119
|
+
var call = _ref3.call;
|
|
120
|
+
return /*#__PURE__*/function () {
|
|
121
|
+
var _ref4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(port) {
|
|
122
|
+
var portId;
|
|
123
|
+
return _regeneratorRuntime.wrap(function (_context2) {
|
|
124
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
125
|
+
case 0:
|
|
126
|
+
portId = portMap.get(port);
|
|
127
|
+
if (!(portId === undefined)) {
|
|
128
|
+
_context2.next = 1;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
throw new Error('The given port is not connected.');
|
|
132
|
+
case 1:
|
|
133
|
+
_context2.next = 2;
|
|
134
|
+
return call('disconnect', {
|
|
135
|
+
portId: portId
|
|
136
|
+
});
|
|
137
|
+
case 2:
|
|
138
|
+
case "end":
|
|
139
|
+
return _context2.stop();
|
|
140
|
+
}
|
|
141
|
+
}, _callee2);
|
|
142
|
+
}));
|
|
143
|
+
return function (_x) {
|
|
144
|
+
return _ref4.apply(this, arguments);
|
|
145
|
+
};
|
|
146
|
+
}();
|
|
147
|
+
},
|
|
148
|
+
isSupported: function isSupported(_ref5) {
|
|
149
|
+
var call = _ref5.call;
|
|
150
|
+
return function () {
|
|
151
|
+
return call('isSupported');
|
|
67
152
|
};
|
|
68
|
-
}();
|
|
69
|
-
},
|
|
70
|
-
isSupported: function isSupported(_ref5) {
|
|
71
|
-
var call = _ref5.call;
|
|
72
|
-
return function () {
|
|
73
|
-
return call('isSupported');
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
80
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
81
|
-
var ONGOING_REQUESTS = new WeakMap();
|
|
82
|
-
var createOrGetOngoingRequests = function createOrGetOngoingRequests(sender) {
|
|
83
|
-
if (ONGOING_REQUESTS.has(sender)) {
|
|
84
|
-
// @todo TypeScript needs to be convinced that has() works as expected.
|
|
85
|
-
return ONGOING_REQUESTS.get(sender);
|
|
86
|
-
}
|
|
87
|
-
var ongoingRequests = new Map();
|
|
88
|
-
ONGOING_REQUESTS.set(sender, ongoingRequests);
|
|
89
|
-
return ongoingRequests;
|
|
90
|
-
};
|
|
91
|
-
var createBroker = function createBroker(brokerImplementation) {
|
|
92
|
-
var fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
|
|
93
|
-
return function (sender) {
|
|
94
|
-
var ongoingRequests = createOrGetOngoingRequests(sender);
|
|
95
|
-
sender.addEventListener('message', function (_ref) {
|
|
96
|
-
var message = _ref.data;
|
|
97
|
-
var id = message.id;
|
|
98
|
-
if (id !== null && ongoingRequests.has(id)) {
|
|
99
|
-
var _ongoingRequests$get = ongoingRequests.get(id),
|
|
100
|
-
reject = _ongoingRequests$get.reject,
|
|
101
|
-
resolve = _ongoingRequests$get.resolve;
|
|
102
|
-
ongoingRequests["delete"](id);
|
|
103
|
-
if (message.error === undefined) {
|
|
104
|
-
resolve(message.result);
|
|
105
|
-
} else {
|
|
106
|
-
reject(new Error(message.error.message));
|
|
107
|
-
}
|
|
108
153
|
}
|
|
109
154
|
});
|
|
110
|
-
if (isMessagePort(sender)) {
|
|
111
|
-
sender.start();
|
|
112
|
-
}
|
|
113
|
-
var call = function call(method) {
|
|
114
|
-
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
115
|
-
var transferables = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
116
|
-
return new Promise(function (resolve, reject) {
|
|
117
|
-
var id = fastUniqueNumbers.generateUniqueNumber(ongoingRequests);
|
|
118
|
-
ongoingRequests.set(id, {
|
|
119
|
-
reject: reject,
|
|
120
|
-
resolve: resolve
|
|
121
|
-
});
|
|
122
|
-
if (params === null) {
|
|
123
|
-
sender.postMessage({
|
|
124
|
-
id: id,
|
|
125
|
-
method: method
|
|
126
|
-
}, transferables);
|
|
127
|
-
} else {
|
|
128
|
-
sender.postMessage({
|
|
129
|
-
id: id,
|
|
130
|
-
method: method,
|
|
131
|
-
params: params
|
|
132
|
-
}, transferables);
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
};
|
|
136
|
-
var notify = function notify(method, params) {
|
|
137
|
-
var transferables = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
138
|
-
sender.postMessage({
|
|
139
|
-
id: null,
|
|
140
|
-
method: method,
|
|
141
|
-
params: params
|
|
142
|
-
}, transferables);
|
|
143
|
-
};
|
|
144
|
-
var functions = {};
|
|
145
|
-
for (var _i = 0, _Object$entries = Object.entries(fullBrokerImplementation); _i < _Object$entries.length; _i++) {
|
|
146
|
-
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
147
|
-
key = _Object$entries$_i[0],
|
|
148
|
-
handler = _Object$entries$_i[1];
|
|
149
|
-
functions = _objectSpread(_objectSpread({}, functions), {}, _defineProperty({}, key, handler({
|
|
150
|
-
call: call,
|
|
151
|
-
notify: notify
|
|
152
|
-
})));
|
|
153
|
-
}
|
|
154
|
-
return _objectSpread({}, functions);
|
|
155
155
|
};
|
|
156
156
|
};
|
|
157
157
|
|
|
158
|
+
var isMessagePort = function isMessagePort(sender) {
|
|
159
|
+
return typeof sender.start === 'function';
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
var createBroker = createBrokerFactory(createCreateOrGetOngoingRequests(new WeakMap()), createExtendBrokerImplementation(new WeakMap()), fastUniqueNumbers.generateUniqueNumber, isMessagePort);
|
|
163
|
+
|
|
158
164
|
exports.createBroker = createBroker;
|
|
159
165
|
|
|
160
166
|
}));
|
package/package.json
CHANGED
|
@@ -16,40 +16,28 @@
|
|
|
16
16
|
},
|
|
17
17
|
"description": "A little factory function to create a broker for a JSON-RPC based Web Worker.",
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@babel/core": "^7.28.
|
|
19
|
+
"@babel/core": "^7.28.5",
|
|
20
20
|
"@babel/plugin-external-helpers": "^7.27.1",
|
|
21
|
-
"@babel/plugin-transform-runtime": "^7.28.
|
|
22
|
-
"@babel/preset-env": "^7.28.
|
|
21
|
+
"@babel/plugin-transform-runtime": "^7.28.5",
|
|
22
|
+
"@babel/preset-env": "^7.28.5",
|
|
23
23
|
"@commitlint/cli": "^19.8.1",
|
|
24
24
|
"@commitlint/config-angular": "^19.8.1",
|
|
25
|
-
"@rollup/plugin-babel": "^6.0
|
|
26
|
-
"
|
|
25
|
+
"@rollup/plugin-babel": "^6.1.0",
|
|
26
|
+
"@vitest/browser-webdriverio": "^4.0.16",
|
|
27
27
|
"commitizen": "^4.3.1",
|
|
28
28
|
"cz-conventional-changelog": "^3.3.0",
|
|
29
29
|
"eslint": "^8.57.0",
|
|
30
|
-
"eslint-config-holy-grail": "^61.0.
|
|
30
|
+
"eslint-config-holy-grail": "^61.0.3",
|
|
31
31
|
"husky": "^9.1.7",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"karma-sauce-launcher": "^4.3.6",
|
|
37
|
-
"karma-sinon-chai": "^2.0.2",
|
|
38
|
-
"karma-webkit-launcher": "^2.6.0",
|
|
39
|
-
"karma-webpack": "^5.0.1",
|
|
40
|
-
"lint-staged": "^16.1.6",
|
|
41
|
-
"mocha": "^11.7.2",
|
|
42
|
-
"prettier": "^3.6.2",
|
|
43
|
-
"rimraf": "^6.0.1",
|
|
44
|
-
"rollup": "^4.52.0",
|
|
45
|
-
"sinon": "^17.0.2",
|
|
46
|
-
"sinon-chai": "^3.7.0",
|
|
47
|
-
"ts-loader": "^9.5.4",
|
|
32
|
+
"lint-staged": "^16.2.7",
|
|
33
|
+
"prettier": "^3.7.4",
|
|
34
|
+
"rimraf": "^6.1.2",
|
|
35
|
+
"rollup": "^4.53.5",
|
|
48
36
|
"tsconfig-holy-grail": "^15.0.2",
|
|
49
37
|
"tslint": "^6.1.3",
|
|
50
38
|
"tslint-config-holy-grail": "^56.0.6",
|
|
51
|
-
"typescript": "^5.9.
|
|
52
|
-
"
|
|
39
|
+
"typescript": "^5.9.3",
|
|
40
|
+
"vitest": "^4.0.16"
|
|
53
41
|
},
|
|
54
42
|
"files": [
|
|
55
43
|
"build/es2019/",
|
|
@@ -74,8 +62,8 @@
|
|
|
74
62
|
"prepare": "husky",
|
|
75
63
|
"prepublishOnly": "npm run build",
|
|
76
64
|
"test": "npm run lint && npm run build && npm run test:unit",
|
|
77
|
-
"test:unit": "if [ \"$TYPE\" = \"\" -o \"$TYPE\" = \"unit\" ]; then
|
|
65
|
+
"test:unit": "if [ \"$TYPE\" = \"\" -o \"$TYPE\" = \"unit\" ]; then npx vitest --config config/vitest/unit.ts; fi"
|
|
78
66
|
},
|
|
79
67
|
"types": "build/es2019/module.d.ts",
|
|
80
|
-
"version": "3.1.
|
|
68
|
+
"version": "3.1.11"
|
|
81
69
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { generateUniqueNumber as generateUniqueNumberFunction } from 'fast-unique-numbers';
|
|
2
|
+
import { IWorkerDefinition, IWorkerErrorMessage, IWorkerResultMessage } from 'worker-factory';
|
|
3
|
+
import type { isMessagePort as isMessagePortFunction } from '../guards/message-port';
|
|
4
|
+
import { IBrokerDefinition, IDefaultBrokerDefinition, IWorkerEvent } from '../interfaces';
|
|
5
|
+
import { TBrokerImplementation } from '../types';
|
|
6
|
+
import type { createCreateOrGetOngoingRequests } from './create-or-get-ongoing-requests';
|
|
7
|
+
import type { createExtendBrokerImplementation } from './extend-broker-implementation';
|
|
8
|
+
|
|
9
|
+
export const createBrokerFactory =
|
|
10
|
+
(
|
|
11
|
+
createOrGetOngoingRequests: ReturnType<typeof createCreateOrGetOngoingRequests>,
|
|
12
|
+
extendBrokerImplementation: ReturnType<typeof createExtendBrokerImplementation>,
|
|
13
|
+
generateUniqueNumber: typeof generateUniqueNumberFunction,
|
|
14
|
+
isMessagePort: typeof isMessagePortFunction
|
|
15
|
+
) =>
|
|
16
|
+
<T extends IBrokerDefinition, U extends IWorkerDefinition>(
|
|
17
|
+
brokerImplementation: TBrokerImplementation<T, U>
|
|
18
|
+
): ((sender: MessagePort | Worker) => T & IDefaultBrokerDefinition) => {
|
|
19
|
+
const fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
|
|
20
|
+
|
|
21
|
+
return (sender: MessagePort | Worker) => {
|
|
22
|
+
const ongoingRequests = createOrGetOngoingRequests(sender);
|
|
23
|
+
|
|
24
|
+
sender.addEventListener('message', <EventListener>(({ data: message }: IWorkerEvent) => {
|
|
25
|
+
const { id } = message;
|
|
26
|
+
|
|
27
|
+
if (id !== null && ongoingRequests.has(id)) {
|
|
28
|
+
const { reject, resolve } = <{ reject: Function; resolve: Function }>ongoingRequests.get(id);
|
|
29
|
+
|
|
30
|
+
ongoingRequests.delete(id);
|
|
31
|
+
|
|
32
|
+
if ((<IWorkerErrorMessage>message).error === undefined) {
|
|
33
|
+
resolve((<IWorkerResultMessage>message).result);
|
|
34
|
+
} else {
|
|
35
|
+
reject(new Error((<IWorkerErrorMessage>message).error.message));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
if (isMessagePort(sender)) {
|
|
41
|
+
sender.start();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const call = <V extends keyof U>(method: V, params: U[V]['params'] = null, transferables: U[V]['transferables'] = []) => {
|
|
45
|
+
return new Promise<U[V]['response']['result']>((resolve, reject) => {
|
|
46
|
+
const id = generateUniqueNumber(ongoingRequests);
|
|
47
|
+
|
|
48
|
+
ongoingRequests.set(id, { reject, resolve });
|
|
49
|
+
|
|
50
|
+
if (params === null) {
|
|
51
|
+
sender.postMessage({ id, method }, <Transferable[]>transferables);
|
|
52
|
+
} else {
|
|
53
|
+
sender.postMessage({ id, method, params }, <Transferable[]>transferables);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
const notify = <V extends keyof U>(method: V, params: U[V]['params'], transferables: U[V]['transferables'] = []) => {
|
|
58
|
+
sender.postMessage({ id: null, method, params }, <Transferable[]>transferables);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
let functions: object = {};
|
|
62
|
+
|
|
63
|
+
for (const [key, handler] of Object.entries(fullBrokerImplementation)) {
|
|
64
|
+
functions = { ...functions, [key]: handler({ call, notify }) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return <T & IDefaultBrokerDefinition>{ ...functions };
|
|
68
|
+
};
|
|
69
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const createCreateOrGetOngoingRequests =
|
|
2
|
+
(ongoingRequestsMap: WeakMap<MessagePort | Worker, Map<number, { reject: Function; resolve: Function }>>) =>
|
|
3
|
+
(sender: MessagePort | Worker): Map<number, { reject: Function; resolve: Function }> => {
|
|
4
|
+
if (ongoingRequestsMap.has(sender)) {
|
|
5
|
+
// @todo TypeScript needs to be convinced that has() works as expected.
|
|
6
|
+
return <Map<number, { reject: Function; resolve: Function }>>ongoingRequestsMap.get(sender);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const ongoingRequests: Map<number, { reject: Function; resolve: Function }> = new Map();
|
|
10
|
+
|
|
11
|
+
ongoingRequestsMap.set(sender, ongoingRequests);
|
|
12
|
+
|
|
13
|
+
return ongoingRequests;
|
|
14
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { IWorkerDefinition } from 'worker-factory';
|
|
2
|
+
import { IBrokerDefinition, IDefaultBrokerDefinition } from '../interfaces';
|
|
3
|
+
import { TBrokerImplementation } from '../types';
|
|
4
|
+
|
|
5
|
+
export const createExtendBrokerImplementation =
|
|
6
|
+
(portMap: WeakMap<MessagePort, number>) =>
|
|
7
|
+
<T extends IBrokerDefinition, U extends IWorkerDefinition>(
|
|
8
|
+
partialBrokerImplementation: TBrokerImplementation<T, U>
|
|
9
|
+
): TBrokerImplementation<T & IDefaultBrokerDefinition, U> =>
|
|
10
|
+
<TBrokerImplementation<T & IDefaultBrokerDefinition, U>>{
|
|
11
|
+
...partialBrokerImplementation,
|
|
12
|
+
connect: ({ call }) => {
|
|
13
|
+
return async (): Promise<MessagePort> => {
|
|
14
|
+
const { port1, port2 } = new MessageChannel();
|
|
15
|
+
|
|
16
|
+
const portId = <number>await call('connect', { port: port1 }, [port1]);
|
|
17
|
+
|
|
18
|
+
portMap.set(port2, portId);
|
|
19
|
+
|
|
20
|
+
return port2;
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
disconnect: ({ call }) => {
|
|
24
|
+
return async (port: MessagePort): Promise<void> => {
|
|
25
|
+
const portId = portMap.get(port);
|
|
26
|
+
|
|
27
|
+
if (portId === undefined) {
|
|
28
|
+
throw new Error('The given port is not connected.');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
await call('disconnect', { portId });
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
isSupported: ({ call }) => {
|
|
35
|
+
return () => call('isSupported');
|
|
36
|
+
}
|
|
37
|
+
};
|
package/src/module.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { generateUniqueNumber } from 'fast-unique-numbers';
|
|
2
|
-
import {
|
|
2
|
+
import { createBrokerFactory } from './factories/create-broker';
|
|
3
|
+
import { createCreateOrGetOngoingRequests } from './factories/create-or-get-ongoing-requests';
|
|
4
|
+
import { createExtendBrokerImplementation } from './factories/extend-broker-implementation';
|
|
3
5
|
import { isMessagePort } from './guards/message-port';
|
|
4
|
-
import { extendBrokerImplementation } from './helpers/extend-broker-implementation';
|
|
5
|
-
import { IBrokerDefinition, IDefaultBrokerDefinition, IWorkerEvent } from './interfaces';
|
|
6
|
-
import { TBrokerImplementation } from './types';
|
|
7
6
|
|
|
8
7
|
/*
|
|
9
8
|
* @todo Explicitly referencing the barrel file seems to be necessary when enabling the
|
|
@@ -12,72 +11,9 @@ import { TBrokerImplementation } from './types';
|
|
|
12
11
|
export * from './interfaces/index';
|
|
13
12
|
export * from './types/index';
|
|
14
13
|
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const ongoingRequests: Map<number, { reject: Function; resolve: Function }> = new Map();
|
|
24
|
-
|
|
25
|
-
ONGOING_REQUESTS.set(sender, ongoingRequests);
|
|
26
|
-
|
|
27
|
-
return ongoingRequests;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export const createBroker = <T extends IBrokerDefinition, U extends IWorkerDefinition>(
|
|
31
|
-
brokerImplementation: TBrokerImplementation<T, U>
|
|
32
|
-
): ((sender: MessagePort | Worker) => T & IDefaultBrokerDefinition) => {
|
|
33
|
-
const fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
|
|
34
|
-
|
|
35
|
-
return (sender: MessagePort | Worker) => {
|
|
36
|
-
const ongoingRequests = createOrGetOngoingRequests(sender);
|
|
37
|
-
|
|
38
|
-
sender.addEventListener('message', <EventListener>(({ data: message }: IWorkerEvent) => {
|
|
39
|
-
const { id } = message;
|
|
40
|
-
|
|
41
|
-
if (id !== null && ongoingRequests.has(id)) {
|
|
42
|
-
const { reject, resolve } = <{ reject: Function; resolve: Function }>ongoingRequests.get(id);
|
|
43
|
-
|
|
44
|
-
ongoingRequests.delete(id);
|
|
45
|
-
|
|
46
|
-
if ((<IWorkerErrorMessage>message).error === undefined) {
|
|
47
|
-
resolve((<IWorkerResultMessage>message).result);
|
|
48
|
-
} else {
|
|
49
|
-
reject(new Error((<IWorkerErrorMessage>message).error.message));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}));
|
|
53
|
-
|
|
54
|
-
if (isMessagePort(sender)) {
|
|
55
|
-
sender.start();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const call = <V extends keyof U>(method: V, params: U[V]['params'] = null, transferables: U[V]['transferables'] = []) => {
|
|
59
|
-
return new Promise<U[V]['response']['result']>((resolve, reject) => {
|
|
60
|
-
const id = generateUniqueNumber(ongoingRequests);
|
|
61
|
-
|
|
62
|
-
ongoingRequests.set(id, { reject, resolve });
|
|
63
|
-
|
|
64
|
-
if (params === null) {
|
|
65
|
-
sender.postMessage({ id, method }, <Transferable[]>transferables);
|
|
66
|
-
} else {
|
|
67
|
-
sender.postMessage({ id, method, params }, <Transferable[]>transferables);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
};
|
|
71
|
-
const notify = <V extends keyof U>(method: V, params: U[V]['params'], transferables: U[V]['transferables'] = []) => {
|
|
72
|
-
sender.postMessage({ id: null, method, params }, <Transferable[]>transferables);
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
let functions: object = {};
|
|
76
|
-
|
|
77
|
-
for (const [key, handler] of Object.entries(fullBrokerImplementation)) {
|
|
78
|
-
functions = { ...functions, [key]: handler({ call, notify }) };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return <T & IDefaultBrokerDefinition>{ ...functions };
|
|
82
|
-
};
|
|
83
|
-
};
|
|
14
|
+
export const createBroker = createBrokerFactory(
|
|
15
|
+
createCreateOrGetOngoingRequests(new WeakMap<MessagePort | Worker, Map<number, { reject: Function; resolve: Function }>>()),
|
|
16
|
+
createExtendBrokerImplementation(new WeakMap<MessagePort, number>()),
|
|
17
|
+
generateUniqueNumber,
|
|
18
|
+
isMessagePort
|
|
19
|
+
);
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { IWorkerDefinition } from 'worker-factory';
|
|
2
|
-
import { IBrokerDefinition, IDefaultBrokerDefinition } from '../interfaces';
|
|
3
|
-
import { TBrokerImplementation } from '../types';
|
|
4
|
-
export declare const extendBrokerImplementation: <T extends IBrokerDefinition, U extends IWorkerDefinition>(partialBrokerImplementation: TBrokerImplementation<T, U>) => TBrokerImplementation<T & IDefaultBrokerDefinition, U>;
|
|
5
|
-
//# sourceMappingURL=extend-broker-implementation.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"extend-broker-implementation.d.ts","sourceRoot":"","sources":["../../../src/helpers/extend-broker-implementation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGjD,eAAO,MAAM,0BAA0B,GAAI,CAAC,SAAS,iBAAiB,EAAE,CAAC,SAAS,iBAAiB,EAC/F,6BAA6B,qBAAqB,CAAC,CAAC,EAAE,CAAC,CAAC,KACzD,qBAAqB,CAAC,CAAC,GAAG,wBAAwB,EAAE,CAAC,CA4BnD,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"extend-broker-implementation.js","sourceRoot":"","sources":["../../../src/helpers/extend-broker-implementation.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,MAAM,CAAC,MAAM,0BAA0B,GAAG,CACtC,2BAAwD,EACF,EAAE,CACxD,CAAwD;IACpD,GAAG,2BAA2B;IAC9B,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE;QAClB,OAAO,KAAK,IAA0B,EAAE;YACpC,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,IAAI,cAAc,EAAE,CAAC;YAE9C,MAAM,MAAM,GAAW,MAAM,IAAI,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;YAEvE,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAE5B,OAAO,KAAK,CAAC;QACjB,CAAC,CAAC;IACN,CAAC;IACD,UAAU,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE;QACrB,OAAO,KAAK,EAAE,IAAiB,EAAiB,EAAE;YAC9C,MAAM,MAAM,GAAG,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAElC,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACvB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;YACxD,CAAC;YAED,MAAM,IAAI,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QACzC,CAAC,CAAC;IACN,CAAC;IACD,WAAW,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE;QACtB,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IACrC,CAAC;CACJ,CAAA,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"port-map.d.ts","sourceRoot":"","sources":["../../../src/helpers/port-map.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,EAAE,OAAO,CAAC,WAAW,EAAE,MAAM,CAAiB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"port-map.js","sourceRoot":"","sources":["../../../src/helpers/port-map.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,QAAQ,GAAiC,IAAI,OAAO,EAAE,CAAC"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { IWorkerDefinition } from 'worker-factory';
|
|
2
|
-
import { IBrokerDefinition, IDefaultBrokerDefinition } from '../interfaces';
|
|
3
|
-
import { TBrokerImplementation } from '../types';
|
|
4
|
-
import { PORT_MAP } from './port-map';
|
|
5
|
-
|
|
6
|
-
export const extendBrokerImplementation = <T extends IBrokerDefinition, U extends IWorkerDefinition>(
|
|
7
|
-
partialBrokerImplementation: TBrokerImplementation<T, U>
|
|
8
|
-
): TBrokerImplementation<T & IDefaultBrokerDefinition, U> =>
|
|
9
|
-
<TBrokerImplementation<T & IDefaultBrokerDefinition, U>>{
|
|
10
|
-
...partialBrokerImplementation,
|
|
11
|
-
connect: ({ call }) => {
|
|
12
|
-
return async (): Promise<MessagePort> => {
|
|
13
|
-
const { port1, port2 } = new MessageChannel();
|
|
14
|
-
|
|
15
|
-
const portId = <number>await call('connect', { port: port1 }, [port1]);
|
|
16
|
-
|
|
17
|
-
PORT_MAP.set(port2, portId);
|
|
18
|
-
|
|
19
|
-
return port2;
|
|
20
|
-
};
|
|
21
|
-
},
|
|
22
|
-
disconnect: ({ call }) => {
|
|
23
|
-
return async (port: MessagePort): Promise<void> => {
|
|
24
|
-
const portId = PORT_MAP.get(port);
|
|
25
|
-
|
|
26
|
-
if (portId === undefined) {
|
|
27
|
-
throw new Error('The given port is not connected.');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
await call('disconnect', { portId });
|
|
31
|
-
};
|
|
32
|
-
},
|
|
33
|
-
isSupported: ({ call }) => {
|
|
34
|
-
return () => call('isSupported');
|
|
35
|
-
}
|
|
36
|
-
};
|
package/src/helpers/port-map.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const PORT_MAP: WeakMap<MessagePort, number> = new WeakMap();
|