1.1.4 • Published 7 years ago

robust-timers v1.1.4

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

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.

ParamTypeDescription
optionsObjectobject containing options for construction.
options.startbooleanif true - calls this.start() at the end of construction.
options.restoreAndStartbooleanif true - calls this.restore() and after restoring states calls this.start(). Use only makes sense in combination with the provided options.dataSource param.
options.dataSourceObjectdata 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.
ParamTypeDescription
optionsObjectobject containing options of timer.
options.namestringname of the timer. Unique. Will be generated if not provided. All methods works with this name.
options.activebooleanif true - timer is active (and will be started immediatly if this.start() was already called).
options.handlerfunctiontimer's handler - function which called every time timer is fired. If options.isOnce is true - will be called once. Required.
options.isOncebooleanif true - after first timer's firing timer will be unregistered.
options.lastExecutionTimestampnumberif 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.intervalnumbertimer's execution interval. In milliseconds. Required.
options.contextObjectcontext 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

ParamTypeDescription
namestringname of the timer.

robustTimers.activate(name)

Starts the timer lifecycle.

Kind: instance method of RobustTimers

ParamTypeDescription
namestringname of the timer.

robustTimers.deactivate(name)

Pauses the timer lifecycle.

Kind: instance method of RobustTimers

ParamTypeDescription
namestringname 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

ParamTypeDescription
namestringname of the timer.
intervalnumberinterval of the timer.
handlerfunctionhandler 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

ParamTypeDescription
namestringname of the timer.
intervalnumberinterval of the timer.
handlerfunctionhandler 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.
ParamTypeDefaultDescription
handlerfunctionasync 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.
ParamTypeDefaultDescription
handlerfunctionasync 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

ParamTypeDescription
dataSourceObjectobject 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!
1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago