threadsx 2.0.2 → 2.2.0
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/errors.d.ts +16 -0
- package/dist/errors.js +31 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/master/implementation.node.js +34 -18
- package/dist/master/invocation-proxy.js +45 -11
- package/dist/master/pool.d.ts +1 -24
- package/dist/master/pool.js +41 -8
- package/dist/master/spawn.js +24 -7
- package/dist/node-require.d.ts +13 -0
- package/dist/node-require.js +87 -0
- package/dist/transferable.d.ts +1 -0
- package/dist/types/master.d.ts +22 -1
- package/dist/types/master.js +0 -1
- package/dist/types/worker.d.ts +1 -0
- package/dist/worker/implementation.browser.d.ts +1 -1
- package/dist/worker/implementation.worker_threads.d.ts +1 -1
- package/dist/worker_threads.d.ts +3 -7
- package/dist/worker_threads.js +35 -11
- package/dist-esm/common.js +1 -0
- package/dist-esm/common.js.map +1 -0
- package/dist-esm/errors.js +27 -0
- package/dist-esm/errors.js.map +1 -0
- package/dist-esm/index.js +2 -0
- package/dist-esm/index.js.map +1 -0
- package/dist-esm/master/get-bundle-url.browser.js +1 -0
- package/dist-esm/master/get-bundle-url.browser.js.map +1 -0
- package/dist-esm/master/implementation.browser.js +1 -0
- package/dist-esm/master/implementation.browser.js.map +1 -0
- package/dist-esm/master/implementation.js +1 -0
- package/dist-esm/master/implementation.js.map +1 -0
- package/dist-esm/master/implementation.node.js +35 -18
- package/dist-esm/master/implementation.node.js.map +1 -0
- package/dist-esm/master/index.js +1 -0
- package/dist-esm/master/index.js.map +1 -0
- package/dist-esm/master/invocation-proxy.js +46 -11
- package/dist-esm/master/invocation-proxy.js.map +1 -0
- package/dist-esm/master/pool-types.js +1 -0
- package/dist-esm/master/pool-types.js.map +1 -0
- package/dist-esm/master/pool.js +42 -8
- package/dist-esm/master/pool.js.map +1 -0
- package/dist-esm/master/register.js +1 -0
- package/dist-esm/master/register.js.map +1 -0
- package/dist-esm/master/spawn.js +25 -7
- package/dist-esm/master/spawn.js.map +1 -0
- package/dist-esm/master/thread.js +1 -0
- package/dist-esm/master/thread.js.map +1 -0
- package/dist-esm/node-require.js +50 -0
- package/dist-esm/node-require.js.map +1 -0
- package/dist-esm/observable-promise.js +1 -0
- package/dist-esm/observable-promise.js.map +1 -0
- package/dist-esm/observable.js +1 -0
- package/dist-esm/observable.js.map +1 -0
- package/dist-esm/ponyfills.js +1 -0
- package/dist-esm/ponyfills.js.map +1 -0
- package/dist-esm/promise.js +1 -0
- package/dist-esm/promise.js.map +1 -0
- package/dist-esm/serializers.js +1 -0
- package/dist-esm/serializers.js.map +1 -0
- package/dist-esm/symbols.js +1 -0
- package/dist-esm/symbols.js.map +1 -0
- package/dist-esm/transferable.js +1 -0
- package/dist-esm/transferable.js.map +1 -0
- package/dist-esm/types/master.js +1 -1
- package/dist-esm/types/master.js.map +1 -0
- package/dist-esm/types/messages.js +1 -0
- package/dist-esm/types/messages.js.map +1 -0
- package/dist-esm/types/worker.js +1 -0
- package/dist-esm/types/worker.js.map +1 -0
- package/dist-esm/worker/bundle-entry.js +1 -0
- package/dist-esm/worker/bundle-entry.js.map +1 -0
- package/dist-esm/worker/implementation.browser.js +1 -0
- package/dist-esm/worker/implementation.browser.js.map +1 -0
- package/dist-esm/worker/implementation.js +1 -0
- package/dist-esm/worker/implementation.js.map +1 -0
- package/dist-esm/worker/implementation.worker_threads.js +1 -0
- package/dist-esm/worker/implementation.worker_threads.js.map +1 -0
- package/dist-esm/worker/index.js +1 -0
- package/dist-esm/worker/index.js.map +1 -0
- package/dist-esm/worker_threads.js +3 -11
- package/dist-esm/worker_threads.js.map +1 -0
- package/esm/common.mjs +20 -0
- package/esm/errors.mjs +34 -0
- package/esm/index.mjs +16 -0
- package/esm/master/get-bundle-url.browser.mjs +32 -0
- package/esm/master/implementation.browser.mjs +76 -0
- package/esm/master/implementation.mjs +24 -0
- package/esm/master/implementation.node.mjs +186 -0
- package/esm/master/index.mjs +17 -0
- package/esm/master/invocation-proxy.mjs +164 -0
- package/esm/master/pool-types.mjs +20 -0
- package/esm/master/pool.mjs +324 -0
- package/esm/master/register.mjs +14 -0
- package/esm/master/spawn.mjs +179 -0
- package/esm/master/thread.mjs +28 -0
- package/esm/node-require.mjs +58 -0
- package/esm/observable-promise.mjs +155 -0
- package/esm/observable.mjs +45 -0
- package/esm/ponyfills.mjs +26 -0
- package/esm/promise.mjs +32 -0
- package/esm/serializers.mjs +55 -0
- package/esm/symbols.mjs +13 -0
- package/esm/transferable.mjs +31 -0
- package/esm/types/master.mjs +15 -0
- package/esm/types/messages.mjs +25 -0
- package/esm/worker/implementation.browser.mjs +31 -0
- package/esm/worker/implementation.mjs +21 -0
- package/esm/worker/implementation.worker_threads.mjs +44 -0
- package/esm/worker/index.mjs +224 -0
- package/esm/worker_threads.mjs +13 -0
- package/package.json +32 -24
- package/index.mjs +0 -4
- package/observable.mjs +0 -1
- package/register.mjs +0 -2
- package/worker.mjs +0 -1
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
import { Observable } from 'observable-fns';
|
|
8
|
+
|
|
9
|
+
var _a;
|
|
10
|
+
const doNothing = () => undefined;
|
|
11
|
+
const returnInput = (input) => input;
|
|
12
|
+
const runDeferred = (fn) => Promise.resolve().then(fn);
|
|
13
|
+
function fail(error) {
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
function isThenable(thing) {
|
|
17
|
+
return thing && typeof thing.then === "function";
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a hybrid, combining the APIs of an Observable and a Promise.
|
|
21
|
+
*
|
|
22
|
+
* It is used to proxy async process states when we are initially not sure
|
|
23
|
+
* if that async process will yield values once (-> Promise) or multiple
|
|
24
|
+
* times (-> Observable).
|
|
25
|
+
*
|
|
26
|
+
* Note that the observable promise inherits some of the observable's characteristics:
|
|
27
|
+
* The `init` function will be called *once for every time anyone subscribes to it*.
|
|
28
|
+
*
|
|
29
|
+
* If this is undesired, derive a hot observable from it using `makeHot()` and
|
|
30
|
+
* subscribe to that.
|
|
31
|
+
*/
|
|
32
|
+
class ObservablePromise extends Observable {
|
|
33
|
+
constructor(init) {
|
|
34
|
+
super((originalObserver) => {
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
36
|
+
const self = this;
|
|
37
|
+
const observer = Object.assign(Object.assign({}, originalObserver), { complete() {
|
|
38
|
+
originalObserver.complete();
|
|
39
|
+
self.onCompletion();
|
|
40
|
+
},
|
|
41
|
+
error(error) {
|
|
42
|
+
originalObserver.error(error);
|
|
43
|
+
self.onError(error);
|
|
44
|
+
},
|
|
45
|
+
next(value) {
|
|
46
|
+
originalObserver.next(value);
|
|
47
|
+
self.onNext(value);
|
|
48
|
+
} });
|
|
49
|
+
try {
|
|
50
|
+
this.initHasRun = true;
|
|
51
|
+
return init(observer);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
observer.error(error);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
this.initHasRun = false;
|
|
58
|
+
this.fulfillmentCallbacks = [];
|
|
59
|
+
this.rejectionCallbacks = [];
|
|
60
|
+
this.firstValueSet = false;
|
|
61
|
+
this.state = "pending";
|
|
62
|
+
this[_a] = "[object ObservablePromise]";
|
|
63
|
+
}
|
|
64
|
+
onNext(value) {
|
|
65
|
+
if (!this.firstValueSet) {
|
|
66
|
+
this.firstValue = value;
|
|
67
|
+
this.firstValueSet = true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
onError(error) {
|
|
71
|
+
this.state = "rejected";
|
|
72
|
+
this.rejection = error;
|
|
73
|
+
for (const onRejected of this.rejectionCallbacks) {
|
|
74
|
+
// Promisifying the call to turn errors into unhandled promise rejections
|
|
75
|
+
// instead of them failing sync and cancelling the iteration
|
|
76
|
+
runDeferred(() => onRejected(error));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
onCompletion() {
|
|
80
|
+
this.state = "fulfilled";
|
|
81
|
+
for (const onFulfilled of this.fulfillmentCallbacks) {
|
|
82
|
+
// Promisifying the call to turn errors into unhandled promise rejections
|
|
83
|
+
// instead of them failing sync and cancelling the iteration
|
|
84
|
+
runDeferred(() => onFulfilled(this.firstValue));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
then(onFulfilledRaw, onRejectedRaw) {
|
|
88
|
+
const onFulfilled = onFulfilledRaw || returnInput;
|
|
89
|
+
const onRejected = onRejectedRaw || fail;
|
|
90
|
+
let onRejectedCalled = false;
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const rejectionCallback = (error) => {
|
|
93
|
+
if (onRejectedCalled)
|
|
94
|
+
return;
|
|
95
|
+
onRejectedCalled = true;
|
|
96
|
+
try {
|
|
97
|
+
resolve(onRejected(error));
|
|
98
|
+
}
|
|
99
|
+
catch (anotherError) {
|
|
100
|
+
reject(anotherError);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const fulfillmentCallback = (value) => {
|
|
104
|
+
try {
|
|
105
|
+
resolve(onFulfilled(value));
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
rejectionCallback(error);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
if (!this.initHasRun) {
|
|
112
|
+
this.subscribe({ error: rejectionCallback });
|
|
113
|
+
}
|
|
114
|
+
if (this.state === "fulfilled") {
|
|
115
|
+
return resolve(onFulfilled(this.firstValue));
|
|
116
|
+
}
|
|
117
|
+
if (this.state === "rejected") {
|
|
118
|
+
onRejectedCalled = true;
|
|
119
|
+
return resolve(onRejected(this.rejection));
|
|
120
|
+
}
|
|
121
|
+
this.fulfillmentCallbacks.push(fulfillmentCallback);
|
|
122
|
+
this.rejectionCallbacks.push(rejectionCallback);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch(onRejected) {
|
|
126
|
+
return this.then(undefined, onRejected);
|
|
127
|
+
}
|
|
128
|
+
finally(onCompleted) {
|
|
129
|
+
const handler = onCompleted || doNothing;
|
|
130
|
+
return this.then((value) => {
|
|
131
|
+
handler();
|
|
132
|
+
return value;
|
|
133
|
+
}, () => handler());
|
|
134
|
+
}
|
|
135
|
+
static from(thing) {
|
|
136
|
+
if (isThenable(thing)) {
|
|
137
|
+
return new ObservablePromise(observer => {
|
|
138
|
+
const onFulfilled = (value) => {
|
|
139
|
+
observer.next(value);
|
|
140
|
+
observer.complete();
|
|
141
|
+
};
|
|
142
|
+
const onRejected = (error) => {
|
|
143
|
+
observer.error(error);
|
|
144
|
+
};
|
|
145
|
+
thing.then(onFulfilled, onRejected);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
return super.from(thing);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
_a = Symbol.toStringTag;
|
|
154
|
+
|
|
155
|
+
export { ObservablePromise };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
import { Observable } from 'observable-fns';
|
|
8
|
+
export { Observable } from 'observable-fns';
|
|
9
|
+
|
|
10
|
+
const $observers = Symbol("observers");
|
|
11
|
+
/**
|
|
12
|
+
* Observable subject. Implements the Observable interface, but also exposes
|
|
13
|
+
* the `next()`, `error()`, `complete()` methods to initiate observable
|
|
14
|
+
* updates "from the outside".
|
|
15
|
+
*
|
|
16
|
+
* Use `Observable.from(subject)` to derive an observable that proxies all
|
|
17
|
+
* values, errors and the completion raised on this subject, but does not
|
|
18
|
+
* expose the `next()`, `error()`, `complete()` methods.
|
|
19
|
+
*/
|
|
20
|
+
class Subject extends Observable {
|
|
21
|
+
constructor() {
|
|
22
|
+
super(observer => {
|
|
23
|
+
this[$observers] = [
|
|
24
|
+
...(this[$observers] || []),
|
|
25
|
+
observer
|
|
26
|
+
];
|
|
27
|
+
const unsubscribe = () => {
|
|
28
|
+
this[$observers] = this[$observers].filter(someObserver => someObserver !== observer);
|
|
29
|
+
};
|
|
30
|
+
return unsubscribe;
|
|
31
|
+
});
|
|
32
|
+
this[$observers] = [];
|
|
33
|
+
}
|
|
34
|
+
complete() {
|
|
35
|
+
this[$observers].forEach(observer => observer.complete());
|
|
36
|
+
}
|
|
37
|
+
error(error) {
|
|
38
|
+
this[$observers].forEach(observer => observer.error(error));
|
|
39
|
+
}
|
|
40
|
+
next(value) {
|
|
41
|
+
this[$observers].forEach(observer => observer.next(value));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { Subject };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
// Based on <https://github.com/es-shims/Promise.allSettled/blob/master/implementation.js>
|
|
8
|
+
function allSettled(values) {
|
|
9
|
+
return Promise.all(values.map(item => {
|
|
10
|
+
const onFulfill = (value) => {
|
|
11
|
+
return { status: 'fulfilled', value };
|
|
12
|
+
};
|
|
13
|
+
const onReject = (reason) => {
|
|
14
|
+
return { status: 'rejected', reason };
|
|
15
|
+
};
|
|
16
|
+
const itemPromise = Promise.resolve(item);
|
|
17
|
+
try {
|
|
18
|
+
return itemPromise.then(onFulfill, onReject);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
return Promise.reject(error);
|
|
22
|
+
}
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { allSettled };
|
package/esm/promise.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
const doNothing = () => undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new promise and exposes its resolver function.
|
|
10
|
+
* Use with care!
|
|
11
|
+
*/
|
|
12
|
+
function createPromiseWithResolver() {
|
|
13
|
+
let alreadyResolved = false;
|
|
14
|
+
let resolvedTo;
|
|
15
|
+
let resolver = doNothing;
|
|
16
|
+
const promise = new Promise(resolve => {
|
|
17
|
+
if (alreadyResolved) {
|
|
18
|
+
resolve(resolvedTo);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
resolver = resolve;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const exposedResolver = (value) => {
|
|
25
|
+
alreadyResolved = true;
|
|
26
|
+
resolvedTo = value;
|
|
27
|
+
resolver(resolvedTo);
|
|
28
|
+
};
|
|
29
|
+
return [promise, exposedResolver];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { createPromiseWithResolver };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
function extendSerializer(extend, implementation) {
|
|
8
|
+
const fallbackDeserializer = extend.deserialize.bind(extend);
|
|
9
|
+
const fallbackSerializer = extend.serialize.bind(extend);
|
|
10
|
+
return {
|
|
11
|
+
deserialize(message) {
|
|
12
|
+
return implementation.deserialize(message, fallbackDeserializer);
|
|
13
|
+
},
|
|
14
|
+
serialize(input) {
|
|
15
|
+
return implementation.serialize(input, fallbackSerializer);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const DefaultErrorSerializer = {
|
|
20
|
+
deserialize(message) {
|
|
21
|
+
return Object.assign(Error(message.message), {
|
|
22
|
+
name: message.name,
|
|
23
|
+
stack: message.stack
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
serialize(error) {
|
|
27
|
+
return {
|
|
28
|
+
__error_marker: "$$error",
|
|
29
|
+
message: error.message,
|
|
30
|
+
name: error.name,
|
|
31
|
+
stack: error.stack
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const isSerializedError = (thing) => thing && typeof thing === "object" && "__error_marker" in thing && thing.__error_marker === "$$error";
|
|
36
|
+
const DefaultSerializer = {
|
|
37
|
+
deserialize(message) {
|
|
38
|
+
if (isSerializedError(message)) {
|
|
39
|
+
return DefaultErrorSerializer.deserialize(message);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return message;
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
serialize(input) {
|
|
46
|
+
if (input instanceof Error) {
|
|
47
|
+
return DefaultErrorSerializer.serialize(input);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return input;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export { DefaultSerializer, extendSerializer };
|
package/esm/symbols.mjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
const $errors = Symbol("thread.errors");
|
|
8
|
+
const $events = Symbol("thread.events");
|
|
9
|
+
const $terminate = Symbol("thread.terminate");
|
|
10
|
+
const $transferable = Symbol("thread.transferable");
|
|
11
|
+
const $worker = Symbol("thread.worker");
|
|
12
|
+
|
|
13
|
+
export { $errors, $events, $terminate, $transferable, $worker };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
import { $transferable } from './symbols.mjs';
|
|
8
|
+
|
|
9
|
+
function isTransferable(thing) {
|
|
10
|
+
if (!thing || typeof thing !== "object")
|
|
11
|
+
return false;
|
|
12
|
+
// Don't check too thoroughly, since the list of transferable things in JS might grow over time
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
function isTransferDescriptor(thing) {
|
|
16
|
+
return thing && typeof thing === "object" && thing[$transferable];
|
|
17
|
+
}
|
|
18
|
+
function Transfer(payload, transferables) {
|
|
19
|
+
if (!transferables) {
|
|
20
|
+
if (!isTransferable(payload))
|
|
21
|
+
throw Error();
|
|
22
|
+
transferables = [payload];
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
[$transferable]: true,
|
|
26
|
+
send: payload,
|
|
27
|
+
transferables
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { Transfer, isTransferDescriptor };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
/** Event as emitted by worker thread. Subscribe to using `Thread.events(thread)`. */
|
|
8
|
+
var WorkerEventType;
|
|
9
|
+
(function (WorkerEventType) {
|
|
10
|
+
WorkerEventType["internalError"] = "internalError";
|
|
11
|
+
WorkerEventType["message"] = "message";
|
|
12
|
+
WorkerEventType["termination"] = "termination";
|
|
13
|
+
})(WorkerEventType || (WorkerEventType = {}));
|
|
14
|
+
|
|
15
|
+
export { WorkerEventType };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
/////////////////////////////
|
|
8
|
+
// Messages sent by master:
|
|
9
|
+
var MasterMessageType;
|
|
10
|
+
(function (MasterMessageType) {
|
|
11
|
+
MasterMessageType["cancel"] = "cancel";
|
|
12
|
+
MasterMessageType["run"] = "run";
|
|
13
|
+
})(MasterMessageType || (MasterMessageType = {}));
|
|
14
|
+
////////////////////////////
|
|
15
|
+
// Messages sent by worker:
|
|
16
|
+
var WorkerMessageType;
|
|
17
|
+
(function (WorkerMessageType) {
|
|
18
|
+
WorkerMessageType["error"] = "error";
|
|
19
|
+
WorkerMessageType["init"] = "init";
|
|
20
|
+
WorkerMessageType["result"] = "result";
|
|
21
|
+
WorkerMessageType["running"] = "running";
|
|
22
|
+
WorkerMessageType["uncaughtError"] = "uncaughtError";
|
|
23
|
+
})(WorkerMessageType || (WorkerMessageType = {}));
|
|
24
|
+
|
|
25
|
+
export { MasterMessageType, WorkerMessageType };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
/// <reference lib="dom" />
|
|
8
|
+
const isWorkerRuntime = function isWorkerRuntime() {
|
|
9
|
+
const isWindowContext = typeof self !== "undefined" && typeof Window !== "undefined" && self instanceof Window;
|
|
10
|
+
return typeof self !== "undefined" && typeof self.postMessage === "function" && !isWindowContext;
|
|
11
|
+
};
|
|
12
|
+
const postMessageToMaster = function postMessageToMaster(data, transferList) {
|
|
13
|
+
self.postMessage(data, transferList);
|
|
14
|
+
};
|
|
15
|
+
const subscribeToMasterMessages = function subscribeToMasterMessages(onMessage) {
|
|
16
|
+
const messageHandler = (messageEvent) => {
|
|
17
|
+
onMessage(messageEvent.data);
|
|
18
|
+
};
|
|
19
|
+
const unsubscribe = () => {
|
|
20
|
+
self.removeEventListener("message", messageHandler);
|
|
21
|
+
};
|
|
22
|
+
self.addEventListener("message", messageHandler);
|
|
23
|
+
return unsubscribe;
|
|
24
|
+
};
|
|
25
|
+
var WebWorkerImplementation = {
|
|
26
|
+
isWorkerRuntime,
|
|
27
|
+
postMessageToMaster,
|
|
28
|
+
subscribeToMasterMessages
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { WebWorkerImplementation as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
import WebWorkerImplementation from './implementation.browser.mjs';
|
|
8
|
+
import WorkerThreadsImplementation from './implementation.worker_threads.mjs';
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
* This file is only a stub to make './implementation' resolve to the right module.
|
|
12
|
+
*/
|
|
13
|
+
const runningInNode = typeof process !== 'undefined' && process.arch !== 'browser' && 'pid' in process;
|
|
14
|
+
function selectNodeImplementation() {
|
|
15
|
+
return WorkerThreadsImplementation;
|
|
16
|
+
}
|
|
17
|
+
var Implementation = runningInNode
|
|
18
|
+
? selectNodeImplementation()
|
|
19
|
+
: WebWorkerImplementation;
|
|
20
|
+
|
|
21
|
+
export { Implementation as default };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { dirname as __pathDirname } from "path";
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __pathDirname(__filename);
|
|
7
|
+
import getImplementation from '../worker_threads.mjs';
|
|
8
|
+
|
|
9
|
+
function assertMessagePort(port) {
|
|
10
|
+
if (!port) {
|
|
11
|
+
throw Error("Invariant violation: MessagePort to parent is not available.");
|
|
12
|
+
}
|
|
13
|
+
return port;
|
|
14
|
+
}
|
|
15
|
+
const isWorkerRuntime = function isWorkerRuntime() {
|
|
16
|
+
return !getImplementation().isMainThread;
|
|
17
|
+
};
|
|
18
|
+
const postMessageToMaster = function postMessageToMaster(data, transferList) {
|
|
19
|
+
assertMessagePort(getImplementation().parentPort).postMessage(data, transferList);
|
|
20
|
+
};
|
|
21
|
+
const subscribeToMasterMessages = function subscribeToMasterMessages(onMessage) {
|
|
22
|
+
const parentPort = getImplementation().parentPort;
|
|
23
|
+
if (!parentPort) {
|
|
24
|
+
throw Error("Invariant violation: MessagePort to parent is not available.");
|
|
25
|
+
}
|
|
26
|
+
const messageHandler = (message) => {
|
|
27
|
+
onMessage(message);
|
|
28
|
+
};
|
|
29
|
+
const unsubscribe = () => {
|
|
30
|
+
assertMessagePort(parentPort).off("message", messageHandler);
|
|
31
|
+
};
|
|
32
|
+
assertMessagePort(parentPort).on("message", messageHandler);
|
|
33
|
+
return unsubscribe;
|
|
34
|
+
};
|
|
35
|
+
function testImplementation() {
|
|
36
|
+
}
|
|
37
|
+
var WorkerThreadsImplementation = {
|
|
38
|
+
isWorkerRuntime,
|
|
39
|
+
postMessageToMaster,
|
|
40
|
+
subscribeToMasterMessages,
|
|
41
|
+
testImplementation
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { WorkerThreadsImplementation as default };
|