1.0.1 • Published 1 year ago
nv-facutil-duplex-worker-bw v1.0.1
nv-facutil-duplex-worker-bw
- simple worker
- this pkg can be used in both nodejs and browser
install
- npm install nv-facutil-duplex-worker-bw
usage
# in main
const creat = require("nv-facutil-duplex-worker-bw").main;
ReqFunc: async(data:Any)->Any
var worker = await creat(worker_script_path:String, handle: async(data:Any, req_func:ReqFunc)=>Any)
await worker.req(data)
# in worker
const creat = require("nv-facutil-duplex-worker-bw").worker;
creat(handle: async(data:Any, req_func:ReqFunc)=>Any)
example
worker
# w.js
const creat = require("nv-facutil-duplex-worker-bw").creat;
var handle = async (recved_data_from_main, send_to_main_before_return) => {
let data = recved_data_from_main;
if(data >0) {
return(data + 1)
} else {
let wait_from_confirm = await send_to_main_before_return("are you ok?");
if(wait_from_confirm) {
return(data - 1)
} else {
return(0)
}
}
}
var task = setInterval(
async ()=> {
let can_i_go_home = await creat.req("can i go home?");
if(can_i_go_home) {
clearInterval(task);
process.exit()
} else {
console.log("~~~~~~~~~~~~~~~~~~")
}
},
5000
);
creat(handle);
main
# main.js
const creat = require("nv-facutil-duplex-worker-bw").main;
var handle = async (recved_data_from_worker, send_to_worker_before_rtrn) => {
let data = recved_data_from_worker;
if(data === "are you ok?") {
return(true)
} else if(data === "can i go home?"){
return(false)
} else {
return("ok")
}
}
var w0 = await creat("./w.js", handle)
var task = setInterval(
async ()=> {
let rslt = await w0.req(Math.random() - 0.5);
console.log(rslt)
},
5000
);
METHODS
APIS
LICENSE
- ISC
1.0.1
1 year ago