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
npm install --save intervals-compositeAPI
require
const { Interval, IntervalComposite } = require('intervals-composite');import
import { Interval, IntervalComposite } from 'intervals-composite';Interval
Construction
| constructor(params) | ||
|---|---|---|
| name | type | props |
| params | object |
cb function ms number label string |
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'
});.getLabel()
gets the interval label.
| return |
|---|
| string |
Example
console.log(interval.getLabel()); // 'test-interval'.getMs()
gets the interval ms.
| return |
|---|
| number |
Example
console.log(interval.getMs()); // 3000.getCb()
gets the interval callback.
| return |
|---|
| function |
Example
console.log(interval.getCb()); // [Function: someFunction].start()
starts the interval.
Example
interval.start();.isRunning()
checks if the interval is running.
Example
console.log(interval.isRunning()); // true
/*
test
test
test
.
.
.
*/.clear()
clears the interval
Example
interval.clear();
console.log(interval.isRunning()); // falseIntervalComposite
Construction
| constructor(label) | |
|---|---|
| name | type |
| label | string |
Example
const dataLoaders = new IntervalComposite('data-loaders');.add(interval)
adds an interval.
| params | |
|---|---|
| name | type |
| interval | 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.
| params | |
|---|---|
| name | type |
| label | string |
Example
console.log(dataLoaders.has('orders')); // true.get(label)
gets an interval by its label.
| return |
|---|
| Interval |
Example
const ordersInterval = dataLoaders.get('orders');
console.log(ordersInterval.getLabel()); // orders
console.log(ordersInterval.isRunning()); // false.getLabel()
gets the composite label.
| return |
|---|
| string |
Example
console.log(dataLoaders.getLabel()); // data-loaders.forEach(cb)
traverses the intervals.
| params | |
|---|---|
| name | type |
| cb | function |
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.
| params | |
|---|---|
| name | type |
| cb | function |
| label | string |
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.
| return | item type |
|---|---|
| array | Interval |
Example
console.log(dataLoaders.toArray().map(i => i.getLabel()));
/*
[ 'users', 'products', 'orders' ]
*/.count()
gets the count of intervals.
| return |
|---|
| number |
Example
console.log(dataLoaders.count()); // 3.start()
starts the intervals
Example
dataLoaders.start();
/*
orders
orders
products
orders
orders
orders
products
orders
users
orders
orders
*/.isRunning()
checks if the intervals are started.
| return |
|---|
| boolean |
Example
console.log(dataLoaders.isRunning()); // true.clear()
clears the intervals.
Example
dataLoaders.clear();
console.log(dataLoaders.isRunning()); // falseBuild
grunt buildLicense
The MIT License. Full License is here