web-background 0.0.11 → 0.0.13
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/dist/index.js
CHANGED
|
@@ -20,13 +20,13 @@ class UtilWorker extends TypedWorker {
|
|
|
20
20
|
this.removeEventListener('message', onMessage);
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
-
request(
|
|
23
|
+
request(payload) {
|
|
24
24
|
return new Promise(resolve => {
|
|
25
25
|
const onMessage = ({ data }) => {
|
|
26
26
|
resolve(data);
|
|
27
27
|
this.removeEventListener('message', onMessage);
|
|
28
28
|
};
|
|
29
|
-
this.postMessage(
|
|
29
|
+
this.postMessage(payload);
|
|
30
30
|
this.addEventListener('message', onMessage);
|
|
31
31
|
});
|
|
32
32
|
}
|
|
@@ -77,23 +77,35 @@ class WorkerBuilder {
|
|
|
77
77
|
});
|
|
78
78
|
`.trim();
|
|
79
79
|
const blob = new Blob([code]);
|
|
80
|
-
|
|
80
|
+
const url = URL.createObjectURL(blob);
|
|
81
|
+
const worker = new Worker(url, options);
|
|
82
|
+
const originTerminate = worker.terminate;
|
|
83
|
+
worker.terminate = () => {
|
|
84
|
+
originTerminate.call(worker);
|
|
85
|
+
URL.revokeObjectURL(url);
|
|
86
|
+
};
|
|
87
|
+
return worker;
|
|
81
88
|
}
|
|
82
89
|
}
|
|
83
90
|
|
|
91
|
+
const workerCleanupRegistry = new FinalizationRegistry((heldWorker) => heldWorker.terminate());
|
|
92
|
+
|
|
84
93
|
function background(fn) {
|
|
85
94
|
function createWorker() {
|
|
86
95
|
return WorkerBuilder.fromModule((UtilWorker), { module: fn });
|
|
87
96
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
97
|
+
let worker = null;
|
|
98
|
+
function request(...payload) {
|
|
99
|
+
if (typeof window === 'undefined') {
|
|
100
|
+
throw new Error('[web-background] You must use background in browser');
|
|
101
|
+
}
|
|
102
|
+
if (worker == null) {
|
|
103
|
+
worker = createWorker();
|
|
104
|
+
workerCleanupRegistry.register(request, worker);
|
|
92
105
|
}
|
|
93
|
-
return
|
|
106
|
+
return worker.request.call(worker, payload.length > 1 ? payload : payload[0]);
|
|
94
107
|
}
|
|
95
|
-
|
|
96
|
-
return worker.request.bind(worker);
|
|
108
|
+
return request;
|
|
97
109
|
}
|
|
98
110
|
|
|
99
111
|
export { background };
|
|
@@ -2,5 +2,5 @@ import { TypedWorker } from "./TypedWorker";
|
|
|
2
2
|
export declare class UtilWorker<Payload = never, Result = void> extends TypedWorker<Payload, Result> {
|
|
3
3
|
constructor(scriptURL: string | URL, options?: WorkerOptions);
|
|
4
4
|
subscribe(observer: (response: Result) => void): () => void;
|
|
5
|
-
request(
|
|
5
|
+
request(payload: Payload): Promise<Result>;
|
|
6
6
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const workerCleanupRegistry: FinalizationRegistry<Worker>;
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"Background",
|
|
8
8
|
"Web Background"
|
|
9
9
|
],
|
|
10
|
-
"version": "0.0.
|
|
10
|
+
"version": "0.0.13",
|
|
11
11
|
"main": "dist/index.js",
|
|
12
12
|
"module": "dist/index.js",
|
|
13
13
|
"type": "module",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"@babel/preset-env": "^7.23.3",
|
|
25
25
|
"@rollup/plugin-babel": "^6.0.4",
|
|
26
26
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
27
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
27
28
|
"rollup": "^4.4.0",
|
|
28
29
|
"rollup-plugin-typescript2": "^0.36.0",
|
|
29
30
|
"typescript": "5.2"
|