defuss-multicore 0.0.3 → 0.1.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.
@@ -1,153 +0,0 @@
1
- 'use strict';
2
-
3
- var index = require('./index.cjs');
4
-
5
- const DEFAULT_IDLE_TIMEOUT_MS = 3e4;
6
- const createPool = (script, config, hooks) => {
7
- const maxWorkers = config.maxWorkers ?? index.getPoolSize();
8
- const idleTimeoutMs = config.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
9
- const workers = [];
10
- const queue = [];
11
- let nextId = 0;
12
- let taskCounter = 0;
13
- const pending = /* @__PURE__ */ new Map();
14
- const onMessage = (handle, data) => {
15
- const p = pending.get(data.id);
16
- if (!p) return;
17
- pending.delete(data.id);
18
- handle.busy = false;
19
- resetIdleTimer(handle);
20
- drainQueue();
21
- if (data.type === "result") {
22
- p.resolve(data.value);
23
- } else {
24
- const err = new Error(data.error);
25
- if (data.stack) err.stack = data.stack;
26
- p.reject(err);
27
- }
28
- };
29
- const onError = (handle, error) => {
30
- for (const [id, p] of pending) {
31
- p.reject(error instanceof Error ? error : new Error(String(error)));
32
- pending.delete(id);
33
- }
34
- handle.busy = false;
35
- handle.terminated = true;
36
- };
37
- const spawnWorker = () => {
38
- const id = nextId++;
39
- const raw = hooks.createWorker(script, id);
40
- const handle = { id, busy: false, terminated: false, raw };
41
- hooks.onMessage(raw, (data) => onMessage(handle, data));
42
- hooks.onError(raw, (err) => onError(handle, err));
43
- workers.push(handle);
44
- resetIdleTimer(handle);
45
- return handle;
46
- };
47
- const resetIdleTimer = (handle) => {
48
- if (handle.idleTimer) clearTimeout(handle.idleTimer);
49
- handle.idleTimer = setTimeout(() => {
50
- if (!handle.busy && !handle.terminated) {
51
- handle.terminated = true;
52
- hooks.terminateWorker(handle.raw);
53
- const idx = workers.indexOf(handle);
54
- if (idx !== -1) workers.splice(idx, 1);
55
- }
56
- }, idleTimeoutMs);
57
- };
58
- const getIdleWorker = () => workers.find((w) => !w.busy && !w.terminated);
59
- const drainQueue = () => {
60
- while (queue.length > 0) {
61
- const idle = getIdleWorker();
62
- if (!idle) {
63
- if (workers.length < maxWorkers) {
64
- const fresh = spawnWorker();
65
- const task2 = queue.shift();
66
- dispatch(fresh, task2);
67
- }
68
- break;
69
- }
70
- const task = queue.shift();
71
- dispatch(idle, task);
72
- }
73
- };
74
- const dispatch = (handle, task) => {
75
- const id = `t${taskCounter++}`;
76
- handle.busy = true;
77
- if (handle.idleTimer) clearTimeout(handle.idleTimer);
78
- pending.set(id, { resolve: task.resolve, reject: task.reject });
79
- if (task.signal?.aborted) {
80
- pending.delete(id);
81
- handle.busy = false;
82
- resetIdleTimer(handle);
83
- task.reject(new DOMException("The operation was aborted.", "AbortError"));
84
- return;
85
- }
86
- if (task.signal) {
87
- const onAbort = () => {
88
- const p = pending.get(id);
89
- if (p) {
90
- pending.delete(id);
91
- handle.terminated = true;
92
- hooks.terminateWorker(handle.raw);
93
- if (handle.idleTimer) clearTimeout(handle.idleTimer);
94
- const idx = workers.indexOf(handle);
95
- if (idx !== -1) workers.splice(idx, 1);
96
- p.reject(new DOMException("The operation was aborted.", "AbortError"));
97
- }
98
- };
99
- task.signal.addEventListener("abort", onAbort, { once: true });
100
- }
101
- hooks.postMessage(
102
- handle.raw,
103
- { type: "execute", id, args: task.args },
104
- task.transfer ?? []
105
- );
106
- };
107
- const pool = {
108
- get size() {
109
- return maxWorkers;
110
- },
111
- execute(args, transfer, signal) {
112
- return new Promise((resolve, reject) => {
113
- const task = { args, transfer, signal, resolve, reject };
114
- const idle = getIdleWorker();
115
- if (idle) {
116
- dispatch(idle, task);
117
- } else if (workers.length < maxWorkers) {
118
- const fresh = spawnWorker();
119
- dispatch(fresh, task);
120
- } else {
121
- queue.push(task);
122
- }
123
- });
124
- },
125
- warmup(count) {
126
- const target = Math.min(count ?? maxWorkers, maxWorkers);
127
- while (workers.length < target) {
128
- spawnWorker();
129
- }
130
- },
131
- async terminate() {
132
- for (const w of workers) {
133
- if (!w.terminated) {
134
- w.terminated = true;
135
- if (w.idleTimer) clearTimeout(w.idleTimer);
136
- hooks.terminateWorker(w.raw);
137
- }
138
- }
139
- workers.length = 0;
140
- for (const task of queue) {
141
- task.reject(new Error("Pool terminated"));
142
- }
143
- queue.length = 0;
144
- for (const [, p] of pending) {
145
- p.reject(new Error("Pool terminated"));
146
- }
147
- pending.clear();
148
- }
149
- };
150
- return pool;
151
- };
152
-
153
- exports.createPool = createPool;
@@ -1,43 +0,0 @@
1
- 'use strict';
2
-
3
- var pool = require('./pool-DqNSYahJ.cjs');
4
- require('./index.cjs');
5
-
6
- const createBrowserPool = (script, config) => {
7
- const blobUrls = /* @__PURE__ */ new Set();
8
- const hooks = {
9
- createWorker(script2) {
10
- const blob = new Blob([script2], { type: "text/javascript" });
11
- const url = URL.createObjectURL(blob);
12
- blobUrls.add(url);
13
- return new Worker(url);
14
- },
15
- postMessage(worker, data, transfer) {
16
- worker.postMessage(data, transfer);
17
- },
18
- terminateWorker(worker) {
19
- worker.terminate();
20
- },
21
- onMessage(worker, handler) {
22
- worker.onmessage = (e) => handler(e.data);
23
- },
24
- onError(worker, handler) {
25
- worker.onerror = (e) => {
26
- e.preventDefault();
27
- handler(new Error(e.message));
28
- };
29
- }
30
- };
31
- const pool$1 = pool.createPool(script, config ?? {}, hooks);
32
- const originalTerminate = pool$1.terminate.bind(pool$1);
33
- pool$1.terminate = async () => {
34
- await originalTerminate();
35
- for (const url of blobUrls) {
36
- URL.revokeObjectURL(url);
37
- }
38
- blobUrls.clear();
39
- };
40
- return pool$1;
41
- };
42
-
43
- exports.createBrowserPool = createBrowserPool;
@@ -1,41 +0,0 @@
1
- import { c as createPool } from './pool-DhG3ybwo.mjs';
2
- import './index.mjs';
3
-
4
- const createBrowserPool = (script, config) => {
5
- const blobUrls = /* @__PURE__ */ new Set();
6
- const hooks = {
7
- createWorker(script2) {
8
- const blob = new Blob([script2], { type: "text/javascript" });
9
- const url = URL.createObjectURL(blob);
10
- blobUrls.add(url);
11
- return new Worker(url);
12
- },
13
- postMessage(worker, data, transfer) {
14
- worker.postMessage(data, transfer);
15
- },
16
- terminateWorker(worker) {
17
- worker.terminate();
18
- },
19
- onMessage(worker, handler) {
20
- worker.onmessage = (e) => handler(e.data);
21
- },
22
- onError(worker, handler) {
23
- worker.onerror = (e) => {
24
- e.preventDefault();
25
- handler(new Error(e.message));
26
- };
27
- }
28
- };
29
- const pool = createPool(script, config ?? {}, hooks);
30
- const originalTerminate = pool.terminate.bind(pool);
31
- pool.terminate = async () => {
32
- await originalTerminate();
33
- for (const url of blobUrls) {
34
- URL.revokeObjectURL(url);
35
- }
36
- blobUrls.clear();
37
- };
38
- return pool;
39
- };
40
-
41
- export { createBrowserPool };