tscommons-esm-webworker 0.0.2

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.
@@ -0,0 +1,2 @@
1
+ import { TEncodedObject, TPropertyObject } from 'tscommons-esm-core';
2
+ export declare function commonsWebWorkerWrapAsPromise<ParamsT extends TPropertyObject = TPropertyObject, ReturnT extends TPropertyObject = TPropertyObject>(createWorker: () => Worker, params: ParamsT, serialiseParams?: (_: ParamsT) => TEncodedObject, unserialiseReturn?: (_: TEncodedObject) => ReturnT, index?: string, activeWorkers?: Map<string, Worker>, debugContext?: string): Promise<ReturnT>;
@@ -0,0 +1,36 @@
1
+ import { commonsTypeDecodePropertyObject, commonsTypeEncodePropertyObject } from 'tscommons-esm-core';
2
+ export function commonsWebWorkerWrapAsPromise(createWorker, params, serialiseParams = (p) => commonsTypeEncodePropertyObject(p), unserialiseReturn = (serialised) => commonsTypeDecodePropertyObject(serialised), index, activeWorkers, debugContext) {
3
+ if (index && activeWorkers) {
4
+ const activeWorker = activeWorkers.get(index);
5
+ if (activeWorker) {
6
+ if (debugContext)
7
+ console.log(`Aborting active worker ${debugContext || ''}`);
8
+ activeWorker.terminate();
9
+ activeWorkers.delete(index);
10
+ }
11
+ }
12
+ const serialisedParams = serialiseParams(params);
13
+ return new Promise((resolve, error) => {
14
+ const closure = createWorker();
15
+ if (index && activeWorkers)
16
+ activeWorkers.set(index, closure);
17
+ closure.onmessage = ({ data }) => {
18
+ const serialisedReturn = data;
19
+ const unserialisedReturn = unserialiseReturn(serialisedReturn);
20
+ if (index && activeWorkers) {
21
+ const activeWorker = activeWorkers.get(index);
22
+ if (activeWorker && activeWorker !== closure) {
23
+ error(new Error('Worker previously terminated'));
24
+ return;
25
+ }
26
+ activeWorkers.delete(index);
27
+ if (debugContext)
28
+ console.log(`CONCLUDED ${debugContext || ''}`);
29
+ }
30
+ resolve(unserialisedReturn);
31
+ };
32
+ closure.postMessage(serialisedParams);
33
+ });
34
+ }
35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AAa,EAAE,CAAC;gBAC5B,MAAM,YAAY,GAAqB,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBAChE,IAAI,YAAY,IAAI,YAAY,KAAK,OAAO,EAAE,CAAC;oBAC9C,KAAK,CAAC,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC,CAAC;oBACjD,OAAO;gBACR,CAAC;gBAED,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAC5B,IAAI,YAAY;oBAAE,OAAO,CAAC,GAAG,CAAC,aAAa,YAAY,IAAI,EAAE,EAAE,CAAC,CAAC;YAClE,CAAC;YAED,OAAO,CAAC,kBAAkB,CAAC,CAAC;QAC7B,CAAC,CAAC;QAEF,OAAO,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;AACJ,CAAC;AAAA,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { commonsWebWorkerWrapAsPromise } from './helpers/workers.mjs';
2
+ export { commonsWebWorkerWrapAsPromise };
package/dist/index.mjs ADDED
@@ -0,0 +1,3 @@
1
+ import { commonsWebWorkerWrapAsPromise } from './helpers/workers.mjs';
2
+ export { commonsWebWorkerWrapAsPromise };
3
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,EACN,6BAA6B,EAC7B,CAAC"}
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "tscommons-esm-webworker",
3
+ "version": "0.0.2",
4
+ "description": "",
5
+ "scripts": {
6
+ "tsc": "./node_modules/typescript/bin/tsc",
7
+ "preprepare": "rm -rf ./dist; php ~/Dev/etim.php src/ && npm run tsc",
8
+ "publish-major": "rm -rf dist; npm run tsc && npx eslint . && npm run preprepare && npm version major && npm install && npm publish && git add . && git commit -m 'publish'",
9
+ "publish-minor": "rm -rf dist; npm run tsc && npx eslint . && npm run preprepare && npm version minor && npm install && npm publish && git add . && git commit -m 'publish'",
10
+ "publish-patch": "rm -rf dist; npm run tsc && npx eslint . && npm run preprepare && npm version patch && npm install && npm publish && git add . && git commit -m 'publish'"
11
+ },
12
+ "main": "dist/index.mjs",
13
+ "types": "dist/index.d.mjs",
14
+ "type": "module",
15
+ "author": "Pete Morris",
16
+ "license": "ISC",
17
+ "devDependencies": {
18
+ "@stylistic/eslint-plugin-ts": "^2.10.1",
19
+ "eslint-plugin-import": "^2.31.0",
20
+ "eslint-plugin-prefer-arrow-functions": "^3.4.1",
21
+ "typescript": "^5.6.3",
22
+ "typescript-eslint": "^8.14.0"
23
+ },
24
+ "files": [
25
+ "dist/**/*"
26
+ ],
27
+ "dependencies": {
28
+ "tscommons-esm-core": "^0.0.2"
29
+ }
30
+ }