Napa-Loader
Install
npm install --save napa-loader
Usage
see test
1. Create a module
// test-module.js
let state = 'init'
function changeState(_state) { state = _state }
function getState() { return state }
function fib(n) {
if (n === 0) return 0;
else if (n === 1) return 1;
else return fib(n - 1) + fib(n - 2)
}
module.exports = { fib, changeState, getState }
zrequire(<module_path>)
2. Load module by // Create a zone and init by load test-module.js
const test_module = await zrequire('./test-module', // same as require()
{ broadcast_funcs: ['changeState'] }) // mask 'changeState()' function will change environment and broadcast to all threads
// It is same as an normal module, but wrapped by Promise.
assert(await test_module.fib(40) === require('./test-module').fib(40))
// broadcast function can change all threads' environment
// note: broadcast function cannot return anything
await test_module.changeState('hello napajs')
assert(await test_module.getState() === 'hello napajs')