Socket
Socket
Sign inDemoInstall

arbitrary-emitter

Package Overview
Dependencies
0
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.10.1 to 0.10.2

46

arbitrary-emitter.js

@@ -7,3 +7,4 @@ 'use strict'

function setActions (e, listeners) {
function setActions (e) {
const listeners = e.listeners.filter(l => l)
let size = listeners.length

@@ -14,9 +15,15 @@ if (!size) {

} else if (size === 1) {
actions.set(e.key, listeners[0])
actions.set(e.key, (a, b) => {
let fn = listeners[0]
if (fn) fn(a, b)
})
} else {
actions.set(e.key, opts => {
actions.set(e.key, (a, b) => {
e.running.push(listeners)
let size = listeners.length
while (size > 0) {
listeners[--size](opts)
const fn = listeners[--size]
if (fn) fn(a, b)
}
e.running.pop()
})

@@ -28,5 +35,15 @@ }

const listeners = []
const running = []
function trash (list, lis) {
let index = list.indexOf(lis)
if (index > -1) {
delete list[index]
}
}
const e = {
key,
listeners,
running,
add (fn) {

@@ -36,10 +53,10 @@ if (listeners.indexOf(fn) === -1) {

}
setActions(e, listeners)
setActions(e)
},
rm (lis) {
let index = listeners.indexOf(lis)
if (index > -1) {
listeners.splice(index, 1)
setActions(e, listeners)
trash(listeners, lis)
if (running.length) {
running.forEach(list => trash(list, lis))
}
setActions(e)
}

@@ -56,9 +73,2 @@ }

e.add(lis)
let isSubscribed = true
return () => {
if (isSubscribed) {
e.rm(lis)
isSubscribed = false
}
}
},

@@ -75,5 +85,5 @@

emit (key, opts) {
emit (key, a, b) {
const action = actions.get(key)
if (action) action(opts)
if (action) action(a, b)
},

@@ -80,0 +90,0 @@

{
"name": "arbitrary-emitter",
"version": "0.10.1",
"version": "0.10.2",
"description": "Event emitter with ES6 Map sugar for modern browsers and node.js",

@@ -5,0 +5,0 @@ "main": "arbitrary-emitter.js",

arbitrary-emitter
=================
Event emitter with ES6 Map sugar for modern browsers and node.js (~400 bytes). [arbitrary-emitter.jacoborus.codes](http://arbitrary-emitter.jacoborus.codes)
Event emitter with ES6 Map sugar for modern browsers and node.js (~450 bytes). [arbitrary-emitter.jacoborus.codes](http://arbitrary-emitter.jacoborus.codes)

@@ -16,6 +16,5 @@ [![Build Status](https://travis-ci.org/jacoborus/arbitrary-emitter.svg?branch=master)](https://travis-ci.org/jacoborus/arbitrary-emitter) [![npm version](https://badge.fury.io/js/arbitrary-emitter.svg)](https://www.npmjs.com/package/arbitrary-emitter) ![npm dependencies](https://david-dm.org/jacoborus/arbitrary-emitter.svg)

- allows to use **arbitrary values** as keys for listeners
- really small footprint (**~400 bytes** when gzipped)
- really small footprint (**~450 bytes** when gzipped)
- **blazing fast**
- conventional api (`on`, `off`, `once` and `emit`)
- `on` method returns an unsubscription function (like in redux.js)

@@ -41,11 +40,6 @@ ## Usage

`on` returns removeListener method
```js
const key = {}
let removeListener = emitter.on(key, () => doSomething())
emitter.on(key, () => doSomething())
emitter.emit(key) // will `doSomething`
emitter.emit(key) // will `doSomething`
removeListener()
emitter.emit(key) // won't do anything
```

@@ -52,0 +46,0 @@

@@ -11,3 +11,3 @@ 'use strict'

const fn = () => ++control
let unsubscribe = emitter.on(obj, fn)
emitter.on(obj, fn)
emitter.emit(obj)

@@ -20,5 +20,2 @@ t.is(control, 1, 'trigger')

t.is(control, 3, 'trigger')
unsubscribe()
emitter.emit(obj)
t.is(control, 3, 'unsubscribe')
t.end()

@@ -108,3 +105,3 @@ })

test('remove listener while emitting', t => {
test('once in a event with muliple listeners', t => {
const emitter = ae()

@@ -130,1 +127,23 @@ const out = []

})
test('remove listener in a event with muliple listeners', t => {
const emitter = ae()
const out = []
const f1 = () => out.push(1)
const f2 = () => {
out.push(2)
emitter.off('test', f3)
}
const f3 = () => out.push(3)
emitter.on('test', f1)
emitter.on('test', f2)
emitter.on('test', f3)
emitter.emit('test')
t.is(out[0], 1)
t.is(out[1], 2)
t.notOk(out[2])
emitter.emit('test')
t.is(out[2], 1)
t.is(out[3], 2)
t.end()
})
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc