nv-facutil-duplex-worker-nd
- simple worker
- same as nv-facutil-duplex-worker-bw, BUT this pkg can ONLY work IN nodejs
- slightly faster than nv-facutil-duplex-worker-bw
- npm install nv-facutil-duplex-worker-nd
# in main
const creat = require("nv-facutil-duplex-worker-nd").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-nd").worker;
creat(handle: async(data:Any, req_func:ReqFunc)=>Any)
# w.js
const creat = require("nv-facutil-duplex-worker-nd").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.js
const creat = require("nv-facutil-duplex-worker-nd").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
);