Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

emitter20

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

emitter20 - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

4

index.d.ts
declare class Emitter {
on: (eventName: string, cb: (data?: any) => void) => void;
on: (eventName: string, callback: (data?: any) => void) => void;
off: (eventName: string, callback: (data?: any) => void) => void;
trigger: (eventName: string, data?: any) => void;
clear: (eventName?: string) => void;
}
export = Emitter;

@@ -1,20 +0,40 @@

module.exports = function() {
var subscribers = []
return {
on: function (eventName, cb) {
subscribers.push({
eventName: eventName,
cb: cb
})
module.exports = function () {
let subscribers = []
const self = {
// remove all subscribers
clear: (eventName) => {
subscribers = eventName != null
? subscribers.filter(subscriber => subscriber.eventName !== eventName)
: []
return self // return self to support chaining
},
trigger: function (eventName, data) {
// remove a subscriber
off: (eventName, callback) => {
const index = subscribers.findIndex(subscriber => subscriber.eventName === eventName && subscriber.callback === callback)
if (index >= 0) {
subscribers.splice(index, 1)
}
return self
},
// subscribe to an event
on: (eventName, callback) => {
subscribers.push({ eventName, callback })
return self
},
// trigger an event; all subscribers will be called
trigger: (eventName, data) => {
subscribers
.filter(function (subscriber) {
return subscriber.eventName === eventName
})
.forEach(function (subscriber) {
subscriber.cb(data)
})
.filter(subscriber => subscriber.eventName === eventName)
.forEach(subscriber => subscriber.callback(data))
return self
}
}
return self
}
{
"name": "emitter20",
"version": "1.0.1",
"version": "2.0.0",
"description": "An event emitter in 20 lines of code.",
"license": "ISC",
"repository": "https://github.com/metaraine/emitter20",
"repository": "https://github.com/raineorshine/emitter20",
"author": {
"name": "Raine Lourie",
"email": "raineorshine@gmail.com",
"url": "https://github.com/metaraine"
"name": "Raine Revere",
"email": "raine@cybersemics.org",
"url": "https://github.com/raineorshine"
},
"engines": {
"node": ">=0.10.0"
"node": ">=6"
},
"scripts": {
"test": "mocha"
"test": "npm run lint && mocha",
"lint": "standard"
},

@@ -24,6 +25,7 @@ "keywords": [

"devDependencies": {
"chai": "*",
"mocha": "*"
"chai": "^4.2.0",
"mocha": "^7.1.1",
"standard": "^14.3.3"
},
"dependencies": {}
}

@@ -5,3 +5,3 @@ # emitter20

An event emitter in 20 lines of code.
An event emitter that used to be 20 lines of code but is still quite small. No dependencies.

@@ -14,2 +14,10 @@ ## Install

## Methods
- `on: (eventName: string, callback: Function) => void` - Subscribe a callback to the given event type.
- `off: (eventName: string, callback: Function) => void` - Remove a callback from the given event type.
- `trigger: (eventName: string, callback: Function) => void` - Trigger an event, invoking all subscribers.
- `clear: (eventName?: string) => void` - Remove all subscribers from the given event type, or all subscribers if no event type is specified.
## Usage

@@ -58,2 +66,2 @@

ISC © [Raine Lourie](https://github.com/metaraine)
ISC © [Raine Revere](https://github.com/raineorshine)

@@ -1,9 +0,8 @@

var chai = require('chai')
var should = chai.should()
var Emitter = require('../index.js')
/* eslint-env mocha */
const chai = require('chai')
const Emitter = require('../index.js')
describe('emitter20', function () {
it('should emit events', function (done) {
var emitter = new Emitter()
it('should emit events', done => {
const emitter = new Emitter()
emitter.on('karate-chop', done)

@@ -13,6 +12,6 @@ emitter.trigger('karate-chop')

it('should pass data from trigger to on', function (done) {
var emitter = new Emitter()
emitter.on('welcome', function (name) {
name.should.equal('bob')
it('should pass data from trigger to on', done => {
const emitter = new Emitter()
emitter.on('welcome', name => {
chai.expect(name).equals('bob')
done()

@@ -23,2 +22,47 @@ })

it('should return itself', done => {
const emitter = new Emitter()
emitter
.on('later', done)
.trigger('later')
})
it('should remove events', done => {
const emitter = new Emitter()
const onWelcome = name => {
throw new Error('Subscriber should not be triggered after off.')
}
emitter.on('welcome', onWelcome)
emitter.off('welcome', onWelcome)
emitter.trigger('welcome', 'bob')
done()
})
it('should clear events with a given eventName', done => {
const emitter = new Emitter()
const onOne = name => {
throw new Error('Subscriber should not be triggered after clear.')
}
emitter.on('one', onOne)
emitter.on('done', done)
emitter.clear('one')
emitter.trigger('one')
emitter.trigger('done')
})
it('should clear events with a given eventName', done => {
const emitter = new Emitter()
const onOne = name => {
throw new Error('Subscriber should not be triggered after clear.')
}
const onTwo = name => {
throw new Error('Subscriber should not be triggered after clear.')
}
emitter.on('one', onOne)
emitter.on('two', onTwo)
emitter.clear()
emitter.trigger('one')
emitter.trigger('two')
done()
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc