Skip to content

Commit

Permalink
replaces UUIs with symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
PierfrancescoSoffritti committed Jan 25, 2019
1 parent f87040e commit f82f6b8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 29 deletions.
21 changes: 7 additions & 14 deletions build/light-event-bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};
Expand Down
2 changes: 1 addition & 1 deletion build/light-event-bus.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
13 changes: 7 additions & 6 deletions src/EventBus.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
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];
}
},
};
};

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));
};
}

Expand Down
7 changes: 0 additions & 7 deletions src/UuidGenerator.js

This file was deleted.

0 comments on commit f82f6b8

Please sign in to comment.