npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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! |