Comparing version 0.1.1 to 0.1.2
@@ -19,4 +19,3 @@ var wk = function (c, msg, transfer, cb) { | ||
var ts = f.toString(); | ||
var spInd = ts.indexOf(' ', 8) + 1; | ||
return ts.slice(spInd, ts.indexOf('(', spInd)); | ||
return ts.slice(0, 9) == 'function ' && ts.slice(9, ts.indexOf('(', 9)); | ||
}; | ||
@@ -66,3 +65,3 @@ var abvList = [ | ||
if (nm in reg) | ||
return "self[" + encoder.string(fnName(v)) + "]"; | ||
return "self[" + encoder.string(nm) + "]"; | ||
reg[nm] = true; | ||
@@ -221,9 +220,10 @@ } | ||
var currentCb; | ||
var runCnt = 0; | ||
var lifetimeCb = function (err, res) { | ||
--runCnt; | ||
var runCount = 0; | ||
var callCount = 0; | ||
var worker = wk(str + ";onmessage=function(e){for(var k in e.data){self[k]=e.data[k]}var h=" + encoder["function"](fn, reg, tfl) + ";var _p=function(d){d?typeof d.then=='function'?d.then(_p):postMessage(d,d.__transferList):postMessage(d)};onmessage=function(e){_p(h.apply(self,e.data))}}", msg, tfl, function (err, res) { | ||
++runCount; | ||
currentCb(err, res); | ||
}; | ||
var worker = wk(str + ";onmessage=function(e){for(var k in e.data){self[k]=e.data[k]}var h=" + encoder["function"](fn, reg, tfl) + ";var _p=function(d){d?typeof d.then=='function'?d.then(_p):d.__transfer?postMessage(d.data,d.__transfer):postMessage(d):postMessage(d)};onmessage=function(e){_p(h.apply(self,e.data))}}", msg, tfl, lifetimeCb); | ||
return function () { | ||
}); | ||
var closed = false; | ||
var wfn = function () { | ||
var args = []; | ||
@@ -236,6 +236,10 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
throw new TypeError('no callback provided'); | ||
if (closed) { | ||
cb(new Error('worker thread closed'), null); | ||
return; | ||
} | ||
var lastCb = currentCb; | ||
var startCnt = runCnt++; | ||
var startCount = ++callCount; | ||
currentCb = function (err, r) { | ||
if (runCnt == startCnt) | ||
if (runCount == startCount) | ||
cb(err, r); | ||
@@ -247,2 +251,7 @@ else | ||
}; | ||
wfn.close = function () { | ||
worker.terminate(); | ||
closed = true; | ||
}; | ||
return wfn; | ||
} |
@@ -22,4 +22,3 @@ "use strict"; | ||
var ts = f.toString(); | ||
var spInd = ts.indexOf(' ', 8) + 1; | ||
return ts.slice(spInd, ts.indexOf('(', spInd)); | ||
return ts.slice(0, 9) == 'function ' && ts.slice(9, ts.indexOf('(', 9)); | ||
}; | ||
@@ -69,3 +68,3 @@ var abvList = [ | ||
if (nm in reg) | ||
return "self[" + encoder.string(fnName(v)) + "]"; | ||
return "self[" + encoder.string(nm) + "]"; | ||
reg[nm] = true; | ||
@@ -225,9 +224,10 @@ } | ||
var currentCb; | ||
var runCnt = 0; | ||
var lifetimeCb = function (err, res) { | ||
--runCnt; | ||
var runCount = 0; | ||
var callCount = 0; | ||
var worker = node_worker_1["default"](str + ";onmessage=function(e){for(var k in e.data){self[k]=e.data[k]}var h=" + encoder["function"](fn, reg, tfl) + ";var _p=function(d){d?typeof d.then=='function'?d.then(_p):postMessage(d,d.__transferList):postMessage(d)};onmessage=function(e){_p(h.apply(self,e.data))}}", msg, tfl, function (err, res) { | ||
++runCount; | ||
currentCb(err, res); | ||
}; | ||
var worker = node_worker_1["default"](str + ";onmessage=function(e){for(var k in e.data){self[k]=e.data[k]}var h=" + encoder["function"](fn, reg, tfl) + ";var _p=function(d){d?typeof d.then=='function'?d.then(_p):d.__transfer?postMessage(d.data,d.__transfer):postMessage(d):postMessage(d)};onmessage=function(e){_p(h.apply(self,e.data))}}", msg, tfl, lifetimeCb); | ||
return function () { | ||
}); | ||
var closed = false; | ||
var wfn = function () { | ||
var args = []; | ||
@@ -240,6 +240,10 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
throw new TypeError('no callback provided'); | ||
if (closed) { | ||
cb(new Error('worker thread closed'), null); | ||
return; | ||
} | ||
var lastCb = currentCb; | ||
var startCnt = runCnt++; | ||
var startCount = ++callCount; | ||
currentCb = function (err, r) { | ||
if (runCnt == startCnt) | ||
if (runCount == startCount) | ||
cb(err, r); | ||
@@ -251,3 +255,8 @@ else | ||
}; | ||
wfn.close = function () { | ||
worker.terminate(); | ||
closed = true; | ||
}; | ||
return wfn; | ||
} | ||
exports.workerize = workerize; |
@@ -18,2 +18,12 @@ import { WorkerTransfer } from './node-worker'; | ||
/** | ||
* A workerized function (from arguments and return type) | ||
*/ | ||
export declare type Workerized<A extends unknown[], R> = ((...args: [...A, (err: Error, res: R) => unknown]) => void) & { | ||
/** | ||
* Kills the worker associated with this workerized function. | ||
* Subsequent calls will fail. | ||
*/ | ||
close(): void; | ||
}; | ||
/** | ||
* Converts a function with dependencies into a worker | ||
@@ -28,2 +38,2 @@ * @param fn The function to workerize | ||
*/ | ||
export declare function workerize<TA extends unknown[], TR>(fn: (...args: TA) => TR, deps: DepList): (...args: [...TA, (err: Error, res: TR) => unknown]) => void; | ||
export declare function workerize<TA extends unknown[], TR>(fn: (...args: TA) => TR, deps: DepList): Workerized<TA, TR>; |
@@ -8,4 +8,3 @@ "use strict"; | ||
var ts = f.toString(); | ||
var spInd = ts.indexOf(' ', 8) + 1; | ||
return ts.slice(spInd, ts.indexOf('(', spInd)); | ||
return ts.slice(0, 9) == 'function ' && ts.slice(9, ts.indexOf('(', 9)); | ||
}; | ||
@@ -55,3 +54,3 @@ var abvList = [ | ||
if (nm in reg) | ||
return "self[" + encoder.string(fnName(v)) + "]"; | ||
return "self[" + encoder.string(nm) + "]"; | ||
reg[nm] = true; | ||
@@ -211,9 +210,10 @@ } | ||
var currentCb; | ||
var runCnt = 0; | ||
var lifetimeCb = function (err, res) { | ||
--runCnt; | ||
var runCount = 0; | ||
var callCount = 0; | ||
var worker = node_worker_1["default"](str + ";onmessage=function(e){for(var k in e.data){self[k]=e.data[k]}var h=" + encoder["function"](fn, reg, tfl) + ";var _p=function(d){d?typeof d.then=='function'?d.then(_p):postMessage(d,d.__transferList):postMessage(d)};onmessage=function(e){_p(h.apply(self,e.data))}}", msg, tfl, function (err, res) { | ||
++runCount; | ||
currentCb(err, res); | ||
}; | ||
var worker = node_worker_1["default"](str + ";onmessage=function(e){for(var k in e.data){self[k]=e.data[k]}var h=" + encoder["function"](fn, reg, tfl) + ";var _p=function(d){d?typeof d.then=='function'?d.then(_p):d.__transfer?postMessage(d.data,d.__transfer):postMessage(d):postMessage(d)};onmessage=function(e){_p(h.apply(self,e.data))}}", msg, tfl, lifetimeCb); | ||
return function () { | ||
}); | ||
var closed = false; | ||
var wfn = function () { | ||
var args = []; | ||
@@ -226,6 +226,10 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
throw new TypeError('no callback provided'); | ||
if (closed) { | ||
cb(new Error('worker thread closed'), null); | ||
return; | ||
} | ||
var lastCb = currentCb; | ||
var startCnt = runCnt++; | ||
var startCount = ++callCount; | ||
currentCb = function (err, r) { | ||
if (runCnt == startCnt) | ||
if (runCount == startCount) | ||
cb(err, r); | ||
@@ -237,3 +241,8 @@ else | ||
}; | ||
wfn.close = function () { | ||
worker.terminate(); | ||
closed = true; | ||
}; | ||
return wfn; | ||
} | ||
exports.workerize = workerize; |
@@ -23,4 +23,3 @@ "use strict"; | ||
var ts = f.toString(); | ||
var spInd = ts.indexOf(' ', 8) + 1; | ||
return ts.slice(spInd, ts.indexOf('(', spInd)); | ||
return ts.slice(0, 9) == 'function ' && ts.slice(9, ts.indexOf('(', 9)); | ||
}; | ||
@@ -70,3 +69,3 @@ var abvList = [ | ||
if (nm in reg) | ||
return "self[" + encoder.string(fnName(v)) + "]"; | ||
return "self[" + encoder.string(nm) + "]"; | ||
reg[nm] = true; | ||
@@ -226,9 +225,10 @@ } | ||
var currentCb; | ||
var runCnt = 0; | ||
var lifetimeCb = function (err, res) { | ||
--runCnt; | ||
var runCount = 0; | ||
var callCount = 0; | ||
var worker = node_worker_1["default"](str + ";onmessage=function(e){for(var k in e.data){self[k]=e.data[k]}var h=" + encoder["function"](fn, reg, tfl) + ";var _p=function(d){d?typeof d.then=='function'?d.then(_p):postMessage(d,d.__transferList):postMessage(d)};onmessage=function(e){_p(h.apply(self,e.data))}}", msg, tfl, function (err, res) { | ||
++runCount; | ||
currentCb(err, res); | ||
}; | ||
var worker = node_worker_1["default"](str + ";onmessage=function(e){for(var k in e.data){self[k]=e.data[k]}var h=" + encoder["function"](fn, reg, tfl) + ";var _p=function(d){d?typeof d.then=='function'?d.then(_p):d.__transfer?postMessage(d.data,d.__transfer):postMessage(d):postMessage(d)};onmessage=function(e){_p(h.apply(self,e.data))}}", msg, tfl, lifetimeCb); | ||
return function () { | ||
}); | ||
var closed = false; | ||
var wfn = function () { | ||
var args = []; | ||
@@ -241,6 +241,10 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
throw new TypeError('no callback provided'); | ||
if (closed) { | ||
cb(new Error('worker thread closed'), null); | ||
return; | ||
} | ||
var lastCb = currentCb; | ||
var startCnt = runCnt++; | ||
var startCount = ++callCount; | ||
currentCb = function (err, r) { | ||
if (runCnt == startCnt) | ||
if (runCount == startCount) | ||
cb(err, r); | ||
@@ -252,3 +256,8 @@ else | ||
}; | ||
wfn.close = function () { | ||
worker.terminate(); | ||
closed = true; | ||
}; | ||
return wfn; | ||
} | ||
exports.workerize = workerize; |
{ | ||
"name": "isoworker", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Isomorphic workerization with dependencies", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -64,14 +64,15 @@ # isoworker | ||
// got 0 from worker | ||
asyncCount(() => {}); | ||
asyncCount((err, res) => { | ||
asyncCount.close(); | ||
}); | ||
// 1 | ||
// got 1 from worker | ||
// Since that was run on another thread, the main thread's value hasn't | ||
// been mutated | ||
console.log(count); // 0 | ||
console.log(number); // 0 | ||
``` | ||
If you want to run setup code, you can use a flag. | ||
If you want to run setup code, you can use a condition within the function and/or a flag. | ||
```js | ||
let wasm; | ||
const wasm = {}; | ||
@@ -81,8 +82,8 @@ // generic WASM runner | ||
const runWasmSync = async (wasmName, method, ...args) => { | ||
if (!wasm) { | ||
wasm = (await WebAssembly.instantiateStreaming( | ||
if (!wasm[wasmName]) { | ||
wasm[wasmName] = (await WebAssembly.instantiateStreaming( | ||
fetch(`/wasm-files/${wasmName}.wasm`) | ||
)).module.exports; | ||
} | ||
return wasm[method](...args); | ||
return wasm[wasmName][method](...args); | ||
} | ||
@@ -89,0 +90,0 @@ const runWasm = workerize(runWasmSync, () => [wasm]); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55750
1334
101