From e3da70cf3f709c880a57ecf76934d770a76e994b Mon Sep 17 00:00:00 2001 From: Sherlock Date: Tue, 16 Jan 2018 18:04:38 +0800 Subject: [PATCH] Add a subscription after executed only once --- src/pubsub.js | 54 +++++++++++--------------------------- test/test-subscribeOnce.js | 32 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 test/test-subscribeOnce.js diff --git a/src/pubsub.js b/src/pubsub.js index 51a5e6e..367b6e3 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -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; @@ -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 = {}; diff --git a/test/test-subscribeOnce.js b/test/test-subscribeOnce.js new file mode 100644 index 0000000..ab5c9f0 --- /dev/null +++ b/test/test-subscribeOnce.js @@ -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 ); + + } ); + +} ); \ No newline at end of file