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

arbitrary-emitter

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

arbitrary-emitter - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

15

arbitrary-emitter.js

@@ -24,2 +24,15 @@ 'use strict'

function addOnce (key, method) {
const link = links.get(key) || createNewLink(key)
const wrap = {}
const fn = () => {
method()
wrap.remove()
}
link.add(fn)
wrap.remove = () => {
link.delete(fn)
}
}
function trigger (key) {

@@ -31,3 +44,3 @@ const link = links.get(key)

return { add, trigger }
return { add, addOnce, trigger }
}

2

package.json
{
"name": "arbitrary-emitter",
"version": "0.1.0",
"version": "0.2.0",
"description": "Event emitter with Map/Set sugar",

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

arbitrary-emitter
=================
Event emitter with Map/Set sugar for browser and node.js apps
**arbitrary-emitter** allows to use arbitrary values as keys for your events
**arbitrary-emitter** is written in vanilla ES6, so maybe you want to transpile it before using it.
**Project in active development, API may change**
## Create a new emitter
```js
const emitter = arbitraryEmitter()
```
## Emitter API
- [emitter.add](#emitter-add-api)
- [emitter.addOnce](#emitter-addonce-api)
- [emitter.trigger](#emitter-trigger-api)
- [Testing](#testing)
<a name="emitter-add-api"></a>
### add(key, method)
Add a listener for `key` which will trigger `method` function.
`key` can be any type of value.
`add` returns unsubscribe method
```js
const obj = {}
let unsubscribe = emitter.add(obj, () => doSomething())
emitter.trigger(obj) // will `doSomething`
unsubscribe()
emitter.trigger(obj) // won't do anything
```
<a name="emitter-addonce-api"></a>
### addOnce(key, method)
Add a listener for `key` which will trigger `method` function just one time, then listener will be removed.
`key` can be any type of value
```js
const obj = {}
emitter.addOnce(obj, () => doSomethingOnce())
emitter.trigger(obj) // will `doSomething`
emitter.trigger(obj) // won't do anything
```
<a name="emitter-trigger-api"></a>
## emitter.trigger(key)
Trigger methods binded to `key`
```js
emitter.trigger(obj)
```
<a name="testing"></a>
## Testing
### Node
```sh
npm test
```
[![Build Status](https://travis-ci.org/jacoborus/arbitrary-emitter.svg?branch=master)](https://travis-ci.org/jacoborus/arbitrary-emitter)

@@ -5,0 +82,0 @@

@@ -6,12 +6,27 @@ 'use strict'

test('hello world', t => {
test('add and trigger', t => {
const emitter = ae()
const obj = {}
let control = 0
emitter.add(obj, () => ++control)
let unsubscribe = emitter.add(obj, () => ++control)
emitter.trigger(obj)
t.is(control, 1)
t.is(control, 1, 'trigger')
emitter.trigger(obj)
t.is(control, 2)
t.is(control, 2, 'trigger')
unsubscribe()
emitter.trigger(obj)
t.is(control, 2, 'unsubscribe')
t.end()
})
test('add once', t => {
const emitter = ae()
const obj = {}
let control = 0
emitter.addOnce(obj, () => ++control)
emitter.trigger(obj)
t.is(control, 1, 'trigger once')
emitter.trigger(obj)
t.is(control, 1, 'automatic unsubscription')
t.end()
})
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