intervals-composite
v1.0.0
Published
simplifies working with individual and composite intervals in an application.
Downloads
4
Maintainers
Readme
intervals-composite
Encapsulate javascript .setInterval
& .clearInterval
into an Interval class. It also adds an IntervalComposite that simplifies working with multiple intervals in an application.
Table of Contents
- Install
- API
- require
- import
- Interval
- IntervalComposite
- Construction
- .add(interval)
- .has(label)
- .get(label)
- .getLabel()
- .forEach(cb)
- .filter(cb, label)
- .toArray()
- .count()
- .start()
- .isRunning()
- .clear()
- Build
- License
install
npm install --save intervals-composite
API
require
const { Interval, IntervalComposite } = require('intervals-composite');
import
import { Interval, IntervalComposite } from 'intervals-composite';
Interval
Example
const interval = new Interval({
cb: () => console.log('test'),
ms: 3000,
label: 'test-interval'
});
// OR, if you have the callback in another object
const handler = {
someFunction: () => console.log('test')
};
const interval = new Interval({
cb: handler.someFunction,
ms: 3000,
label: 'test-interval'
});
gets the interval label.
Example
console.log(interval.getLabel()); // 'test-interval'
.getMs()
gets the interval ms.
Example
console.log(interval.getMs()); // 3000
.getCb()
gets the interval callback.
Example
console.log(interval.getCb()); // [Function: someFunction]
starts the interval.
Example
interval.start();
checks if the interval is running.
Example
console.log(interval.isRunning()); // true
/*
test
test
test
.
.
.
*/
clears the interval
Example
interval.clear();
console.log(interval.isRunning()); // false
IntervalComposite
Example
const dataLoaders = new IntervalComposite('data-loaders');
.add(interval)
adds an interval.
Example
dataLoaders.add(new Interval({
cb: () => console.log('users'),
ms: 7000,
label: 'users'
}));
dataLoaders.add(new Interval({
cb: () => console.log('products'),
ms: 3000,
label: 'products'
}));
dataLoaders.add(new Interval({
cb: () => console.log('orders'),
ms: 1000,
label: 'orders'
}));
.has(label)
checks if a label exists.
Example
console.log(dataLoaders.has('orders')); // true
gets an interval by its label.
Example
const ordersInterval = dataLoaders.get('orders');
console.log(ordersInterval.getLabel()); // orders
console.log(ordersInterval.isRunning()); // false
gets the composite label.
Example
console.log(dataLoaders.getLabel()); // data-loaders
.forEach(cb)
traverses the intervals.
Example
dataLoaders.forEach((interval) => {
console.log(interval.getLabel());
});
/*
users
products
orders
*/
.filter(cb, label)
filters the intervals using a callback. It also accept an optional label to name the filtered composite.
Example
const slowLoaders = dataLoaders.filter((i) => i.getMs() > 1000, 'slow-intervals');
console.log(slowLoaders.getLabel()); // slow-intervals
slowLoaders.forEach((interval) => console.log(interval.getLabel()));
/*
users
products
*/
.toArray()
converts the composite into an array of intervals.
Example
console.log(dataLoaders.toArray().map(i => i.getLabel()));
/*
[ 'users', 'products', 'orders' ]
*/
.count()
gets the count of intervals.
Example
console.log(dataLoaders.count()); // 3
starts the intervals
Example
dataLoaders.start();
/*
orders
orders
products
orders
orders
orders
products
orders
users
orders
orders
*/
checks if the intervals are started.
Example
console.log(dataLoaders.isRunning()); // true
clears the intervals.
Example
dataLoaders.clear();
console.log(dataLoaders.isRunning()); // false
Build
grunt build
License
The MIT License. Full License is here