arbitrary-emitter
Advanced tools
Comparing version 0.7.2 to 0.7.3
@@ -6,42 +6,41 @@ 'use strict' | ||
function createNewLink (key) { | ||
const bindings = new Set() | ||
function oneTrigger (fn, lis) { | ||
lis.launch1 = fn | ||
lis.launchX = function () { | ||
fn.apply(fn, arguments[0]) | ||
} | ||
} | ||
const link = { | ||
function multipleTriggers (triggers, lis) { | ||
lis.launch1 = a => triggers.forEach(f => f(a)) | ||
lis.launchX = function () { | ||
let a = arguments[0] | ||
triggers.forEach(f => f.apply(f, a)) | ||
} | ||
} | ||
function newListener (key) { | ||
const triggers = new Set() | ||
const lis = { | ||
add (fn) { | ||
bindings.add(fn) | ||
let size = bindings.size | ||
triggers.add(fn) | ||
let size = triggers.size | ||
if (size === 1) { | ||
link.launch0 = fn | ||
link.launchX = function () { | ||
fn.apply(fn, arguments[0]) | ||
} | ||
oneTrigger(fn, lis) | ||
} else if (size === 2) { | ||
// link.launch0 = () => bindings.forEach(f => f()) | ||
link.launch0 = a => bindings.forEach(f => f(a)) | ||
link.launchX = function () { | ||
let a = arguments[0] | ||
bindings.forEach(f => f.apply(f, a)) | ||
} | ||
multipleTriggers(triggers, lis) | ||
} | ||
}, | ||
rm (method) { | ||
bindings.delete(method) | ||
let size = bindings.size | ||
if (size === 0) { | ||
triggers.delete(method) | ||
let size = triggers.size | ||
if (!size) { | ||
listeners.delete(key) | ||
} else if (size === 1) { | ||
let fn | ||
bindings.forEach(f => {fn = f}) | ||
link.launch0 = fn | ||
link.launchX = function () { | ||
fn.apply(fn, arguments[0]) | ||
} | ||
triggers.forEach(f => {fn = f}) | ||
oneTrigger(fn, lis) | ||
} else if (size === 2) { | ||
// link.launch0 = () => bindings.forEach(f => f()) | ||
link.launch0 = a => bindings.forEach(f => f(a)) | ||
link.launchX = function () { | ||
let a = arguments[0] | ||
bindings.forEach(f => f.apply(f, a)) | ||
} | ||
multipleTriggers(triggers, lis) | ||
} | ||
@@ -51,4 +50,4 @@ } | ||
listeners.set(key, link) | ||
return link | ||
listeners.set(key, lis) | ||
return lis | ||
} | ||
@@ -58,8 +57,8 @@ | ||
on (key, method) { | ||
const link = listeners.get(key) || createNewLink(key) | ||
link.add(method) | ||
const lis = listeners.get(key) || newListener(key) | ||
lis.add(method) | ||
let isSubscribed = true | ||
return () => { | ||
if (isSubscribed) { | ||
link.rm(method) | ||
lis.rm(method) | ||
isSubscribed = false | ||
@@ -71,7 +70,7 @@ } | ||
once (key, method) { | ||
const link = listeners.get(key) || createNewLink(key) | ||
link.add(fn) | ||
const lis = listeners.get(key) || newListener(key) | ||
lis.add(fn) | ||
function fn () { | ||
method(arguments) | ||
link.rm(fn) | ||
lis.rm(fn) | ||
} | ||
@@ -81,14 +80,18 @@ }, | ||
emit (key) { | ||
const link = listeners.get(key) | ||
if (!link) return | ||
const lis = listeners.get(key) | ||
if (!lis) return | ||
const l = arguments.length | ||
switch (l) { | ||
case 1: { | ||
link.launch0() | ||
lis.launch1() | ||
break | ||
} | ||
case 2: { | ||
link.launch0(arguments[1]) | ||
lis.launch1(arguments[1]) | ||
break | ||
} | ||
case 3: { | ||
lis.launch1(arguments[1], arguments[2]) | ||
break | ||
} | ||
default: { | ||
@@ -100,3 +103,3 @@ let l = arguments.length | ||
} | ||
link.launchX(args) | ||
lis.launchX(args) | ||
} | ||
@@ -108,4 +111,4 @@ } | ||
if (1 in arguments) { | ||
let link = listeners.get(key) | ||
if (link) link.rm(action) | ||
let lis = listeners.get(key) | ||
if (lis) lis.rm(action) | ||
} else { | ||
@@ -112,0 +115,0 @@ listeners.delete(key) |
{ | ||
"name": "arbitrary-emitter", | ||
"version": "0.7.2", | ||
"description": "Event emitter with Map/Set sugar", | ||
"version": "0.7.3", | ||
"description": "Hi-Perf event emitter with Map/Set sugar", | ||
"main": "arbitrary-emitter.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
arbitrary-emitter | ||
================= | ||
Event emitter with Map/Set sugar for node.js and browsers (<400 bytes when gzipped) | ||
High performance event emitter with Map/Set sugar for node.js and browsers (<500 bytes when gzipped) | ||
@@ -21,8 +21,2 @@ [![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) | ||
- [on](#emitter-on-api) | ||
- [once](#emitter-once-api) | ||
- [emit](#emitter-emit-api) | ||
- [off](#emitter-off-api) | ||
<a name="emitter-on-api"></a> | ||
@@ -29,0 +23,0 @@ ### on(key, action) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
199
10097
106