Skip to content

Commit

Permalink
Add a subscription after executed only once
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryuurock committed Jan 16, 2018
1 parent 3a6a6f0 commit e3da70c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
54 changes: 15 additions & 39 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,44 +156,6 @@ https://github.com/mroderick/PubSubJS
* Subscribes the passed function to the passed message. Every returned token is unique and should be stored if
* you need to unsubscribe
**/
<<<<<<< HEAD
PubSub.subscribe = function( message, func ){
if ( typeof func !== 'function'){
return false;
}

// message is not registered yet
if ( !messages.hasOwnProperty( message ) ){
messages[message] = {};
}

// forcing token as String, to allow for future expansions without breaking usage
// and allow for easy use as key names for the 'messages' object
var token = 'uid_' + String(++lastUid);
messages[message][token] = func;

// return token for unsubscribing
return token;
};

/**
* PubSub.subscribeOnce( message, func ) -> PubSub
* - message (String): The message to subscribe to
* - func (Function): The function to call when a new message is published
* Subscribes the passed function to the passed message once
**/
PubSub.subscribeOnce = function( message, func ){
let token = pubsub.on( event, function(){
// before func apply, unsubscribe message
pubsub.unsubscribe( token );
func.apply( this, arguments );
} );

return PubSub;
}

/* Public: Clears all subscriptions
=======
PubSub.subscribe = function( message, func ){
if ( typeof func !== 'function'){
return false;
Expand All @@ -213,8 +175,22 @@ https://github.com/mroderick/PubSubJS
return token;
};

/**
* PubSub.subscribeOnce( message, func ) -> PubSub
* - message (String): The message to subscribe to
* - func (Function): The function to call when a new message is published
* Subscribes the passed function to the passed message once
**/
PubSub.subscribeOnce = function( message, func ){
var token = PubSub.subscribe( message, function(){
// before func apply, unsubscribe message
PubSub.unsubscribe( token );
func.apply( this, arguments );
});
return PubSub;
};

/* Public: Clears all subscriptions
>>>>>>> remote-code/master
*/
PubSub.clearAllSubscriptions = function clearAllSubscriptions(){
messages = {};
Expand Down
32 changes: 32 additions & 0 deletions test/test-subscribeOnce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var PubSub = require('../src/pubsub'),
TestHelper = require('../test/helper'),
assert = require('referee').assert,
sinon = require('sinon');


describe( 'subscribeOnce method', function() {

it( 'should return PubSub', function() {
var func = function(){ return undefined; },
message = TestHelper.getUniqueString(),
pubSub = PubSub.subscribeOnce( message , func );
assert.same( pubSub, PubSub );
} );

it( 'must be executed only once', function() {

var topic = TestHelper.getUniqueString(),
spy = sinon.spy();

PubSub.subscribeOnce( topic, spy );
for ( var i = 0; i < 3; i++ ) {
PubSub.publishSync( topic, TestHelper.getUniqueString() );
}

assert( spy.calledOnce );

} );

} );

0 comments on commit e3da70c

Please sign in to comment.