regular-listener
v1.0.8
Published
RegularListener is a listener that detects the value periodically.
Downloads
30
Maintainers
Readme
RegularListener ·
RegularListener is a listener that detects the value periodically.
This listener can periodically detects any event without having to handle complicated settings. There are many kind of listeners, and you also can make your own one. Of course, all listeners are successfully tested by Jest.
Usage
listen to boolean once
import { BooleanListener } from 'regular-listener';
let boolean = false;
...
const listener = new BooleanListener(() => {
console.log('The value is true!');
})
listener.listenOnce(() => boolean);
listen to boolean every 500ms
import { BooleanListener } from 'regular-listener';
let boolean = false;
...
const listener = new BooleanListener(() => {
console.log('The value is true!');
})
listener.setDelay(500);
listener.listen(() => boolean);
listen to each element in the array and check that at least one element satisfies the condition
import { BooleanListener } from 'regular-listener';
let array = [true, true];
...
const listener = new BooleanListener(() => {
console.log('All Elements are true!');
})
listener.setReadTypeOfArray('some');
listener.listen(() => array);
listen to each element in the array and check that every element satisfies the condition
import { BooleanListener } from 'regular-listener';
let array = [true, true];
...
const listener = new BooleanListener(() => {
console.log('All Elements are true!');
})
listener.setReadTypeOfArray('every');
listener.listen(() => array);
handle error
import { BooleanListener } from 'regular-listener';
const listener = new BooleanListener(() => {
throw new Error('mock error');
})
listener.catch((error) => {
console.warn(`An error was thrown.`, error);
})
listener.listenOnce(() => {
throw new Error('mock error');
})
print message
import { BooleanListener } from 'regular-listener';
let value = false;
const listener = new BooleanListener(() => { throw new Error('mock error') });
listener.printMessage();
listener.catch(() => {});
listener.listenOnce(() => value);
setTimeout(() => {
value = true;
}, 3000);
listen | isTriggered: false, value: false
listen | isTriggered: false, value: false
listen | isTriggered: false, value: false
listen | isTriggered: true, value: true
catch error | Error: mock error
make new listener
class PositiveNumberListener extends RegularListener {
protected isTriggered(value: number): boolean {
return (value > 0);
}
}
Installation
npm install regular-listener
API
class RegularListener
| method | Return Type | Description | | ----------------------------- | ----------- | --------------------------------------------------- | | isTriggered( value: unknown) | boolean | Define whether the value triggers the condition. | | isTriggered( element: unknown, index: number)isTriggered( element: unknown, index: number, value: Array<unknown>) | boolean | Define whether the element in the value, which is an array, triggers the condition. |
Listener
| name | Description | | --------------------- | --------------------------------------------------------- | | BooleanListener | Listen whether the value is true through Boolean. | | ValueExistedListener | Listen whether the value exists, not undefined or null. | | ValueUpdatedListener | Listen whether the value is updated. |
License
RegularListener is MIT licensed.