Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbols fix #211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -146,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 }
*/
Expand All @@ -158,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 }
*/
Expand All @@ -170,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 }
*/
Expand All @@ -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] = {};
Expand All @@ -203,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 }
*/
Expand Down
15 changes: 15 additions & 0 deletions test/test-symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});