Comparing version 0.0.1 to 0.0.2
20
call.js
import serialize from "./serialize.js"; | ||
export default function call(it, method, params) { | ||
export default function call(it, method, params, { debug_level = 0 } = {}) { | ||
if (!params) params = []; | ||
let [params_serialized, params_functions] = serialize( | ||
params, | ||
"microlink.call:" | ||
); | ||
let [params_serialized, params_functions] = serialize(params, "microlink.call:"); | ||
if (debug_level >= 2) { | ||
console.log("[Microlink.call] serialized to ", [params_serialized, params_functions]); | ||
} | ||
@@ -19,3 +19,5 @@ return new Promise(resolve => { | ||
const result = await params_functions[data.method](...data.params); | ||
it.postMessage({ jsonrpc: "2.0", result, id: data.id }); | ||
const msg = { jsonrpc: "2.0", result, id: data.id }; | ||
if (debug_level >= 2) console.log("[Microlink.call] top thread posting message", msg); | ||
return it.postMessage(msg); | ||
} | ||
@@ -30,8 +32,10 @@ | ||
resolve(data.result); | ||
return resolve(data.result); | ||
} | ||
}); | ||
it.postMessage({ jsonrpc: "2.0", id, method, params: params_serialized }); | ||
const msg = { jsonrpc: "2.0", id, method, params: params_serialized }; | ||
if (debug_level >= 2) console.log("[Microlink.call] top thread posting message", msg); | ||
return it.postMessage(msg); | ||
}); | ||
} |
import deserialize from "./deserialize.js"; | ||
export default function expose(obj) { | ||
export default function expose(obj, options) { | ||
const debug_level = options && options.debug_level; | ||
addEventListener("message", async evt => { | ||
const { data } = evt; | ||
if (debug_level >= 2) console.log("[Microlink.expose] received message data", data); | ||
@@ -16,2 +19,3 @@ if (typeof data !== "object") return; | ||
if (method === "microlink.list") { | ||
if (debug_level >= 2) console.log("[Microlink.expose] posting method names", data); | ||
return postMessage({ | ||
@@ -25,2 +29,3 @@ jsonrpc: "2.0", | ||
if (typeof obj[method] !== "function") { | ||
if (debug_level >= 2) console.error("[Microlink.expose] method not found: " + method); | ||
return postMessage({ | ||
@@ -39,2 +44,3 @@ jsonrpc: "2.0", | ||
const result = await obj[method](...deserialized_params); | ||
if (debug_level >= 2) console.log("[Microlink.expose] posting result for " + method + ": " + JSON.stringify(result)); | ||
return postMessage({ | ||
@@ -46,3 +52,3 @@ jsonrpc: "2.0", | ||
} catch (error) { | ||
console.error(error); | ||
if (debug_level >= 2) console.error("[Microlink.expose] error:", error); | ||
return postMessage({ | ||
@@ -49,0 +55,0 @@ jsonrpc: "2.0", |
{ | ||
"name": "microlink", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Comlink Alternative. Built with JSON-RPC.", | ||
@@ -16,3 +16,3 @@ "main": "index.js", | ||
"scripts": { | ||
"format": "npx prettier --arrow-parens=avoid --trailing-comma=none --write *.js", | ||
"format": "npx prettier --arrow-parens=avoid --print-width=160 --trailing-comma=none --write *.js", | ||
"serve": "npx srvd --debug", | ||
@@ -19,0 +19,0 @@ "test": "node test.js" |
@@ -33,2 +33,12 @@ # microlink | ||
await obj.run(count_elements, 'div'); | ||
``` | ||
``` | ||
## advanced usage | ||
#### debugging | ||
```js | ||
// pass an options object to expose or wrap | ||
const options = { debug_level: 10 }; | ||
expose(methods, options); | ||
wrap(worker, options) | ||
``` |
@@ -15,5 +15,3 @@ /** | ||
} else if (typeof it === "object") { | ||
return Object.fromEntries( | ||
Object.entries(it).map(([k, v]) => [k, stringify(v)]) | ||
); | ||
return Object.fromEntries(Object.entries(it).map(([k, v]) => [k, stringify(v)])); | ||
} else if (typeof it === "function") { | ||
@@ -20,0 +18,0 @@ const fid = Math.random(); |
12
wrap.js
import call from "./call.js"; | ||
export default async function wrap(worker) { | ||
export default async function wrap(worker, { debug_level = 0 } = {}) { | ||
const obj = {}; | ||
const methods = await call(worker, "microlink.list"); | ||
const methods = await call(worker, "microlink.list", undefined, { | ||
debug_level | ||
}); | ||
if (debug_level >= 2) console.log("[Microlink.wrap] methods:", methods); | ||
methods.forEach(method => { | ||
obj[method] = (...params) => call(worker, method, params); | ||
obj[method] = (...params) => { | ||
if (debug_level >= 2) console.log("[Microlink.wrap] called worker." + method); | ||
return call(worker, method, params, { debug_level }); | ||
}; | ||
}); | ||
@@ -11,0 +17,0 @@ |
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
13824
145
44