From bda33ea76e86a80be2b10b5cd26ec7313458c9e3 Mon Sep 17 00:00:00 2001 From: Jim Cowart Date: Tue, 13 Sep 2011 01:13:51 -0400 Subject: [PATCH] Changed createChannel method name to channel. Rebuilt output file --- README.md | 8 ++--- build/output/browser/postal.js | 37 ++++++++++++----------- build/output/nodejs/postal.js | 37 ++++++++++++----------- spec/Postal.spec.js | 54 +++++++++++++++++----------------- src/Postal.js | 2 +- 5 files changed, 68 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 5197b6e..eba2422 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ JavaScript: // The world's simplest subscription - first let's look at the fluent configuration approach: var name = undefined, - channel = postal.createChannel("Name.Changed"); + channel = postal.channel("Name.Changed"); // subscribe channel.subscribe(function(data) { name = data.name }); // And someone publishes a first name change: @@ -31,13 +31,13 @@ JavaScript: // The # symbol represents "one word" in a topic (i.e - the text between two periods of a topic). // By subscribing to "#.Changed", the binding will match // Name.Changed & Location.Changed but *not* for Changed.Companion - var chgChannel = postal.createChannel("#.Changed"), + var chgChannel = postal.channel("#.Changed"), chgSubscription = chgChannel.subscribe(function(data) { console.log(data.type + " Changed: " + data.value); }); - postal.createChannel("Name.Changed") + postal.channel("Name.Changed") .publish({ type: "Name", value:"John Smith" }); - postal.createChannel("Location.Changed") + postal.channel("Location.Changed") .publish({ type: "Location", value: "Early 20th Century England" }); ## How can I extend it? diff --git a/build/output/browser/postal.js b/build/output/browser/postal.js index 4d12057..bac9bb4 100644 --- a/build/output/browser/postal.js +++ b/build/output/browser/postal.js @@ -72,10 +72,19 @@ SubscriptionDefinition.prototype = { }, disposeAfter: function(maxCalls) { - if(_.isNaN(maxCalls)) { - throw "The value provided to disposeAfter (maxCalls) must be a number"; + if(_.isNaN(maxCalls) || maxCalls <= 0) { + throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero."; } - this.maxCalls = maxCalls; + + var fn = this.onHandled; + var dispose = _.after(maxCalls, _.bind(function() { + this.unsubscribe(this); + }, this)); + + this.onHandled = function() { + fn.apply(this.context, arguments); + dispose(); + }; return this; }, @@ -206,16 +215,12 @@ var localBus = { subscribe: function(subDef) { var idx, found, fn; - if(subDef.maxCalls) { - fn = subDef.onHandled; - var dispose = _.after(subDef.maxCalls, _.bind(function() { - this.unsubscribe(subDef); - }, this)); - subDef.onHandled = function() { - fn.apply(subDef.context, arguments); - dispose(); - } + if(!this.subscriptions[subDef.exchange]) { + this.subscriptions[subDef.exchange] = {}; + } + if(!this.subscriptions[subDef.exchange][subDef.topic]) { + this.subscriptions[subDef.exchange][subDef.topic] = []; } idx = this.subscriptions[subDef.exchange][subDef.topic].length - 1; @@ -264,15 +269,9 @@ var postal = { resolver: bindingsResolver }, - createChannel: function(exchange, topic) { + channel: function(exchange, topic) { var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, tpc = arguments.length === 2 ? topic : exchange; - if(!this.configuration.bus.subscriptions[exch]) { - this.configuration.bus.subscriptions[exch] = {}; - } - if(!this.configuration.bus.subscriptions[exch][tpc]) { - this.configuration.bus.subscriptions[exch][tpc] = []; - } return new ChannelDefinition(exch, tpc); }, diff --git a/build/output/nodejs/postal.js b/build/output/nodejs/postal.js index a316694..38de6d1 100644 --- a/build/output/nodejs/postal.js +++ b/build/output/nodejs/postal.js @@ -70,10 +70,19 @@ SubscriptionDefinition.prototype = { }, disposeAfter: function(maxCalls) { - if(_.isNaN(maxCalls)) { - throw "The value provided to disposeAfter (maxCalls) must be a number"; + if(_.isNaN(maxCalls) || maxCalls <= 0) { + throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero."; } - this.maxCalls = maxCalls; + + var fn = this.onHandled; + var dispose = _.after(maxCalls, _.bind(function() { + this.unsubscribe(this); + }, this)); + + this.onHandled = function() { + fn.apply(this.context, arguments); + dispose(); + }; return this; }, @@ -204,16 +213,12 @@ var localBus = { subscribe: function(subDef) { var idx, found, fn; - if(subDef.maxCalls) { - fn = subDef.onHandled; - var dispose = _.after(subDef.maxCalls, _.bind(function() { - this.unsubscribe(subDef); - }, this)); - subDef.onHandled = function() { - fn.apply(subDef.context, arguments); - dispose(); - } + if(!this.subscriptions[subDef.exchange]) { + this.subscriptions[subDef.exchange] = {}; + } + if(!this.subscriptions[subDef.exchange][subDef.topic]) { + this.subscriptions[subDef.exchange][subDef.topic] = []; } idx = this.subscriptions[subDef.exchange][subDef.topic].length - 1; @@ -262,15 +267,9 @@ var postal = { resolver: bindingsResolver }, - createChannel: function(exchange, topic) { + channel: function(exchange, topic) { var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, tpc = arguments.length === 2 ? topic : exchange; - if(!this.configuration.bus.subscriptions[exch]) { - this.configuration.bus.subscriptions[exch] = {}; - } - if(!this.configuration.bus.subscriptions[exch][tpc]) { - this.configuration.bus.subscriptions[exch][tpc] = []; - } return new ChannelDefinition(exch, tpc); }, diff --git a/spec/Postal.spec.js b/spec/Postal.spec.js index f9c9d9f..db6fe88 100644 --- a/spec/Postal.spec.js +++ b/spec/Postal.spec.js @@ -5,7 +5,7 @@ QUnit.specify("postal.js", function(){ channel; describe("when creating basic subscription", function() { before(function(){ - subscription = postal.createChannel("MyExchange","MyTopic") + subscription = postal.channel("MyExchange","MyTopic") .subscribe(function() { }); sub = postal.configuration.bus.subscriptions.MyExchange.MyTopic[0]; }); @@ -41,7 +41,7 @@ QUnit.specify("postal.js", function(){ var subExistsBefore = false, subExistsAfter = true; before(function(){ - subscription = postal.createChannel("MyExchange","MyTopic") + subscription = postal.channel("MyExchange","MyTopic") .subscribe(function() { }); subExistsBefore = postal.configuration.bus.subscriptions.MyExchange.MyTopic[0] !== undefined; subscription.unsubscribe(); @@ -61,7 +61,7 @@ QUnit.specify("postal.js", function(){ var msgReceivedCnt = 0, msgData; before(function(){ - channel = postal.createChannel("MyExchange","MyTopic") + channel = postal.channel("MyExchange","MyTopic") subscription = channel.subscribe(function(data) { msgReceivedCnt++; msgData = data;}); channel.publish("Testing123"); subscription.unsubscribe(); @@ -80,7 +80,7 @@ QUnit.specify("postal.js", function(){ describe("When subscribing with a disposeAfter of 5", function(){ var msgReceivedCnt = 0; before(function(){ - channel = postal.createChannel("MyExchange","MyTopic"); + channel = postal.channel("MyExchange","MyTopic"); subscription = channel.subscribe(function(data) { msgReceivedCnt++; }) .disposeAfter(5); channel.publish("Testing123"); @@ -100,7 +100,7 @@ QUnit.specify("postal.js", function(){ describe("When subscribing and ignoring duplicates", function(){ var subInvokedCnt = 0; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic"); + channel = postal.channel("MyExchange", "MyTopic"); subscription = channel.subscribe(function(data) { subInvokedCnt++; }) .ignoreDuplicates(); channel.publish("Testing123"); @@ -124,7 +124,7 @@ QUnit.specify("postal.js", function(){ describe("When subscribing and passing onHandled callback", function(){ var whte = false; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic"); + channel = postal.channel("MyExchange", "MyTopic"); subscription = channel.subscribe(function(data) { }) .whenHandledThenExecute(function() { whte = true; }); channel.publish("Testing123"); @@ -143,7 +143,7 @@ QUnit.specify("postal.js", function(){ describe("When subscribing with one constraint returning true", function(){ var recvd = false; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic"); + channel = postal.channel("MyExchange", "MyTopic"); subscription = channel.subscribe(function(data) { recvd= true; }) .withConstraint(function() { return true; }); channel.publish("Testing123"); @@ -162,7 +162,7 @@ QUnit.specify("postal.js", function(){ describe("When subscribing with one constraint returning false", function(){ var recvd = false; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic"); + channel = postal.channel("MyExchange", "MyTopic"); subscription = channel.subscribe(function(data) { recvd= true; }) .withConstraint(function() { return false; }); channel.publish("Testing123"); @@ -181,7 +181,7 @@ QUnit.specify("postal.js", function(){ describe("When subscribing with multiple constraints returning true", function(){ var recvd = false; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic"); + channel = postal.channel("MyExchange", "MyTopic"); subscription = channel.subscribe(function(data) { recvd= true; }) .withConstraints([function() { return true; }, function() { return true; }, @@ -202,7 +202,7 @@ QUnit.specify("postal.js", function(){ describe("When subscribing with multiple constraints and one returning false", function(){ var recvd = false; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic"); + channel = postal.channel("MyExchange", "MyTopic"); subscription = channel.subscribe(function(data) { recvd= true; }) .withConstraints([function() { return true; }, function() { return false; }, @@ -228,7 +228,7 @@ QUnit.specify("postal.js", function(){ } }; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic"); + channel = postal.channel("MyExchange", "MyTopic"); subscription = channel.subscribe(function(data) { this.increment(); }) .withContext(obj); channel.publish("Testing123"); @@ -243,9 +243,9 @@ QUnit.specify("postal.js", function(){ describe("When subscribing with a hierarchical binding, no wildcards", function(){ var count = 0, channelB, channelC; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic.MiddleTopic.SubTopic"); - channelB = postal.createChannel("MyExchange", "MyTopic.MiddleTopic"); - channelC = postal.createChannel("MyExchange", "MyTopic.MiddleTopic.SubTopic.YetAnother"); + channel = postal.channel("MyExchange", "MyTopic.MiddleTopic.SubTopic"); + channelB = postal.channel("MyExchange", "MyTopic.MiddleTopic"); + channelC = postal.channel("MyExchange", "MyTopic.MiddleTopic.SubTopic.YetAnother"); subscription = channel.subscribe(function(data) { count++; }); channel.publish("Testing123"); channelB.publish("Testing123"); @@ -262,10 +262,10 @@ QUnit.specify("postal.js", function(){ describe("When subscribing with a hierarchical binding, using #", function(){ var count = 0, channelB, channelC, channelD; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic.#.SubTopic"); - channelB = postal.createChannel("MyExchange", "MyTopic.MiddleTopic"); - channelC = postal.createChannel("MyExchange", "MyTopic.MiddleTopic.SubTopic"); - channelD = postal.createChannel("MyExchange", "MyTopic.MiddleTopic.SubTopic.YetAnother"); + channel = postal.channel("MyExchange", "MyTopic.#.SubTopic"); + channelB = postal.channel("MyExchange", "MyTopic.MiddleTopic"); + channelC = postal.channel("MyExchange", "MyTopic.MiddleTopic.SubTopic"); + channelD = postal.channel("MyExchange", "MyTopic.MiddleTopic.SubTopic.YetAnother"); subscription = channel.subscribe(function(data) { count++; }); channelC.publish({exchange: "MyExchange", topic: "MyTopic.MiddleTopic.SubTopic", data: "Testing123"}); channelB.publish({exchange: "MyExchange", topic: "MyTopic.MiddleTopic", data: "Testing123"}); @@ -282,10 +282,10 @@ QUnit.specify("postal.js", function(){ describe("When subscribing with a hierarchical binding, using *", function(){ var count = 0, channelB, channelC, channelD; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic.MiddleTopic.*"); - channelB = postal.createChannel("MyExchange", "MyTopic.MiddleTopic"); - channelC = postal.createChannel("MyExchange", "MyTopic.MiddleTopic.SubTopic"); - channelD = postal.createChannel("MyExchange", "MyTopic.MiddleTopic.SubTopic.YetAnother"); + channel = postal.channel("MyExchange", "MyTopic.MiddleTopic.*"); + channelB = postal.channel("MyExchange", "MyTopic.MiddleTopic"); + channelC = postal.channel("MyExchange", "MyTopic.MiddleTopic.SubTopic"); + channelD = postal.channel("MyExchange", "MyTopic.MiddleTopic.SubTopic.YetAnother"); subscription = channel.subscribe(function(data) { count++; }); channelC.publish("Testing123"); @@ -303,11 +303,11 @@ QUnit.specify("postal.js", function(){ describe("When subscribing with a hierarchical binding, using # and *", function(){ var count = 0, channelB, channelC, channelD, channelE; before(function(){ - channel = postal.createChannel("MyExchange", "MyTopic.#.*"); - channelB = postal.createChannel("MyExchange", "MyTopic.MiddleTopic"); - channelC = postal.createChannel("MyExchange", "MyTopic.MiddleTopic.SubTopic"); - channelD = postal.createChannel("MyExchange", "MyTopic.MiddleTopic.SubTopic.YetAnother"); - channelE = postal.createChannel("MyExchange", "OtherTopic.MiddleTopic.SubTopic.YetAnother"); + channel = postal.channel("MyExchange", "MyTopic.#.*"); + channelB = postal.channel("MyExchange", "MyTopic.MiddleTopic"); + channelC = postal.channel("MyExchange", "MyTopic.MiddleTopic.SubTopic"); + channelD = postal.channel("MyExchange", "MyTopic.MiddleTopic.SubTopic.YetAnother"); + channelE = postal.channel("MyExchange", "OtherTopic.MiddleTopic.SubTopic.YetAnother"); subscription = channel.subscribe(function(data) { count++; }); channelC.publish({exchange: "MyExchange", topic: "MyTopic.MiddleTopic.SubTopic", data: "Testing123"}); diff --git a/src/Postal.js b/src/Postal.js index fffce29..0b38c7e 100644 --- a/src/Postal.js +++ b/src/Postal.js @@ -4,7 +4,7 @@ var postal = { resolver: bindingsResolver }, - createChannel: function(exchange, topic) { + channel: function(exchange, topic) { var exch = arguments.length === 2 ? exchange : DEFAULT_EXCHANGE, tpc = arguments.length === 2 ? topic : exchange; return new ChannelDefinition(exch, tpc);