From 99fe511bfacc301689c4d4e54677522c2710bde3 Mon Sep 17 00:00:00 2001 From: Oleg Baltag Date: Thu, 23 Sep 2021 20:53:22 +0300 Subject: [PATCH 1/2] fixed symbols --- src/pubsub.js | 4 ---- test/test-symbol.js | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/pubsub.js b/src/pubsub.js index 16e1730..7453c51 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -125,8 +125,6 @@ } function publish( message, data, sync, immediateExceptions ){ - message = (typeof message === 'symbol') ? message.toString() : message; - var deliver = createDeliveryFunction( message, data, immediateExceptions ), hasSubscribers = messageHasSubscribers( message ); @@ -179,8 +177,6 @@ return false; } - message = (typeof message === 'symbol') ? message.toString() : message; - // message is not registered yet if ( !Object.prototype.hasOwnProperty.call( messages, message ) ){ messages[message] = {}; diff --git a/test/test-symbol.js b/test/test-symbol.js index b7694fc..24d9a20 100644 --- a/test/test-symbol.js +++ b/test/test-symbol.js @@ -18,4 +18,19 @@ describe( 'subscribe and publish', function() { assert( PubSub.publish( MESSAGE ), true ); }); + it("should call func1 only once", function () { + var MESSAGE1 = Symbol("MESSAGE"); + var MESSAGE2 = Symbol("MESSAGE"); + var count = 0; + var func1 = function (msg) { + count++; + assert(count === 1, true); + return undefined; + }; + + PubSub.subscribe(MESSAGE1, func1); + + PubSub.publish(MESSAGE1); + PubSub.publish(MESSAGE2); + }); }); From f9d3491c81884ac1ab4557ecff477ac6e93adbf7 Mon Sep 17 00:00:00 2001 From: Oleg Baltag Date: Thu, 23 Sep 2021 21:02:16 +0300 Subject: [PATCH 2/2] added symbol annotation --- src/pubsub.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pubsub.js b/src/pubsub.js index 7453c51..dd7b129 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -144,7 +144,7 @@ * Publishes the message, passing the data to it's subscribers * @function * @alias publish - * @param { String } message The message to publish + * @param { String | Symbol } message The message to publish * @param {} data The data to pass to subscribers * @return { Boolean } */ @@ -156,7 +156,7 @@ * Publishes the message synchronously, passing the data to it's subscribers * @function * @alias publishSync - * @param { String } message The message to publish + * @param { String | Symbol } message The message to publish * @param {} data The data to pass to subscribers * @return { Boolean } */ @@ -168,7 +168,7 @@ * Subscribes the passed function to the passed message. Every returned token is unique and should be stored if you need to unsubscribe * @function * @alias subscribe - * @param { String } message The message to subscribe to + * @param { String | Symbol } message The message to subscribe to * @param { Function } func The function to call when a new message is published * @return { String } */ @@ -199,7 +199,7 @@ * Subscribes the passed function to the passed message once * @function * @alias subscribeOnce - * @param { String } message The message to subscribe to + * @param { String | Symbol } message The message to subscribe to * @param { Function } func The function to call when a new message is published * @return { PubSub } */