@atolljs/core 0.1.5 → 0.1.7
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/contract/reef.d.ts +88 -28
- package/dist/contract/sharedMemory.d.ts +2 -0
- package/dist/contract/types.d.ts +2 -0
- package/dist/contract/zod.d.ts +114 -14
- package/dist/devtools.d.ts +223 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +19 -17
- package/dist/index10.js +14 -185
- package/dist/index11.js +310 -47
- package/dist/index12.js +46 -59
- package/dist/index13.js +59 -20
- package/dist/index14.js +24 -60
- package/dist/index15.js +56 -43
- package/dist/index16.js +44 -50
- package/dist/index17.js +52 -38
- package/dist/index18.js +36 -54
- package/dist/index19.js +81 -47
- package/dist/index20.js +48 -51
- package/dist/index21.js +57 -0
- package/dist/index3.js +101 -41
- package/dist/index5.js +156 -35
- package/dist/index6.js +32 -485
- package/dist/index7.js +508 -19
- package/dist/index8.js +19 -33
- package/dist/index9.js +29 -14
- package/dist/pool/workerClient.d.ts +2 -0
- package/dist/pool/workerPool.d.ts +4 -0
- package/package.json +1 -1
- package/src/contract/sharedMemory.ts +19 -3
- package/src/contract/types.ts +2 -0
- package/src/contract/zod.ts +130 -14
- package/src/devtools.ts +490 -0
- package/src/index.ts +16 -0
- package/src/pool/workerClient.ts +3 -0
- package/src/pool/workerPool.ts +56 -7
- package/src/reactive.ts +107 -42
- package/src/worker/workerBootstrap.ts +3 -0
package/dist/index12.js
CHANGED
|
@@ -1,64 +1,51 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/
|
|
3
|
-
var t = e
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
1
|
+
import { WorkerPool as e } from "./index11.js";
|
|
2
|
+
//#region src/pool/workerClient.ts
|
|
3
|
+
var t = (e, n, r, i) => {
|
|
4
|
+
let a = /* @__PURE__ */ new Map();
|
|
5
|
+
return new Proxy(() => {}, {
|
|
6
|
+
get(o, s) {
|
|
7
|
+
if (s === "then" || typeof s == "symbol") return;
|
|
8
|
+
if (s === "with") return (i) => t(e, n, r, i);
|
|
9
|
+
if (e.length === 0 && r && s in r) return Reflect.get(r, s);
|
|
10
|
+
let c = a.get(s);
|
|
11
|
+
return c || (c = t([...e, s], n, r, i), a.set(s, c)), c;
|
|
12
|
+
},
|
|
13
|
+
apply(t, r, a) {
|
|
14
|
+
let o = n(), s = { taskId: e.join(".") };
|
|
15
|
+
return o.dispatch ? o.dispatch(s, a, i) : o.runTask(s, ...a);
|
|
16
16
|
}
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
runTask(e, ...t) {
|
|
20
|
-
return new Promise((r, i) => {
|
|
21
|
-
let a = Math.random().toString(36).substring(2), o = performance.now();
|
|
22
|
-
n.debug(`→ ${e.taskId}`, { args: t });
|
|
23
|
-
let s = (t) => {
|
|
24
|
-
let c = t.data;
|
|
25
|
-
if (c.messageId === a) {
|
|
26
|
-
this.port.removeEventListener("message", s);
|
|
27
|
-
let t = (performance.now() - o).toFixed(1);
|
|
28
|
-
c.success ? (n.debug(`← ${e.taskId} ok`, { ms: +t }), r(c.result)) : (n.warn(`← ${e.taskId} failed: ${c.error}`, { ms: +t }), i(Error(c.error)));
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
this.port.addEventListener("message", s), this.port.postMessage({
|
|
32
|
-
type: "EXECUTE_TASK",
|
|
33
|
-
messageId: a,
|
|
34
|
-
taskId: e.taskId,
|
|
35
|
-
args: t
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
disconnect() {
|
|
40
|
-
try {
|
|
41
|
-
this.port.postMessage({ type: "SHARED_DISCONNECT" });
|
|
42
|
-
} finally {
|
|
43
|
-
this.port.close();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
17
|
+
});
|
|
46
18
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
n.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
19
|
+
function n(e) {
|
|
20
|
+
let n;
|
|
21
|
+
return t([], typeof e == "function" ? () => n ??= e() : () => e);
|
|
22
|
+
}
|
|
23
|
+
function r(n) {
|
|
24
|
+
let r = null, i = () => r ??= new e({
|
|
25
|
+
sharedMemory: n.sharedMemory,
|
|
26
|
+
createWorker: typeof n.worker == "function" ? n.worker : () => new Worker(n.worker, { type: "module" }),
|
|
27
|
+
poolSize: n.poolSize,
|
|
28
|
+
name: n.name,
|
|
29
|
+
memory: n.memory,
|
|
30
|
+
concurrency: n.concurrency,
|
|
31
|
+
maxQueue: n.maxQueue,
|
|
32
|
+
respawn: n.respawn,
|
|
33
|
+
taskTimeout: n.taskTimeout
|
|
34
|
+
});
|
|
35
|
+
return n.lazy === !1 && i(), t([], i, {
|
|
36
|
+
start: () => {
|
|
37
|
+
i();
|
|
38
|
+
},
|
|
39
|
+
terminate: () => {
|
|
40
|
+
r?.terminate(), r = null;
|
|
41
|
+
},
|
|
42
|
+
get pool() {
|
|
43
|
+
return r;
|
|
44
|
+
},
|
|
45
|
+
get sharedMemory() {
|
|
46
|
+
return n.sharedMemory;
|
|
47
|
+
}
|
|
61
48
|
});
|
|
62
49
|
}
|
|
63
50
|
//#endregion
|
|
64
|
-
export {
|
|
51
|
+
export { r as connectWorker, n as workerClient };
|
package/dist/index13.js
CHANGED
|
@@ -1,25 +1,64 @@
|
|
|
1
|
-
import { scoped as e } from "./
|
|
2
|
-
//#region src/
|
|
3
|
-
var t = e("
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { scoped as e } from "./index6.js";
|
|
2
|
+
//#region src/shared/sharedWorkerClient.ts
|
|
3
|
+
var t = e("shared-client"), n = e("task"), r = class {
|
|
4
|
+
clientIndex;
|
|
5
|
+
sharedMemory;
|
|
6
|
+
port;
|
|
7
|
+
constructor(e, n, r) {
|
|
8
|
+
this.clientIndex = n, this.port = e, this.sharedMemory = r.sharedMemory;
|
|
9
|
+
let i = r.tasks ?? {};
|
|
10
|
+
for (let [e, n] of Object.entries(i)) {
|
|
11
|
+
if (e in this) {
|
|
12
|
+
t.warn(`task "${e}" collides with a SharedWorkerClient member — not installed`);
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
this[e] = (...e) => this.runTask(n, ...e);
|
|
16
|
+
}
|
|
17
|
+
Object.keys(i).length > 0 && t.info(`task methods: ${Object.keys(i).join(", ")}`);
|
|
13
18
|
}
|
|
14
|
-
|
|
15
|
-
return
|
|
19
|
+
runTask(e, ...t) {
|
|
20
|
+
return new Promise((r, i) => {
|
|
21
|
+
let a = Math.random().toString(36).substring(2), o = performance.now();
|
|
22
|
+
n.debug(`→ ${e.taskId}`, { args: t });
|
|
23
|
+
let s = (t) => {
|
|
24
|
+
let c = t.data;
|
|
25
|
+
if (c.messageId === a) {
|
|
26
|
+
this.port.removeEventListener("message", s);
|
|
27
|
+
let t = (performance.now() - o).toFixed(1);
|
|
28
|
+
c.success ? (n.debug(`← ${e.taskId} ok`, { ms: +t }), r(c.result)) : (n.warn(`← ${e.taskId} failed: ${c.error}`, { ms: +t }), i(Error(c.error)));
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
this.port.addEventListener("message", s), this.port.postMessage({
|
|
32
|
+
type: "EXECUTE_TASK",
|
|
33
|
+
messageId: a,
|
|
34
|
+
taskId: e.taskId,
|
|
35
|
+
args: t
|
|
36
|
+
});
|
|
37
|
+
});
|
|
16
38
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
39
|
+
disconnect() {
|
|
40
|
+
try {
|
|
41
|
+
this.port.postMessage({ type: "SHARED_DISCONNECT" });
|
|
42
|
+
} finally {
|
|
43
|
+
this.port.close();
|
|
44
|
+
}
|
|
22
45
|
}
|
|
23
46
|
};
|
|
47
|
+
async function i(e) {
|
|
48
|
+
if (typeof SharedArrayBuffer > "u" || typeof crossOriginIsolated < "u" && !crossOriginIsolated) throw Error("connectSharedWorker requires SharedArrayBuffer in a cross-origin isolated context — serve the page with COOP/COEP headers.");
|
|
49
|
+
if (!e.port && !e.workerUrl && !e.createWorker) throw Error("connectSharedWorker requires `workerUrl`, `createWorker`, or `port` in its config.");
|
|
50
|
+
let n = e.port ?? (e.createWorker ? e.createWorker().port : new SharedWorker(e.workerUrl, { type: "module" }).port);
|
|
51
|
+
return n.start(), await new Promise((i, a) => {
|
|
52
|
+
let o = setTimeout(() => a(/* @__PURE__ */ Error("connectSharedWorker: timed out waiting for the shared worker — is the host module calling sharedWorkerHost()?")), e.connectTimeoutMs ?? 1e4), s = (a) => {
|
|
53
|
+
let c = a.data;
|
|
54
|
+
c?.type === "SHARED_MEMORY" && (n.removeEventListener("message", s), clearTimeout(o), e.sharedMemory.bind(c.buffer), t.info(`connected as client #${c.clientIndex}`, { bytes: c.buffer.byteLength }), i(new r(n, c.clientIndex, e)));
|
|
55
|
+
};
|
|
56
|
+
n.addEventListener("message", s), n.postMessage({
|
|
57
|
+
type: "SHARED_CONNECT",
|
|
58
|
+
memoryBytes: e.sharedMemory.totalBytes,
|
|
59
|
+
memory: e.memory
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
24
63
|
//#endregion
|
|
25
|
-
export {
|
|
64
|
+
export { i as connectSharedWorker };
|
package/dist/index14.js
CHANGED
|
@@ -1,61 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
if (r?.type === "EXECUTE_TASK") {
|
|
26
|
-
let { messageId: n, taskId: s, args: c } = r;
|
|
27
|
-
if (!o) {
|
|
28
|
-
e.postMessage({
|
|
29
|
-
messageId: n,
|
|
30
|
-
success: !1,
|
|
31
|
-
error: "Shared memory not initialized in worker."
|
|
32
|
-
});
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
let l = performance.now();
|
|
36
|
-
try {
|
|
37
|
-
let r = await i.execute(s, ...c || []);
|
|
38
|
-
a.debug(`task ${s} → ${(performance.now() - l).toFixed(1)}ms (client #${t})`), e.postMessage({
|
|
39
|
-
messageId: n,
|
|
40
|
-
success: !0,
|
|
41
|
-
result: r
|
|
42
|
-
});
|
|
43
|
-
} catch (t) {
|
|
44
|
-
a.warn(`task ${s} failed: ${t.message || String(t)}`), e.postMessage({
|
|
45
|
-
messageId: n,
|
|
46
|
-
success: !1,
|
|
47
|
-
error: t.message || String(t)
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
r?.type === "SHARED_DISCONNECT" && (e.close(), a.info(`client #${t} disconnected`));
|
|
53
|
-
}, e.start();
|
|
54
|
-
}
|
|
55
|
-
function u() {
|
|
56
|
-
self.onconnect = (e) => {
|
|
57
|
-
l(e.ports[0]);
|
|
58
|
-
};
|
|
59
|
-
}
|
|
1
|
+
import { scoped as e } from "./index6.js";
|
|
2
|
+
//#region src/worker/registry.ts
|
|
3
|
+
var t = e("registry"), n = class {
|
|
4
|
+
static tasks = /* @__PURE__ */ new Map();
|
|
5
|
+
static register(e, n) {
|
|
6
|
+
this.tasks.set(e.taskId, {
|
|
7
|
+
contract: e,
|
|
8
|
+
handler: n
|
|
9
|
+
}), t.debug(`registered "${e.taskId}"`, {
|
|
10
|
+
args: e.argsSchema ? "validated" : "unvalidated",
|
|
11
|
+
result: e.resultSchema ? "validated" : "unvalidated"
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
static getContract(e) {
|
|
15
|
+
return this.tasks.get(e)?.contract;
|
|
16
|
+
}
|
|
17
|
+
static async execute(e, ...t) {
|
|
18
|
+
let n = this.tasks.get(e);
|
|
19
|
+
if (!n) throw Error(`Task handler not found for id: ${e}`);
|
|
20
|
+
let { contract: r, handler: i } = n, a = await i(...r.argsSchema ? r.argsSchema.parse(t) : t);
|
|
21
|
+
return r.resultSchema ? r.resultSchema.parse(a) : a;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
60
24
|
//#endregion
|
|
61
|
-
export {
|
|
25
|
+
export { n as TaskRegistry };
|
package/dist/index15.js
CHANGED
|
@@ -1,48 +1,61 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { fmtBytes as e, scoped as t } from "./index6.js";
|
|
2
|
+
import { bindSharedMemories as n } from "./index7.js";
|
|
3
|
+
import { MemoryManager as r } from "./index9.js";
|
|
4
|
+
import { TaskRegistry as i } from "./index14.js";
|
|
5
|
+
//#region src/shared/sharedWorkerHost.ts
|
|
6
|
+
var a = t("shared-worker"), o = null, s = 0;
|
|
7
|
+
function c(t, i) {
|
|
8
|
+
if (o) return o.buffer.byteLength < t && a.warn(`client requested ${e(t)} but buffer is ${e(o.buffer.byteLength)} — first client's capacity wins`), o;
|
|
9
|
+
let s = new r(i);
|
|
10
|
+
return s.ensureCapacity(t), o = s.memory, n(s.getBuffer()), a.info(`shared buffer allocated (${e(o.buffer.byteLength)})`), o;
|
|
7
11
|
}
|
|
8
|
-
function
|
|
9
|
-
|
|
12
|
+
function l(e) {
|
|
13
|
+
let t = ++s;
|
|
14
|
+
a.info(`client #${t} connected`), e.onmessage = async (n) => {
|
|
15
|
+
let r = n.data;
|
|
16
|
+
if (r?.type === "SHARED_CONNECT") {
|
|
17
|
+
let n = c(r.memoryBytes ?? 0, r.memory);
|
|
18
|
+
e.postMessage({
|
|
19
|
+
type: "SHARED_MEMORY",
|
|
20
|
+
buffer: n.buffer,
|
|
21
|
+
clientIndex: t
|
|
22
|
+
});
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (r?.type === "EXECUTE_TASK") {
|
|
26
|
+
let { messageId: n, taskId: s, args: c } = r;
|
|
27
|
+
if (!o) {
|
|
28
|
+
e.postMessage({
|
|
29
|
+
messageId: n,
|
|
30
|
+
success: !1,
|
|
31
|
+
error: "Shared memory not initialized in worker."
|
|
32
|
+
});
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
let l = performance.now();
|
|
36
|
+
try {
|
|
37
|
+
let r = await i.execute(s, ...c || []);
|
|
38
|
+
a.debug(`task ${s} → ${(performance.now() - l).toFixed(1)}ms (client #${t})`), e.postMessage({
|
|
39
|
+
messageId: n,
|
|
40
|
+
success: !0,
|
|
41
|
+
result: r
|
|
42
|
+
});
|
|
43
|
+
} catch (t) {
|
|
44
|
+
a.warn(`task ${s} failed: ${t.message || String(t)}`), e.postMessage({
|
|
45
|
+
messageId: n,
|
|
46
|
+
success: !1,
|
|
47
|
+
error: t.message || String(t)
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
r?.type === "SHARED_DISCONNECT" && (e.close(), a.info(`client #${t} disconnected`));
|
|
53
|
+
}, e.start();
|
|
10
54
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
let t = "def" in i ? i.def : i;
|
|
15
|
-
n[r] = {
|
|
16
|
-
taskId: t.taskId ?? `${e}.${r}`,
|
|
17
|
-
...t.dataType ? { dataType: t.dataType } : {},
|
|
18
|
-
...t.argsSchema ? { argsSchema: t.argsSchema } : {},
|
|
19
|
-
...t.resultSchema ? { resultSchema: t.resultSchema } : {}
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
return {
|
|
23
|
-
name: e,
|
|
24
|
-
tasks: n
|
|
55
|
+
function u() {
|
|
56
|
+
self.onconnect = (e) => {
|
|
57
|
+
l(e.ports[0]);
|
|
25
58
|
};
|
|
26
59
|
}
|
|
27
|
-
function o(e, r) {
|
|
28
|
-
let i = Object.keys(e.tasks).filter((e) => {
|
|
29
|
-
let t = r[e];
|
|
30
|
-
return typeof t != "function" && typeof t?.run != "function";
|
|
31
|
-
});
|
|
32
|
-
if (i.length > 0) throw Error(`implementService("${e.name}"): missing handlers for ${i.join(", ")}`);
|
|
33
|
-
for (let [n, i] of Object.entries(e.tasks)) {
|
|
34
|
-
let e = r[n];
|
|
35
|
-
t.register(i, typeof e == "function" ? e.bind(r) : e.run.bind(e));
|
|
36
|
-
}
|
|
37
|
-
return n.info(`service "${e.name}" implemented — ${Object.keys(e.tasks).join(", ")}`), {
|
|
38
|
-
service: e,
|
|
39
|
-
handlers: r
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
function s(e, t) {
|
|
43
|
-
let n = {};
|
|
44
|
-
for (let [r, i] of Object.entries(e.tasks)) n[r] = (...e) => t.runTask(i, ...e);
|
|
45
|
-
return n;
|
|
46
|
-
}
|
|
47
60
|
//#endregion
|
|
48
|
-
export {
|
|
61
|
+
export { l as attachSharedPort, u as sharedWorkerHost };
|
package/dist/index16.js
CHANGED
|
@@ -1,54 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
n(t.buffer), o = !0, a.info(`shared memory initialized (${e(t.buffer.byteLength)})`);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (s.type === "EXECUTE_TASK") {
|
|
24
|
-
let { messageId: e, taskId: t, args: n } = s;
|
|
25
|
-
if (!o) {
|
|
26
|
-
a.warn(`rejected ${t} — shared memory not initialized`), self.postMessage({
|
|
27
|
-
messageId: e,
|
|
28
|
-
success: !1,
|
|
29
|
-
error: "Shared memory not initialized in worker."
|
|
30
|
-
});
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
let r = performance.now();
|
|
34
|
-
try {
|
|
35
|
-
let o = await i.execute(t, ...n || []);
|
|
36
|
-
a.debug(`task ${t} → ${(performance.now() - r).toFixed(1)}ms`), self.postMessage({
|
|
37
|
-
messageId: e,
|
|
38
|
-
success: !0,
|
|
39
|
-
result: o
|
|
40
|
-
});
|
|
41
|
-
} catch (n) {
|
|
42
|
-
a.warn(`task ${t} failed: ${n.message || String(n)}`), self.postMessage({
|
|
43
|
-
messageId: e,
|
|
44
|
-
success: !1,
|
|
45
|
-
error: n.message || String(n)
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
}
|
|
1
|
+
import { scoped as e } from "./index6.js";
|
|
2
|
+
import { TaskRegistry as t } from "./index14.js";
|
|
3
|
+
//#region src/service.ts
|
|
4
|
+
var n = e("service");
|
|
5
|
+
function r(e = {}) {
|
|
6
|
+
return e;
|
|
7
|
+
}
|
|
8
|
+
function i(e) {
|
|
9
|
+
return e;
|
|
10
|
+
}
|
|
11
|
+
function a(e, t) {
|
|
12
|
+
let n = {};
|
|
13
|
+
for (let [r, i] of Object.entries(t)) {
|
|
14
|
+
let t = "def" in i ? i.def : i;
|
|
15
|
+
n[r] = {
|
|
16
|
+
taskId: t.taskId ?? `${e}.${r}`,
|
|
17
|
+
...t.dataType ? { dataType: t.dataType } : {},
|
|
18
|
+
...t.argsSchema ? { argsSchema: t.argsSchema } : {},
|
|
19
|
+
...t.resultSchema ? { resultSchema: t.resultSchema } : {}
|
|
49
20
|
};
|
|
50
21
|
}
|
|
22
|
+
return {
|
|
23
|
+
name: e,
|
|
24
|
+
tasks: n
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function o(e, r) {
|
|
28
|
+
let i = Object.keys(e.tasks).filter((e) => {
|
|
29
|
+
let t = r[e];
|
|
30
|
+
return typeof t != "function" && typeof t?.run != "function";
|
|
31
|
+
});
|
|
32
|
+
if (i.length > 0) throw Error(`implementService("${e.name}"): missing handlers for ${i.join(", ")}`);
|
|
33
|
+
for (let [n, i] of Object.entries(e.tasks)) {
|
|
34
|
+
let e = r[n];
|
|
35
|
+
t.register(i, typeof e == "function" ? e.bind(r) : e.run.bind(e));
|
|
36
|
+
}
|
|
37
|
+
return n.info(`service "${e.name}" implemented — ${Object.keys(e.tasks).join(", ")}`), {
|
|
38
|
+
service: e,
|
|
39
|
+
handlers: r
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function s(e, t) {
|
|
43
|
+
let n = {};
|
|
44
|
+
for (let [r, i] of Object.entries(e.tasks)) n[r] = (...e) => t.runTask(i, ...e);
|
|
45
|
+
return n;
|
|
51
46
|
}
|
|
52
|
-
c();
|
|
53
47
|
//#endregion
|
|
54
|
-
export {
|
|
48
|
+
export { s as createClient, a as defineService, o as implementService, r as rpc, i as serviceMethod };
|
package/dist/index17.js
CHANGED
|
@@ -1,41 +1,55 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
-
|
|
1
|
+
import { enableWorkerDevtoolsForwarding as e } from "./index5.js";
|
|
2
|
+
import { fmtBytes as t, scoped as n } from "./index6.js";
|
|
3
|
+
import { bindSharedMemories as r, getDefinedSharedMemoryCount as i } from "./index7.js";
|
|
4
|
+
import { TaskRegistry as a } from "./index14.js";
|
|
5
|
+
//#region src/worker/workerBootstrap.ts
|
|
6
|
+
var o = n("worker"), s = !1, c = !1;
|
|
7
|
+
function l() {
|
|
8
|
+
if (!c) {
|
|
9
|
+
if (typeof self > "u") {
|
|
10
|
+
o.warn("workerBootstrap: no worker global found — skipping message wiring");
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
c = !0, self.onmessage = async (n) => {
|
|
14
|
+
let c = n.data;
|
|
15
|
+
if (c.type === "INIT") {
|
|
16
|
+
s = !0, c.devtools && e(), i() > 0 && o.warn("pool sent INIT without memory, but shared-memory contracts are defined — field access will throw until bound"), o.info("initialized (message-only pool — no shared memory)");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (c.type === "INIT_MEMORY") {
|
|
20
|
+
let n = c.memory;
|
|
21
|
+
r(n.buffer), s = !0, c.devtools && e(), o.info(`shared memory initialized (${t(n.buffer.byteLength)})`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (c.type === "EXECUTE_TASK") {
|
|
25
|
+
let { messageId: e, taskId: t, args: n } = c;
|
|
26
|
+
if (!s) {
|
|
27
|
+
o.warn(`rejected ${t} — shared memory not initialized`), self.postMessage({
|
|
28
|
+
messageId: e,
|
|
29
|
+
success: !1,
|
|
30
|
+
error: "Shared memory not initialized in worker."
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
let r = performance.now();
|
|
35
|
+
try {
|
|
36
|
+
let i = await a.execute(t, ...n || []);
|
|
37
|
+
o.debug(`task ${t} → ${(performance.now() - r).toFixed(1)}ms`), self.postMessage({
|
|
38
|
+
messageId: e,
|
|
39
|
+
success: !0,
|
|
40
|
+
result: i
|
|
41
|
+
});
|
|
42
|
+
} catch (n) {
|
|
43
|
+
o.warn(`task ${t} failed: ${n.message || String(n)}`), self.postMessage({
|
|
44
|
+
messageId: e,
|
|
45
|
+
success: !1,
|
|
46
|
+
error: n.message || String(n)
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
32
51
|
}
|
|
33
|
-
r.info(`worker defined — ${c} method(s)`);
|
|
34
|
-
let u = {
|
|
35
|
-
methods: i,
|
|
36
|
-
services: s
|
|
37
|
-
};
|
|
38
|
-
return e.sharedMemory && (u.sharedMemory = e.sharedMemory), u;
|
|
39
52
|
}
|
|
53
|
+
l();
|
|
40
54
|
//#endregion
|
|
41
|
-
export {
|
|
55
|
+
export { l as installWorkerListener };
|