intervals-composite

1.0.0 • Public • Published

intervals-composite

build:? npm npm npm

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

API

require

const { Interval, IntervalComposite } = require('intervals-composite');

import

import { Interval, IntervalComposite } from 'intervals-composite';

Interval

Construction

constructor(params)
nametypeprops
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()); // false

IntervalComposite

Construction

constructor(label)
nametype
label string

Example

const dataLoaders = new IntervalComposite('data-loaders');

.add(interval)

adds an interval.

params
nametype
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
nametype
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
nametype
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
nametype
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.

returnitem 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()); // false

Build

grunt build

License

The MIT License. Full License is here

Package Sidebar

Install

npm i intervals-composite

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

14.5 kB

Total Files

7

Last publish

Collaborators

  • eyas-ranjous