Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
seppevs committed May 24, 2019
0 parents commit b092551
Show file tree
Hide file tree
Showing 8 changed files with 6,159 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/dist
/dist.zip

# Created by https://www.gitignore.io/api/node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless

# End of https://www.gitignore.io/api/node

.idea
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sudo: false
language: node_js
node_js:
- '8'
cache:
directories:
- node_modules
before_install:
- npm update
install:
- npm install
script:
- npm test
- npm run coveralls
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# scheduled-event-emitter
Schedule events to be emitted in the future

[![Build Status](http://img.shields.io/travis/seppevs/scheduled-event-emitter.svg?style=flat)](https://travis-ci.org/seppevs/scheduled-event-emitter) [![Coverage Status](https://coveralls.io/repos/github/seppevs/scheduled-event-emitter/badge.svg?branch=master)](https://coveralls.io/r/seppevs/scheduled-event-emitter) [![NPM](http://img.shields.io/npm/v/scheduled-event-emitter.svg?style=flat)](https://www.npmjs.org/package/scheduled-event-emitter) [![Downloads](http://img.shields.io/npm/dm/scheduled-event-emitter.svg?style=flat)](https://www.npmjs.org/package/scheduled-event-emitter) [![Dependencies](https://david-dm.org/seppevs/scheduled-event-emitter.svg)](https://david-dm.org/seppevs/scheduled-event-emitter) [![Known Vulnerabilities](https://snyk.io/test/github/seppevs/scheduled-event-emitter/badge.svg)](https://snyk.io/test/github/seppevs/scheduled-event-emitter)

## Introduction
With this module, you can schedule events to be emitted in the future

## Installation
```bash
$ npm install scheduled-event-emitter --save
```

## API

### `new ScheduledEventEmitter()`
Constructs a new scheduledEventEmitter instance

### `scheduledEventEmitter.scheduleEmit(eventName, date, payload) → timeout`
Schedules an event with name `eventName`. This event will be emitted on `date` with payload `payload`
This function returns a `timeout`, which can be used to cancel the event (with the `clearTimeout` function)

### `scheduledEventEmitter.clearSchedule()`
Cancels all scheduled events

## Demo's

## Schedule an event on a fixed date
This snippet will write 'Happy New Year' on the first of January 2030
```javascript
const ScheduledEventEmitter = require('../src/ScheduledEventEmitter');

const scheduledEventEmitter = new ScheduledEventEmitter();
scheduledEventEmitter.on('deadlineReached', (message) => {
console.log(message);
});
scheduledEventEmitter.scheduleEmit('deadlineReached', new Date('2030-01-01T00:00:00.000Z'), 'Happy New Year!');
```

## Schedule an event to be emitted within a certain time
This snippet writes Ba Dum Tss! to the console after 5 seconds.
```javascript
const ScheduledEventEmitter = require('../src/ScheduledEventEmitter');

const scheduledEventEmitter = new ScheduledEventEmitter();
scheduledEventEmitter.on('playDrums', (data) => {
console.log(data);
});

const deadline = new Date(new Date().getTime() + 5000);
scheduledEventEmitter.scheduleEmit('playDrums', deadline, 'Ba Dum Tss');
```

## Cancel the emission of a single event
To cancel an event, use the `timeout` (returned by the `scheduleEmit` function) with the built-in [https://nodejs.org/api/timers
.html#timers_cleartimeout_timeout]
(clearTimeout) of Node.js (or your browser)

In this demo, nothing will be written to the console (because we cancelled the event we first scheduled):

```javascript
const ScheduledEventEmitter = require('../src/ScheduledEventEmitter');

const scheduledEventEmitter = new ScheduledEventEmitter();
scheduledEventEmitter.on('playDrums', (data) => {
console.log(data);
});

const deadline = new Date(new Date().getTime() + 5000);
const timeout = scheduledEventEmitter.scheduleEmit('playDrums', deadline, 'Ba Dum Tss');

clearTimeout(timeout);
```

## Cancel the emission of ALL scheduled events

In this demo, nothing will be written to the console (because we cancelled ALL scheduled events (with `clearSchedule`)):

```javascript
const ScheduledEventEmitter = require('../src/ScheduledEventEmitter');

const scheduledEventEmitter = new ScheduledEventEmitter();
scheduledEventEmitter.on('giveWarning', (data) => {
console.log(data);
});

const firstDeadline = new Date(new Date().getTime() + 5000);
scheduledEventEmitter.scheduleEmit('giveWarning', firstDeadline, 'First warning, please fix it');

const secondDeadline = new Date(new Date().getTime() + 5000);
scheduledEventEmitter.scheduleEmit('giveWarning', secondDeadline, 'Second warning, please fix it ASAP!!');

scheduledEventEmitter.clearSchedule();
```
Loading

0 comments on commit b092551

Please sign in to comment.