Skip to content
Permalink
Browse files

Update: WeakMaps

  • Loading branch information
jacoborus committed Jan 8, 2020
1 parent b13d985 commit 560163e82f29fd26f7bf5bee18c9a3906b30696f
Showing with 32 additions and 92 deletions.
  1. +1 −1 LICENSE
  2. +8 −26 README.md
  3. +7 −7 package.json
  4. +9 −33 src/tests.js
  5. +7 −13 src/{arbitrary-emitter.js → weak-emitter.js}
  6. +0 −12 tests/test.html
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Jacobo Tabernero - github.com/jacoborus
Copyright (c) 2020 Jacobo Tabernero - github.com/jacoborus

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
@@ -1,14 +1,14 @@
arbitrary-emitter
weak-emitter
=================

ES6 Map based event emitter in ~350 bytes
ES6 WeakMap based event emitter in ~350 bytes

[![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)
[![Build Status](https://travis-ci.org/jacoborus/weak-emitter.svg?branch=master)](https://travis-ci.org/jacoborus/weak-emitter) [![npm version](https://badge.fury.io/js/weak-emitter.svg)](https://www.npmjs.com/package/weak-emitter) ![npm dependencies](https://david-dm.org/jacoborus/weak-emitter.svg)

Arbitrary-emitter stores listeners in **ES6 maps**, so you can use any kind of value as key for your events
Weak-emitter stores events in **ES6 weakmaps**, so objects are the only valid type for the event keys.

```js
const emitter = arbitraryEmitter()
const emitter = weakEmitter()
const key = {}
emitter.on(key, () => doSomething())
// will `doSomething`
@@ -17,12 +17,12 @@ emitter.emit(key)

- **~350 bytes** when gzipped
- conventional api (`on`, `off`, `once` and `emit`)
- check [weak-emitter](https://github.com/jacoborus/weak-emitter) for a version that uses weakmaps to store events
- check [arbitrary-emitter](https://github.com/jacoborus/weak-emitter/tree/arbitrary-emitter) for a version that uses maps to store events


## Install

Install with [npm](https://www.npmjs.com/package/arbitrary-emitter) or yarn, clone the repo or download and extract the [zip](https://github.com/jacoborus/arbitrary-emitter/archive/master.zip).
Install with [npm](https://www.npmjs.com/package/weak-emitter) or yarn, clone the repo or download and extract the [zip](https://github.com/jacoborus/weak-emitter/archive/master.zip).
Then import or insert it as script tag.


@@ -32,7 +32,6 @@ Then import or insert it as script tag.
- [off](#emitter-off-api)
- [once](#emitter-once-api)
- [emit](#emitter-emit-api)
- [listeners](#emitter-listeners-api)

<a name="emitter-on-api"></a>
### on(key, handler)
@@ -86,23 +85,6 @@ emitter.off(key) // will remove all the listeners tagged with `key`, and the tag
```


<a name="emitter-listeners-api"></a>
### listeners(key)

Create and return an array with all the handlers for the event tagged with `key`


```js
const f1 = () => alert('hi')
const f2 = () => alert('ho')
emitter.on('a', f1)
emitter.on('a', f2)
emitter.emitters()
// ==> [f1, f2]
```


<a name="testing"></a>
## Testing

@@ -114,4 +96,4 @@ npm test

---

© 2020 [Jacobo Tabernero](http://jacoborus.codes) - Released under [MIT License](https://raw.github.com/jacoborus/arbitrary-emitter/master/LICENSE)
© 2020 [Jacobo Tabernero](http://jacoborus.codes) - Released under [MIT License](https://raw.github.com/jacoborus/weak-emitter/master/LICENSE)
@@ -1,15 +1,15 @@
{
"name": "arbitrary-emitter",
"version": "2.0.1",
"description": "Map based event emitter in ~350 bytes",
"main": "src/arbitrary-emitter.js",
"name": "weak-emitter",
"version": "3.0.0",
"description": "ES6 WeakMap based event emitter (~350 bytes)",
"main": "src/weak-emitter.js",
"scripts": {
"linter": "eslint src/*.js",
"test": "node ./src/tests.js"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/jacoborus/arbitrary-emitter.git"
"url": "git+ssh://git@github.com/jacoborus/weak-emitter.git"
},
"keywords": [
"event emitter",
@@ -19,9 +19,9 @@
"author": "Jacobo Tabernero",
"license": "MIT",
"bugs": {
"url": "https://github.com/jacoborus/arbitrary-emitter/issues"
"url": "https://github.com/jacoborus/weak-emitter/issues"
},
"homepage": "https://github.com/jacoborus/arbitrary-emitter",
"homepage": "https://github.com/jacoborus/weak-emitter",
"devDependencies": {
"eslint": "6.8.0",
"eslint-config-standard": "14.1.0",
@@ -1,10 +1,10 @@
'use strict'

const test = require('tape')
const ae = require('./arbitrary-emitter.js')
const weake = require('./weak-emitter.js')

test('on and emit', t => {
const emitter = ae()
const emitter = weake()
const obj = {}
let control = 0
const fn = () => ++control
@@ -20,7 +20,7 @@ test('on and emit', t => {
})

test('once', t => {
const emitter = ae()
const emitter = weake()
const obj = {}
let control = 0
emitter.once(obj, () => ++control)
@@ -32,7 +32,7 @@ test('once', t => {
})

test('off listener', t => {
const emitter = ae()
const emitter = weake()
const obj = {}
let control = 0
emitter.on(obj, () => ++control)
@@ -46,7 +46,7 @@ test('off listener', t => {
})

test('off action', t => {
const emitter = ae()
const emitter = weake()
const obj = {}
const control = {
a: 0,
@@ -66,7 +66,7 @@ test('off action', t => {
})

test('emit actions in order', t => {
const emitter = ae()
const emitter = weake()
const obj = {}
let control = ''
emitter.on(obj, () => { control = control + 'a' })
@@ -87,7 +87,7 @@ test('emit actions in order', t => {
})

test('emit with arguments', t => {
const emitter = ae()
const emitter = weake()
const obj = {}
const control = {
a: 0
@@ -101,7 +101,7 @@ test('emit with arguments', t => {
})

test('once in a event with muliple listeners', t => {
const emitter = ae()
const emitter = weake()
const out = []
const tt = {}
emitter.on(tt, () => {
@@ -124,7 +124,7 @@ test('once in a event with muliple listeners', t => {
})

test('remove listener in a event with muliple listeners', t => {
const emitter = ae()
const emitter = weake()
const out = []
const tt = {}
const f1 = () => out.push(1)
@@ -145,27 +145,3 @@ test('remove listener in a event with muliple listeners', t => {
t.is(out[3], 2)
t.end()
})

test('get listeners', t => {
const emitter = ae()
const key = {}
const key2 = {}
let c1 = 0
let c2 = 0
const f1 = () => ++c1
const f2 = () => ++c2
emitter.on(key, f1)
emitter.on(key, f2)
emitter.on(key2, f2)
emitter.emit(key)
t.is(c1, 1)
t.is(c2, 1)
const list = emitter.listeners(key)
t.is(list[0], f1)
t.is(list[1], f2)
const list2 = emitter.listeners(key2)
t.is(list2[0], f2)
const other = emitter.listeners({})
t.is(other.length, 0)
t.end()
})
@@ -1,7 +1,7 @@
'use strict'

module.exports = () => {
const events = new Map()
const events = new WeakMap()
const newEvent = key => {
const handlers = new Map()
events.set(key, handlers)
@@ -10,13 +10,14 @@ module.exports = () => {

return {
on (key, handler) {
(events.get(key) || newEvent(key)).set(handler, handler)
(events.get(key) || newEvent(key))
.set(handler, handler)
},
once (key, handler) {
const handlers = events.get(key) || newEvent(key)
handlers.set(handler, function () {
handler(arguments)
handlers.delete(handler)
handler(arguments)
})
},

@@ -26,20 +27,13 @@ module.exports = () => {
},

off (key, handler) {
const handlers = events.get(key)
if (!handlers) return
if (!(0 in arguments)) return
if (1 in arguments) {
handlers.delete(handler)
const handlers = events.get(key)
handlers && handlers.delete(handler)
} else {
events.delete(key)
}
},

listeners (key) {
const handlers = events.get(key)
return handlers
? [...handlers.values()]
: []
}
}
}

This file was deleted.

0 comments on commit 560163e

Please sign in to comment.
You can’t perform that action at this time.