cron-emitter
This is an event emitter that uses crontab instructions to register events to be emitted at regular intervals, or at certain specific times in the future. This module uses cron-parser to parse the crontab instructions. See also node-cron for a similar project with a more traditional node.js callback approach.
The main difference between this Library and node-cron is that this library aims to implement the event listener pattern, to achieve capabilities found in the Observer Pattern and the PUB/SUB Pattern
This enables you to write highly decoupled, functional and reactive code with lower complexity per function than callback driven code. You can delegate responsibility for dealing with events to as many different subsystems as we want, all listening to the same event notifications.
Table of contents
Install
$ npm install --save cron-emitter
Crontab syntax
See cron-parser for a simple introduction to the crontab syntax. If you're on a Linux or OS X computer type
$ man crontab
Usage
Creating an object
const CronEmitter = ;const emitter = ;
Adding an event
emitter;
Options
CronEmitter exposes the same options as cron-parser to provide a start date and an end date for when events should be emitted:
emitter;
API
new CronEmitter()
Return a new CronEmitter object
CronEmitter
cronEmitter.add(crontab, name, options) ⇒ Adds a new event to the list of events to be emitted. CronEmitter exposes the same options as cron-parser to provide a start date and an end date for when events should be emitted:
- Kind: instance method of
CronEmitter
- Returns:
CronEmitter
- A reference to self. - See: https://www.npmjs.com/package/cron-parser
Param | Type | Description |
---|---|---|
crontab | string |
the crontab declaration |
name | string |
the name of the event you want to emit. |
options | object |
an object with options to cron-parser |
CronEmitter
cronEmitter.remove(name) ⇒ Remove an event from the list of events
- Kind: instance method of
CronEmitter
- Returns:
CronEmitter
- self when successful - Throws:
- TypeError
Param | Type | Description |
---|---|---|
name | string |
of the event to remove. |
object
cronEmitter.getEventList() ⇒ Returns object with all the registered emitters
- Kind: instance method of
CronEmitter
- Returns:
object
- the list of events.
Example
const CronEmitter = ;const emitter = ; const now = ;const increment = counter + 1; emitter;emitter;emitter;emitter;emitter;emitter; console; emitter; emitter; emitter; emitter; emitter; emitter; const increment = counter + 1;let counter = 0; emitter; emitter;