diff --git a/build/light-event-bus.js b/build/light-event-bus.js index 5be6a8f..560a19e 100644 --- a/build/light-event-bus.js +++ b/build/light-event-bus.js @@ -4,34 +4,27 @@ (global = global || self, factory(global.EVENT_BUS = {})); }(this, function (exports) { 'use strict'; - /** - * Source: https://gist.github.com/jed/982883 - */ - - /* eslint-disable */ - function b(a) { - return a ? (a ^ Math.random() * 16 >> a / 4).toString(16) : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, b); - } - /* eslint-enable */ - function EventBus() { var subscriptions = {}; this.subscribe = function subscribeCallbackToEvent(eventType, callback) { - var id = b(); + var id = Symbol('id'); if (!subscriptions[eventType]) subscriptions[eventType] = {}; subscriptions[eventType][id] = callback; return { - unsubscribe: function unsub() { + unsubscribe: function unsubscribe() { delete subscriptions[eventType][id]; - if (Object.keys(subscriptions[eventType]).length === 0) delete subscriptions[eventType]; + + if (Object.getOwnPropertySymbols(subscriptions[eventType]).length === 0) { + delete subscriptions[eventType]; + } } }; }; this.publish = function publishEventWithArgs(eventType, arg) { if (!subscriptions[eventType]) return; - Object.keys(subscriptions[eventType]).forEach(function (key) { + Object.getOwnPropertySymbols(subscriptions[eventType]).forEach(function (key) { return subscriptions[eventType][key](arg); }); }; diff --git a/build/light-event-bus.min.js b/build/light-event-bus.min.js index 55b0717..78f9b9d 100644 --- a/build/light-event-bus.min.js +++ b/build/light-event-bus.min.js @@ -1 +1 @@ -(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.EVENT_BUS={}))})(this,function(a){"use strict";function c(b){return b?(b^16*Math.random()>>b/4).toString(16):"10000000-1000-4000-8000-100000000000".replace(/[018]/g,c)}function b(){var a={};this.subscribe=function(b,d){var e=c();return a[b]||(a[b]={}),a[b][e]=d,{unsubscribe:function(){delete a[b][e],0===Object.keys(a[b]).length&&delete a[b]}}},this.publish=function(b,c){a[b]&&Object.keys(a[b]).forEach(function(d){return a[b][d](c)})}}var d=new b;a.EventBus=b,a.EventBusSingleton=d,Object.defineProperty(a,"__esModule",{value:!0})}); +(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.EVENT_BUS={}))})(this,function(a){"use strict";function b(){var a={};this.subscribe=function(b,c){var d=Symbol("id");return a[b]||(a[b]={}),a[b][d]=c,{unsubscribe:function(){delete a[b][d],0===Object.getOwnPropertySymbols(a[b]).length&&delete a[b]}}},this.publish=function(b,c){a[b]&&Object.getOwnPropertySymbols(a[b]).forEach(function(d){return a[b][d](c)})}}var c=new b;a.EventBus=b,a.EventBusSingleton=c,Object.defineProperty(a,"__esModule",{value:!0})}); diff --git a/package.json b/package.json index 10dafd4..f05c414 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "light-event-bus", - "version": "1.0.0", + "version": "1.0.1", "description": "Lightweight event bus for node and the browser.", "main": "build/light-event-bus.min.js", "scripts": { diff --git a/src/EventBus.js b/src/EventBus.js index 8819ed2..1d6028b 100644 --- a/src/EventBus.js +++ b/src/EventBus.js @@ -1,16 +1,16 @@ -import uuidv4 from './UuidGenerator'; - function EventBus() { const subscriptions = { }; this.subscribe = function subscribeCallbackToEvent(eventType, callback) { - const id = uuidv4(); + const id = Symbol('id'); if (!subscriptions[eventType]) subscriptions[eventType] = { }; subscriptions[eventType][id] = callback; return { - unsubscribe: function unsub() { + unsubscribe: function unsubscribe() { delete subscriptions[eventType][id]; - if (Object.keys(subscriptions[eventType]).length === 0) delete subscriptions[eventType]; + if (Object.getOwnPropertySymbols(subscriptions[eventType]).length === 0) { + delete subscriptions[eventType]; + } }, }; }; @@ -18,7 +18,8 @@ function EventBus() { this.publish = function publishEventWithArgs(eventType, arg) { if (!subscriptions[eventType]) return; - Object.keys(subscriptions[eventType]).forEach(key => subscriptions[eventType][key](arg)); + Object.getOwnPropertySymbols(subscriptions[eventType]) + .forEach(key => subscriptions[eventType][key](arg)); }; } diff --git a/src/UuidGenerator.js b/src/UuidGenerator.js deleted file mode 100644 index cff9a10..0000000 --- a/src/UuidGenerator.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Source: https://gist.github.com/jed/982883 -*/ - -/* eslint-disable */ -export default function b(a){return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,b)} -/* eslint-enable */