Code Monkey home page Code Monkey logo

reissue's Introduction

reissue

NPM Version Build Status Coverage Status Dependency Status devDependency Status bitHound Score NSP Status

setInterval with setTimeout semantics

Often you want to execute an operation at certain intervals, pending completion of the operation. For asynchronous operations, setInterval() is not sufficient for this scenario, as it can execute the operation a second time even if the first one is still in flight. A refresh like operation is a common use case.

Instead, reissue uses setTimeout() under the hood, queuing up a second execution only after the first exeuction has completed. If the time elapsed exceeds that of the specified interval, the second execution will be invoked immediately. This means that the behavior of reissue may not be identical to that of setInterval() for asynchronous operations. This is intended behavior.

Getting Started

Install the module with: npm install reissue

Usage

To begin, create a reissue object, which will return you an handler object:

var reissue = require('reissue');

var handler = reissue.create({
    func: function print(callback) {
        console.log('hi');
        return callback();
    },
    interval: 1000
});

handler.start();

Calling start() on the handler will begin the interval, and now reissue will log 'hi' to the console once every second. Note the callback parameter passed to the function - this callback must be called in order for the next invocation to proceed.

API

reissue.create()

reissue takes the following options to its create() method:

  • opts.func {Function} the function to execute. This function is invoked with a callback function as it's last parameter.
  • opts.interval {Number | Function} the inteval in ms to execute the function, or a function that returns an interval, allowing usage of a dynamic interval.
  • [opts.context] {Context} an optional this context for the function. use this in lieu of native bind() if you are concerned about performance. reissue uses apply() under the hood to do context/arg binding.
  • [opts.args] {Array} an optional array of arguments for the function. used in conjunction with the same apply() call as the context.

Returns: {Object} returns a handler object

The returned handler object exposes the following methods:

handler.start(delay)

Starts the timer interval. Calling start() while reissue is already active will throw an exception.

  • delay {Number} an optional delay in ms before first invocation. if no delay is provided, first invocation is synchronous (no setImmediate, no setTimeout).

Returns: {undefined} returns nothing

handler.stop()

Stops the timer interval. Takes no parameters. This will stop all further invocations of the function, even if they have already been scheduled.

Returns: {undefined} returns nothing

The handler object also emits the following events:

handler.on('error', function(err) {...}

If your function returns an error to the callback, this event will be emitted. The subscribed function will receive an error as it's only parameter.

Contributing

Add unit tests for any new or changed functionality. Ensure that lint and style checks pass.

To start contributing, install the git preush hooks:

make githooks

Before committing, run the prepush hook:

make prepush

If you have style errors, you can auto fix whitespace issues by running:

make codestyle-fix

License

Copyright (c) 2015 Alex Liu

Licensed under the MIT license.

reissue's People

Contributors

donutespresso avatar

Watchers

Scott O'Neal avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.