About
- Node.js object wrapper for unref'ed setInterval.
- weak means it will not prevent Node.js process to exit if event loop is empty.
- It will help you remember about routine context.
- No external dependencies.
Installation
Requires Node.js v4.8.7
$ npm install weak-daemon
Examples
const WeakDaemon = require('weak-daemon').WeakDaemon
var daemon = new WeakDaemon(
102,
null,
() => { console.log('tick', 'tock') }
);
daemon.start();
..
daemon.start();
daemon.start(false);
daemon.start(true);
..
daemon.start();
daemon.isRunning();
daemon.stop();
daemon.isRunning();
daemon.start();
- Equivalent of above example
var daemon = new WeakDaemon(
102,
null,
console.log,
['tick', 'tock']
);
- Function that requires a caller context, because of
this
usage:
const worker = {
data: {},
updateData(source) {
this.data = source.data;
}
}
var daemon = new WeakDaemon(
102,
worker,
worker.updateData,
[data_source]
);
daemon.start();
const worker = {
data: {},
source: {...},
updateData() {
this.data = this.source.data()
}
}
var daemon = new WeakDaemon(
101,
null,
worker.updateData
);
daemon.start();
Mocking for test purposes:
const {getInstance} = require('weak-daemon');
const non_mockable_daemon = new WeakDaemon(...args);
const mockable_daemon = getInstance(...args);
const WD = require('weak-daemon');
WD.getInstance = () => { return your_mock; }
Alternative way:
const {WeakDaemon, getClass} = require('weak-daemon');
const non_mockable_daemon = new WeakDaemon(...args)
const MockableWeakDaemon = getClass();
const mockable_daemon = new MockableWeakDaemon(...args);
const WD = require('weak-daemon');
WD.getClass = () => { return YourMock; }
Documentation
class WeakDaemon
function getInstance
Provided for mocking purposes.
-
Arguments: @see class WeakDaemon
constructor
-
Returns: instance of WeakDaemon.
function getClass
Provided for mocking purposes.
-
Arguments: no
-
Returns: WeakDaemon class.