arbitrary-emitter
Advanced tools
Comparing version 0.7.1 to 0.7.2
'use strict' | ||
function arbitrary () { | ||
const links = new Map() | ||
const listeners = new Map() | ||
function createNewLink (key) { | ||
const link = new Set() | ||
links.set(key, link) | ||
const bindings = new Set() | ||
const link = { | ||
add (fn) { | ||
bindings.add(fn) | ||
let size = bindings.size | ||
if (size === 1) { | ||
link.launch0 = fn | ||
link.launchX = function () { | ||
fn.apply(fn, arguments[0]) | ||
} | ||
} 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)) | ||
} | ||
} | ||
}, | ||
rm (method) { | ||
bindings.delete(method) | ||
let size = bindings.size | ||
if (size === 0) { | ||
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]) | ||
} | ||
} 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)) | ||
} | ||
} | ||
} | ||
} | ||
listeners.set(key, link) | ||
return link | ||
} | ||
const apply = Function.prototype.apply | ||
return { | ||
on (key, method) { | ||
const link = links.get(key) || createNewLink(key) | ||
const link = listeners.get(key) || createNewLink(key) | ||
link.add(method) | ||
@@ -21,3 +61,3 @@ let isSubscribed = true | ||
if (isSubscribed) { | ||
link.delete(method) | ||
link.rm(method) | ||
isSubscribed = false | ||
@@ -29,8 +69,7 @@ } | ||
once (key, method) { | ||
const link = links.get(key) || createNewLink(key) | ||
const link = listeners.get(key) || createNewLink(key) | ||
link.add(fn) | ||
const rm = () => link.delete(fn) | ||
function fn () { | ||
rm() | ||
method(arguments) | ||
link.rm(fn) | ||
} | ||
@@ -40,3 +79,3 @@ }, | ||
emit (key) { | ||
const link = links.get(key) | ||
const link = listeners.get(key) | ||
if (!link) return | ||
@@ -46,13 +85,9 @@ const l = arguments.length | ||
case 1: { | ||
link.forEach(fn => fn()) | ||
link.launch0() | ||
break | ||
} | ||
case 2: { | ||
link.forEach(fn => fn(arguments[1])) | ||
link.launch0(arguments[1]) | ||
break | ||
} | ||
case 3: { | ||
link.forEach(fn => fn(arguments[1], arguments[2])) | ||
break | ||
} | ||
default: { | ||
@@ -64,3 +99,3 @@ let l = arguments.length | ||
} | ||
link.forEach(fn => apply.call(fn, fn, args)) | ||
link.launchX(args) | ||
} | ||
@@ -72,6 +107,6 @@ } | ||
if (1 in arguments) { | ||
let link = links.get(key) | ||
if (link) link.delete(action) | ||
let link = listeners.get(key) | ||
if (link) link.rm(action) | ||
} else { | ||
links.delete(key) | ||
listeners.delete(key) | ||
} | ||
@@ -78,0 +113,0 @@ } |
{ | ||
"name": "arbitrary-emitter", | ||
"version": "0.7.1", | ||
"version": "0.7.2", | ||
"description": "Event emitter with Map/Set sugar", | ||
@@ -5,0 +5,0 @@ "main": "arbitrary-emitter.js", |
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
10369
198