robust-timers
v1.1.4
Published
Powerful yet simple timers engine with ability to save and restore timers states.
Downloads
17
Readme
Documentation: https://elkornacio.github.io/Robust-Timers/
Installation
$ npm install robust-timers
Example
Simple example of usage in pair with mysql native driver.
let RobustTimers = require('./index');
let mysql = require('mysql');
app.timers = new RobustTimers();
app.timers.register({
name: 'mail checker',
interval: 15 * 60 * 1000,
handler: _ => new Promise((resole, reject) => {
app.gmail.preloadAllMails()
.then(app.gmail.loadNewMails)
.then(app.gmail.loadAttachments)
.then(resolve)
.catch(reject);
})
});
let conn = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'tempTest'
});
conn.connect();
let dataSource = RobustTimers.DataSource.NativeMySQL(conn);
app.timers.assignDataSource(dataSource);
app.timers.restore().then(_ => r.start());
RobustTimers
Simple class used for multiple purposes regarding to timers. Except obvious functions like registering, unregistering timers, you can easily save and restore timers states, register "once" timers, activate and deactivate registered timers, provide your own handlers to serialize/unseriaize and save/restore timer states.
All functions (except asynchronous) returns context to provide fluent interface pattern.
Meant that this class will be used as singleton, but it's up to you and yours app's architecture.
Kind: global class
new RobustTimers(options)
Just constructor.
| Param | Type | Description | | --- | --- | --- | | options | Object | object containing options for construction. | | options.start | boolean | if true - calls this.start() at the end of construction. | | options.restoreAndStart | boolean | if true - calls this.restore() and after restoring states calls this.start(). Use only makes sense in combination with the provided options.dataSource param. | | options.dataSource | Object | data source object. You can see object description at this.assignDataSource() method. |
robustTimers.register(options)
Registers new timer and runs it if needed.
Kind: instance method of RobustTimers
Throws:
- Will throw an exception if there are no handler or interval provided in options.
| Param | Type | Description | | --- | --- | --- | | options | Object | object containing options of timer. | | options.name | string | name of the timer. Unique. Will be generated if not provided. All methods works with this name. | | options.active | boolean | if true - timer is active (and will be started immediatly if this.start() was already called). | | options.handler | function | timer's handler - function which called every time timer is fired. If options.isOnce is true - will be called once. Required. | | options.isOnce | boolean | if true - after first timer's firing timer will be unregistered. | | options.lastExecutionTimestamp | number | if timer was already fired in past this param should contain last execution timestamp (in ms) - it will be used to calculate correctly next execution time. | | options.interval | number | timer's execution interval. In milliseconds. Required. | | options.context | Object | context that will be provided to the timer's handler when it's executing. |
robustTimers.unregister(name)
Unregisters timer and stops it if needed.
Kind: instance method of RobustTimers
| Param | Type | Description | | --- | --- | --- | | name | string | name of the timer. |
robustTimers.activate(name)
Starts the timer lifecycle.
Kind: instance method of RobustTimers
| Param | Type | Description | | --- | --- | --- | | name | string | name of the timer. |
robustTimers.deactivate(name)
Pauses the timer lifecycle.
Kind: instance method of RobustTimers
| Param | Type | Description | | --- | --- | --- | | name | string | name of the timer. |
robustTimers.start()
If all event handlers and other staff is registered, timers states are restored, and we are ready to go at all - starts all registered timers lifecycles.
Kind: instance method of RobustTimers
robustTimers.interval(name, interval, handler)
Shortcut to register simple interval-based timer. Similar to simple JS setInterval.
Kind: instance method of RobustTimers
| Param | Type | Description | | --- | --- | --- | | name | string | name of the timer. | | interval | number | interval of the timer. | | handler | function | handler of the timer. |
robustTimers.once(name, interval, handler)
Shortcut to register simple timer, that should be fired only once. Similar to simple JS setTimeout.
Kind: instance method of RobustTimers
| Param | Type | Description | | --- | --- | --- | | name | string | name of the timer. | | interval | number | interval of the timer. | | handler | function | handler of the timer. |
robustTimers.restore(handler)
If handler is provided - registers it as default restoring function this.__onRestoreHandler. If not - trying to restore all inner state using this.__onRestoreHandler.
Kind: instance method of RobustTimers
Throws:
- Will throw an exception if there are no restore handler, or if restore handler did not return promise.
| Param | Type | Default | Description | | --- | --- | --- | --- | | handler | function | | async function that gets one param - this instance and should restore all it's inner state basing on anything (DB/Local Storage/etc.). |
robustTimers.save(handler)
If handler is provided - registers it as default saving function this.__onSaveHandler. If not - trying to save all inner state using this.__onSaveHandler.
Kind: instance method of RobustTimers
Throws:
- Will throw an exception if there are no save handler, or if save handler did not return promise.
| Param | Type | Default | Description | | --- | --- | --- | --- | | handler | function | | async function that gets one param - this instance and should save all it's inner state in anywhere (DB/Local Storage/etc.). |
robustTimers.assignDataSource(dataSource)
Registers save/restore handlers from the given object.
Kind: instance method of RobustTimers
| Param | Type | Description | | --- | --- | --- | | dataSource | Object | object with two properties "save" and "restore" containing save and restore functions respectively. THIS OBJECT IS NOT USED AS A CONTEXT WHEN THIS FUNCTIONS ARE CALLED! |