From b48bdfc27662d279c5faa12e00257691adfdbcaf Mon Sep 17 00:00:00 2001 From: Jim Cowart Date: Tue, 20 Mar 2012 02:04:35 -0400 Subject: [PATCH] Updated README, consolidated standard and AMD build processes --- README.md | 75 ++-- build-all.sh | 16 +- build-browser-amd-diags.json | 12 - build-browser-amd.json | 12 - build-browser-standard.json | 6 +- build-node-diags.json | 6 +- build-node.json | 6 +- .../amd/js/libs/postal/postal.diagnostics.js | 53 ++- example/amd/js/libs/postal/postal.js | 266 +++++++------ example/standard/js/postal.diagnostics.js | 51 ++- example/standard/js/postal.js | 266 +++++++------ lib/browser/amd/postal.diagnostics.js | 20 - lib/browser/amd/postal.diagnostics.min.gz.js | Bin 243 -> 0 bytes lib/browser/amd/postal.diagnostics.min.js | 1 - lib/browser/amd/postal.js | 356 ------------------ lib/browser/amd/postal.min.gz.js | Bin 1538 -> 0 bytes lib/browser/amd/postal.min.js | 1 - lib/browser/standard/postal.diagnostics.js | 51 ++- .../standard/postal.diagnostics.min.gz.js | Bin 220 -> 307 bytes .../standard/postal.diagnostics.min.js | 2 +- lib/browser/standard/postal.js | 266 +++++++------ lib/browser/standard/postal.min.gz.js | Bin 1515 -> 1615 bytes lib/browser/standard/postal.min.js | 2 +- lib/node/postal.diagnostics.js | 39 +- lib/node/postal.js | 252 +++++++------ spec/Postal.spec.js | 1 - src/diags/postal.diagnostics.js | 51 ++- src/diags/postal.diagnostics.node.js | 21 ++ src/main/ChannelDefinition.js | 2 +- src/main/VersionHeader.js | 2 +- src/main/postal.js | 18 +- src/main/postal.node.js | 12 + 32 files changed, 810 insertions(+), 1056 deletions(-) delete mode 100644 build-browser-amd-diags.json delete mode 100644 build-browser-amd.json delete mode 100644 lib/browser/amd/postal.diagnostics.js delete mode 100644 lib/browser/amd/postal.diagnostics.min.gz.js delete mode 100644 lib/browser/amd/postal.diagnostics.min.js delete mode 100644 lib/browser/amd/postal.js delete mode 100644 lib/browser/amd/postal.min.gz.js delete mode 100644 lib/browser/amd/postal.min.js create mode 100644 src/diags/postal.diagnostics.node.js create mode 100644 src/main/postal.node.js diff --git a/README.md b/README.md index b5005c8..7ee9f12 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,16 @@ JavaScript: ```javascript // The world's simplest subscription -var channel = postal.channel("Name.Changed"); +// doesn't specify a channel name, so it defaults to "/" (DEFAULT_CHANNEL) +var channel = postal.channel( { topic: "Name.Changed" } ); // subscribe -var subscription = channel.subscribe(function(data) { $("#example1").html("Name: " + data.name); }); +var subscription = channel.subscribe( function( data, envelope ) { + $( "#example1" ).html( "Name: " + data.name ); +}); // And someone publishes a first name change: -channel.publish({ name: "Dr. Who" }); +channel.publish( { name: "Dr. Who" } ); subscription.unsubscribe(); ``` @@ -37,14 +40,14 @@ subscription.unsubscribe(); 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` ```javascript -var hashChannel = postal.channel("#.Changed"), - chgSubscription = hashChannel.subscribe(function(data) { - $('
  • ' + data.type + " Changed: " + data.value + '
  • ').appendTo("#example2"); +var hashChannel = postal.channel( { topic: "#.Changed" } ), + chgSubscription = hashChannel.subscribe( function( data ) { + $( '
  • ' + data.type + " Changed: " + data.value + '
  • ' ).appendTo( "#example2" ); }); -postal.channel("Name.Changed") - .publish({ type: "Name", value:"John Smith" }); -postal.channel("Location.Changed") - .publish({ type: "Location", value: "Early 20th Century England" }); +postal.channel( { topic: "Name.Changed" } ) + .publish( { type: "Name", value:"John Smith" } ); +postal.channel( "Location.Changed" ) + .publish( { type: "Location", value: "Early 20th Century England" } ); chgSubscription.unsubscribe(); ``` @@ -53,40 +56,38 @@ chgSubscription.unsubscribe(); The `*` symbol represents any number of characters/words in a topic string. By subscribing to ``"DrWho.*.Changed"``, the binding will match `DrWho.NinthDoctor.Companion.Changed` & `DrWho.Location.Changed` but *not* `Changed` ```javascript -var starChannel = postal.channel("DrWho.*.Changed"), - starSubscription = starChannel.subscribe(function(data) { - $('
  • ' + data.type + " Changed: " + data.value + '
  • ').appendTo("#example3"); +var starChannel = postal.channel( { channel: "Doctor.Who", topic: "DrWho.*.Changed" } ), + starSubscription = starChannel.subscribe( function( data ) { + $( '
  • ' + data.type + " Changed: " + data.value + '
  • ' ).appendTo( "#example3" ); }); -postal.channel("DrWho.NinthDoctor.Companion.Changed") - .publish({ type: "Name", value:"Rose" }); -postal.channel("DrWho.TenthDoctor.Companion.Changed") - .publish({ type: "Name", value:"Martha" }); -postal.channel("DrWho.Eleventh.Companion.Changed") - .publish({ type: "Name", value:"Amy" }); -postal.channel("DrWho.Location.Changed") - .publish({ type: "Location", value: "The Library" }); -postal.channel("TheMaster.DrumBeat.Changed") - .publish({ type: "DrumBeat", value: "This won't trigger any subscriptions" }); -postal.channel("Changed") - .publish({ type: "Useless", value: "This won't trigger any subscriptions either" }); +// demonstrating how we're re-using the channel delcared above to publish, but overriding the topic in the second argument +starChannel.publish( { type: "Name", value:"Rose" }, { topic: "DrWho.NinthDoctor.Companion.Changed" } ); +starChannel.publish( { type: "Name", value:"Martha" }, { topic: "DrWho.TenthDoctor.Companion.Changed" } ); +starChannel.publish( { type: "Name", value:"Amy" }, { topic: "DrWho.Eleventh.Companion.Changed" } ); +starChannel.publish( { type: "Location", value: "The Library" }, { topic: "DrWho.Location.Changed" } ); +starChannel.publish( { type: "DrumBeat", value: "This won't trigger any subscriptions" }, { topic: "TheMaster.DrumBeat.Changed" } ); +starChannel.publish( { type: "Useless", value: "This won't trigger any subscriptions either" }, { topic: "Changed" } ); + starSubscription.unsubscribe(); ``` ### Applying ignoreDuplicates to a subscription ```javascript -var dupChannel = postal.channel("WeepingAngel.*"), - dupSubscription = dupChannel.subscribe(function(data) { - $('
  • ' + data.value + '
  • ').appendTo("#example4"); +var dupChannel = postal.channel( { topic: "WeepingAngel.*" } ), + dupSubscription = dupChannel.subscribe( function( data ) { + $( '
  • ' + data.value + '
  • ' ).appendTo( "#example4" ); }).ignoreDuplicates(); -postal.channel("WeepingAngel.DontBlink") - .publish({ value:"Don't Blink" }); -postal.channel("WeepingAngel.DontBlink") - .publish({ value:"Don't Blink" }); -postal.channel("WeepingAngel.DontEvenBlink") - .publish({ value:"Don't Even Blink" }); -postal.channel("WeepingAngel.DontBlink") - .publish({ value:"Don't Close Your Eyes" }); +// demonstrating multiple channels per topic being used +// You can do it this way if you like, but the example above has nicer syntax (and less overhead) +postal.channel( { topic: "WeepingAngel.DontBlink" } ) + .publish( { value:"Don't Blink" } ); +postal.channel( { topic: "WeepingAngel.DontBlink" } ) + .publish( { value:"Don't Blink" } ); +postal.channel( { topic: "WeepingAngel.DontEvenBlink" } ) + .publish( { value:"Don't Even Blink" } ); +postal.channel( { topic: "WeepingAngel.DontBlink" } ) + .publish( { value:"Don't Close Your Eyes" } ); dupSubscription.unsubscribe(); ``` @@ -104,7 +105,5 @@ Please - by all means! While I hope the API is relatively stable, I'm open to p ## Roadmap for the Future Here's where Postal is headed: -* The original proof-of-concept version of postal supported the ability to capture messages and replay them. This functionality will be added soon. -* The ability to 'join' two (or more) subscriptions, so that the original subscription will only fire once the second "joined" subscription has also fired. Think of it as a message-based version of a compound promise. * I haven't yet thoroughly tested Postal on Node.js - that is high on my list as well. * What else would you like to see? \ No newline at end of file diff --git a/build-all.sh b/build-all.sh index 98f53cb..fb02833 100755 --- a/build-all.sh +++ b/build-all.sh @@ -2,12 +2,14 @@ anvil -b build-browser-standard.json anvil -b build-browser-standard-diags.json -anvil -b build-browser-amd.json -anvil -b build-browser-amd-diags.json -anvil -b build-node.json -anvil -b build-node-diags.json -cp ./lib/browser/amd/postal.js ./example/amd/js/libs/postal/ -cp ./lib/browser/amd/postal.diagnostics.js ./example/amd/js/libs/postal/ +cp ./lib/browser/standard/postal.js ./example/amd/js/libs/postal/ +cp ./lib/browser/standard/postal.diagnostics.js ./example/amd/js/libs/postal/ cp ./lib/browser/standard/postal.js ./example/standard/js/ -cp ./lib/browser/standard/postal.diagnostics.js ./example/standard/js/ \ No newline at end of file +cp ./lib/browser/standard/postal.diagnostics.js ./example/standard/js/ + +mv ./lib/browser/standard/postal.node.js ./lib/node/postal.js +rm ./lib/browser/standard/postal.node* + +mv ./lib/browser/standard/postal.diagnostics.node.js ./lib/node/postal.diagnostics.js +rm ./lib/browser/standard/postal.diagnostics.node* \ No newline at end of file diff --git a/build-browser-amd-diags.json b/build-browser-amd-diags.json deleted file mode 100644 index 42430de..0000000 --- a/build-browser-amd-diags.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "source": "src/diags", - "output": "lib/browser/amd", - "lint": {}, - "uglify": {}, - "gzip": {}, - "extensions": { "uglify": "min", "gzip": "gz" }, - "wrap": { - "prefix": "define(['postal', 'underscore'], function(postal, _) {", - "suffix": "});" - } -} \ No newline at end of file diff --git a/build-browser-amd.json b/build-browser-amd.json deleted file mode 100644 index 6817679..0000000 --- a/build-browser-amd.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "source": "src/main", - "output": "lib/browser/amd", - "lint": {}, - "uglify": {}, - "gzip": {}, - "extensions": { "uglify": "min", "gzip": "gz" }, - "wrap": { - "prefix": "define(['underscore'], function(_) {", - "suffix": "return postal; });" - } -} \ No newline at end of file diff --git a/build-browser-standard.json b/build-browser-standard.json index cb413ca..63711c5 100644 --- a/build-browser-standard.json +++ b/build-browser-standard.json @@ -4,9 +4,5 @@ "lint": {}, "uglify": {}, "gzip": {}, - "extensions": { "uglify": "min", "gzip": "gz" }, - "wrap": { - "prefix": "(function(global, undefined) {", - "suffix": "global.postal = postal; })(window);" - } + "extensions": { "uglify": "min", "gzip": "gz" } } \ No newline at end of file diff --git a/build-node-diags.json b/build-node-diags.json index 5085ed7..20f1560 100644 --- a/build-node-diags.json +++ b/build-node-diags.json @@ -1,9 +1,5 @@ { "source": "src/diags", "output": "lib/node", - "lint": {}, - "wrap": { - "prefix": "module.exports = function(postal) {", - "suffix": "};" - } + "lint": {} } \ No newline at end of file diff --git a/build-node.json b/build-node.json index 7b4d7b4..c7f9289 100644 --- a/build-node.json +++ b/build-node.json @@ -1,9 +1,5 @@ { "source": "src/main", "output": "lib/node", - "lint": {}, - "wrap": { - "prefix": "var _ = require('underscore');", - "suffix": "module.exports = postal;" - } + "lint": {} } \ No newline at end of file diff --git a/example/amd/js/libs/postal/postal.diagnostics.js b/example/amd/js/libs/postal/postal.diagnostics.js index 9f9ee66..5defd32 100644 --- a/example/amd/js/libs/postal/postal.diagnostics.js +++ b/example/amd/js/libs/postal/postal.diagnostics.js @@ -1,20 +1,33 @@ -define(['postal', 'underscore'], function(postal, _) { -postal.addWireTap(function(data, envelope) { - var all = _.extend(envelope, { data: data }); - if(!JSON) { - throw "This browser or environment does not provide JSON support"; - } - try { - console.log(JSON.stringify(all)); - } - catch(exception) { - try { - all.data = "ERROR: " + exception.message; - console.log(JSON.stringify(all)); - } - catch(ex) { - console.log("Unable to parse data to JSON: " + exception); - } - } -}); -}); \ No newline at end of file +(function(root, doc, factory) { + if (typeof define === "function" && define.amd) { + // AMD. Register as an anonymous module. + define(["postal", "underscore"], function(postal, _) { + return factory(postal, _, root, doc); + }); + } else { + // Browser globals + factory(root.postal, root._, root, doc); + } +}(this, document, function(postal, _, global, document, undefined) { + + // this returns a callback that, if invoked, removes the wireTap + return postal.addWireTap(function(data, envelope) { + var all = _.extend(envelope, { data: data }); + if(!JSON) { + throw "This browser or environment does not provide JSON support"; + } + try { + console.log(JSON.stringify(all)); + } + catch(exception) { + try { + all.data = "ERROR: " + exception.message; + console.log(JSON.stringify(all)); + } + catch(ex) { + console.log("Unable to parse data to JSON: " + exception); + } + } + }); + +})); \ No newline at end of file diff --git a/example/amd/js/libs/postal/postal.js b/example/amd/js/libs/postal/postal.js index f8037cc..7fe0a44 100644 --- a/example/amd/js/libs/postal/postal.js +++ b/example/amd/js/libs/postal/postal.js @@ -1,4 +1,3 @@ -define(['underscore'], function(_) { /* postal.js Author: Jim Cowart @@ -6,6 +5,18 @@ define(['underscore'], function(_) { Version 0.4.0 */ +(function(root, doc, factory) { + if (typeof define === "function" && define.amd) { + // AMD. Register as an anonymous module. + define(["underscore"], function(_) { + return factory(_, root, doc); + }); + } else { + // Browser globals + factory(root._, root, doc); + } +}(this, document, function(_, global, document, undefined) { + var DEFAULT_CHANNEL = "/", DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, @@ -34,8 +45,14 @@ var ChannelDefinition = function(channelName, defaultTopic) { }; ChannelDefinition.prototype = { - subscribe: function(callback) { - return new SubscriptionDefinition(this.channel, this.topic, callback); + subscribe: function() { + var len = arguments.length; + if(len === 1) { + return new SubscriptionDefinition(this.channel, this.topic, arguments[0]); + } + else if (len === 2) { + return new SubscriptionDefinition(this.channel, arguments[0], arguments[1]); + } }, publish: function(data, envelope) { @@ -43,142 +60,140 @@ ChannelDefinition.prototype = { env.channel = this.channel; env.timeStamp = new Date(); env.topic = env.topic || this.topic; - postal.configuration.bus.publish(env, data); + postal.configuration.bus.publish(data, env); } }; var SubscriptionDefinition = function(channel, topic, callback) { - this.channel = channel; - this.topic = topic; - this.callback = callback; - this.priority = DEFAULT_PRIORITY; - this.constraints = new Array(0); - this.maxCalls = DEFAULT_DISPOSEAFTER; - this.onHandled = NO_OP; - this.context = null; + this.channel = channel; + this.topic = topic; + this.callback = callback; + this.priority = DEFAULT_PRIORITY; + this.constraints = new Array(0); + this.maxCalls = DEFAULT_DISPOSEAFTER; + this.onHandled = NO_OP; + this.context = null; + postal.publish({ + event: "subscription.created", + channel: channel, + topic: topic + },{ + channel: SYSTEM_CHANNEL, + topic: "subscription.created" + }); postal.configuration.bus.subscribe(this); - postal.publish({ - channel: SYSTEM_CHANNEL, - topic: "subscription.created" - }, - { - event: "subscription.created", - channel: channel, - topic: topic - }); }; SubscriptionDefinition.prototype = { - unsubscribe: function() { - postal.configuration.bus.unsubscribe(this); - postal.publish({ - channel: SYSTEM_CHANNEL, - topic: "subscription.removed" - }, - { - event: "subscription.removed", - channel: this.channel, - topic: this.topic - }); - }, + unsubscribe: function() { + postal.configuration.bus.unsubscribe(this); + postal.publish({ + event: "subscription.removed", + channel: this.channel, + topic: this.topic + },{ + channel: SYSTEM_CHANNEL, + topic: "subscription.removed" + }); + }, - defer: function() { - var fn = this.callback; - this.callback = function(data) { - setTimeout(fn,0,data); - }; - return this; - }, + defer: function() { + var fn = this.callback; + this.callback = function(data) { + setTimeout(fn,0,data); + }; + return this; + }, - disposeAfter: function(maxCalls) { - if(_.isNaN(maxCalls) || maxCalls <= 0) { - throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero."; - } + disposeAfter: function(maxCalls) { + if(_.isNaN(maxCalls) || maxCalls <= 0) { + throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero."; + } - var fn = this.onHandled; - var dispose = _.after(maxCalls, _.bind(function() { - this.unsubscribe(this); - }, this)); + 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; - }, + this.onHandled = function() { + fn.apply(this.context, arguments); + dispose(); + }; + return this; + }, - ignoreDuplicates: function() { - this.withConstraint(new DistinctPredicate()); - return this; - }, + ignoreDuplicates: function() { + this.withConstraint(new DistinctPredicate()); + return this; + }, - whenHandledThenExecute: function(callback) { - if(! _.isFunction(callback)) { - throw "Value provided to 'whenHandledThenExecute' must be a function"; - } - this.onHandled = callback; - return this; - }, + whenHandledThenExecute: function(callback) { + if(! _.isFunction(callback)) { + throw "Value provided to 'whenHandledThenExecute' must be a function"; + } + this.onHandled = callback; + return this; + }, - withConstraint: function(predicate) { - if(! _.isFunction(predicate)) { - throw "Predicate constraint must be a function"; - } - this.constraints.push(predicate); - return this; - }, + withConstraint: function(predicate) { + if(! _.isFunction(predicate)) { + throw "Predicate constraint must be a function"; + } + this.constraints.push(predicate); + return this; + }, - withConstraints: function(predicates) { - var self = this; - if(_.isArray(predicates)) { - _.each(predicates, function(predicate) { self.withConstraint(predicate); } ); - } - return self; - }, + withConstraints: function(predicates) { + var self = this; + if(_.isArray(predicates)) { + _.each(predicates, function(predicate) { self.withConstraint(predicate); } ); + } + return self; + }, - withContext: function(context) { - this.context = context; - return this; - }, + withContext: function(context) { + this.context = context; + return this; + }, - withDebounce: function(milliseconds) { - if(_.isNaN(milliseconds)) { - throw "Milliseconds must be a number"; - } - var fn = this.callback; - this.callback = _.debounce(fn, milliseconds); - return this; - }, + withDebounce: function(milliseconds) { + if(_.isNaN(milliseconds)) { + throw "Milliseconds must be a number"; + } + var fn = this.callback; + this.callback = _.debounce(fn, milliseconds); + return this; + }, - withDelay: function(milliseconds) { - if(_.isNaN(milliseconds)) { - throw "Milliseconds must be a number"; - } - var fn = this.callback; - this.callback = function(data) { - setTimeout(fn, milliseconds, data); - }; - return this; - }, + withDelay: function(milliseconds) { + if(_.isNaN(milliseconds)) { + throw "Milliseconds must be a number"; + } + var fn = this.callback; + this.callback = function(data) { + setTimeout(fn, milliseconds, data); + }; + return this; + }, - withPriority: function(priority) { - if(_.isNaN(priority)) { - throw "Priority must be a number"; - } - this.priority = priority; - return this; - }, + withPriority: function(priority) { + if(_.isNaN(priority)) { + throw "Priority must be a number"; + } + this.priority = priority; + return this; + }, - withThrottle: function(milliseconds) { - if(_.isNaN(milliseconds)) { - throw "Milliseconds must be a number"; - } - var fn = this.callback; - this.callback = _.throttle(fn, milliseconds); - return this; - } + withThrottle: function(milliseconds) { + if(_.isNaN(milliseconds)) { + throw "Milliseconds must be a number"; + } + var fn = this.callback; + this.callback = _.throttle(fn, milliseconds); + return this; + } }; var bindingsResolver = { @@ -209,9 +224,9 @@ var localBus = { wireTaps: new Array(0), - publish: function(envelope, data) { + publish: function(data, envelope) { _.each(this.wireTaps,function(tap) { - tap(envelope, data); + tap(data, envelope); }); _.each(this.subscriptions[envelope.channel], function(topic) { @@ -279,14 +294,14 @@ var localBus = { }; var publishPicker = { - "2" : function(envelope, payload) { + "2" : function(data, envelope) { if(!envelope.channel) { envelope.channel = DEFAULT_CHANNEL; } - postal.configuration.bus.publish(envelope, payload); + postal.configuration.bus.publish(data, envelope); }, "3" : function(channel, topic, payload) { - postal.configuration.bus.publish({ channel: channel, topic: topic }, payload); + postal.configuration.bus.publish(payload, { channel: channel, topic: topic }); } }; @@ -343,7 +358,7 @@ var postal = { var newEnv = env; newEnv.topic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; newEnv.channel = destChannel; - postal.publish(newEnv, msg); + postal.publish(msg, newEnv); } }) ); @@ -352,5 +367,8 @@ var postal = { return result; } }; - -return postal; }); \ No newline at end of file + + + global.postal = postal; + return postal; +})); \ No newline at end of file diff --git a/example/standard/js/postal.diagnostics.js b/example/standard/js/postal.diagnostics.js index f1d0516..5defd32 100644 --- a/example/standard/js/postal.diagnostics.js +++ b/example/standard/js/postal.diagnostics.js @@ -1,18 +1,33 @@ -postal.addWireTap(function(data, envelope) { - var all = _.extend(envelope, { data: data }); - if(!JSON) { - throw "This browser or environment does not provide JSON support"; - } - try { - console.log(JSON.stringify(all)); - } - catch(exception) { - try { - all.data = "ERROR: " + exception.message; - console.log(JSON.stringify(all)); - } - catch(ex) { - console.log("Unable to parse data to JSON: " + exception); - } - } -}); \ No newline at end of file +(function(root, doc, factory) { + if (typeof define === "function" && define.amd) { + // AMD. Register as an anonymous module. + define(["postal", "underscore"], function(postal, _) { + return factory(postal, _, root, doc); + }); + } else { + // Browser globals + factory(root.postal, root._, root, doc); + } +}(this, document, function(postal, _, global, document, undefined) { + + // this returns a callback that, if invoked, removes the wireTap + return postal.addWireTap(function(data, envelope) { + var all = _.extend(envelope, { data: data }); + if(!JSON) { + throw "This browser or environment does not provide JSON support"; + } + try { + console.log(JSON.stringify(all)); + } + catch(exception) { + try { + all.data = "ERROR: " + exception.message; + console.log(JSON.stringify(all)); + } + catch(ex) { + console.log("Unable to parse data to JSON: " + exception); + } + } + }); + +})); \ No newline at end of file diff --git a/example/standard/js/postal.js b/example/standard/js/postal.js index 66cd36a..7fe0a44 100644 --- a/example/standard/js/postal.js +++ b/example/standard/js/postal.js @@ -1,4 +1,3 @@ -(function(global, undefined) { /* postal.js Author: Jim Cowart @@ -6,6 +5,18 @@ Version 0.4.0 */ +(function(root, doc, factory) { + if (typeof define === "function" && define.amd) { + // AMD. Register as an anonymous module. + define(["underscore"], function(_) { + return factory(_, root, doc); + }); + } else { + // Browser globals + factory(root._, root, doc); + } +}(this, document, function(_, global, document, undefined) { + var DEFAULT_CHANNEL = "/", DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, @@ -34,8 +45,14 @@ var ChannelDefinition = function(channelName, defaultTopic) { }; ChannelDefinition.prototype = { - subscribe: function(callback) { - return new SubscriptionDefinition(this.channel, this.topic, callback); + subscribe: function() { + var len = arguments.length; + if(len === 1) { + return new SubscriptionDefinition(this.channel, this.topic, arguments[0]); + } + else if (len === 2) { + return new SubscriptionDefinition(this.channel, arguments[0], arguments[1]); + } }, publish: function(data, envelope) { @@ -43,142 +60,140 @@ ChannelDefinition.prototype = { env.channel = this.channel; env.timeStamp = new Date(); env.topic = env.topic || this.topic; - postal.configuration.bus.publish(env, data); + postal.configuration.bus.publish(data, env); } }; var SubscriptionDefinition = function(channel, topic, callback) { - this.channel = channel; - this.topic = topic; - this.callback = callback; - this.priority = DEFAULT_PRIORITY; - this.constraints = new Array(0); - this.maxCalls = DEFAULT_DISPOSEAFTER; - this.onHandled = NO_OP; - this.context = null; + this.channel = channel; + this.topic = topic; + this.callback = callback; + this.priority = DEFAULT_PRIORITY; + this.constraints = new Array(0); + this.maxCalls = DEFAULT_DISPOSEAFTER; + this.onHandled = NO_OP; + this.context = null; + postal.publish({ + event: "subscription.created", + channel: channel, + topic: topic + },{ + channel: SYSTEM_CHANNEL, + topic: "subscription.created" + }); postal.configuration.bus.subscribe(this); - postal.publish({ - channel: SYSTEM_CHANNEL, - topic: "subscription.created" - }, - { - event: "subscription.created", - channel: channel, - topic: topic - }); }; SubscriptionDefinition.prototype = { - unsubscribe: function() { - postal.configuration.bus.unsubscribe(this); - postal.publish({ - channel: SYSTEM_CHANNEL, - topic: "subscription.removed" - }, - { - event: "subscription.removed", - channel: this.channel, - topic: this.topic - }); - }, + unsubscribe: function() { + postal.configuration.bus.unsubscribe(this); + postal.publish({ + event: "subscription.removed", + channel: this.channel, + topic: this.topic + },{ + channel: SYSTEM_CHANNEL, + topic: "subscription.removed" + }); + }, - defer: function() { - var fn = this.callback; - this.callback = function(data) { - setTimeout(fn,0,data); - }; - return this; - }, + defer: function() { + var fn = this.callback; + this.callback = function(data) { + setTimeout(fn,0,data); + }; + return this; + }, - disposeAfter: function(maxCalls) { - if(_.isNaN(maxCalls) || maxCalls <= 0) { - throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero."; - } + disposeAfter: function(maxCalls) { + if(_.isNaN(maxCalls) || maxCalls <= 0) { + throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero."; + } - var fn = this.onHandled; - var dispose = _.after(maxCalls, _.bind(function() { - this.unsubscribe(this); - }, this)); + 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; - }, + this.onHandled = function() { + fn.apply(this.context, arguments); + dispose(); + }; + return this; + }, - ignoreDuplicates: function() { - this.withConstraint(new DistinctPredicate()); - return this; - }, + ignoreDuplicates: function() { + this.withConstraint(new DistinctPredicate()); + return this; + }, - whenHandledThenExecute: function(callback) { - if(! _.isFunction(callback)) { - throw "Value provided to 'whenHandledThenExecute' must be a function"; - } - this.onHandled = callback; - return this; - }, + whenHandledThenExecute: function(callback) { + if(! _.isFunction(callback)) { + throw "Value provided to 'whenHandledThenExecute' must be a function"; + } + this.onHandled = callback; + return this; + }, - withConstraint: function(predicate) { - if(! _.isFunction(predicate)) { - throw "Predicate constraint must be a function"; - } - this.constraints.push(predicate); - return this; - }, + withConstraint: function(predicate) { + if(! _.isFunction(predicate)) { + throw "Predicate constraint must be a function"; + } + this.constraints.push(predicate); + return this; + }, - withConstraints: function(predicates) { - var self = this; - if(_.isArray(predicates)) { - _.each(predicates, function(predicate) { self.withConstraint(predicate); } ); - } - return self; - }, + withConstraints: function(predicates) { + var self = this; + if(_.isArray(predicates)) { + _.each(predicates, function(predicate) { self.withConstraint(predicate); } ); + } + return self; + }, - withContext: function(context) { - this.context = context; - return this; - }, + withContext: function(context) { + this.context = context; + return this; + }, - withDebounce: function(milliseconds) { - if(_.isNaN(milliseconds)) { - throw "Milliseconds must be a number"; - } - var fn = this.callback; - this.callback = _.debounce(fn, milliseconds); - return this; - }, + withDebounce: function(milliseconds) { + if(_.isNaN(milliseconds)) { + throw "Milliseconds must be a number"; + } + var fn = this.callback; + this.callback = _.debounce(fn, milliseconds); + return this; + }, - withDelay: function(milliseconds) { - if(_.isNaN(milliseconds)) { - throw "Milliseconds must be a number"; - } - var fn = this.callback; - this.callback = function(data) { - setTimeout(fn, milliseconds, data); - }; - return this; - }, + withDelay: function(milliseconds) { + if(_.isNaN(milliseconds)) { + throw "Milliseconds must be a number"; + } + var fn = this.callback; + this.callback = function(data) { + setTimeout(fn, milliseconds, data); + }; + return this; + }, - withPriority: function(priority) { - if(_.isNaN(priority)) { - throw "Priority must be a number"; - } - this.priority = priority; - return this; - }, + withPriority: function(priority) { + if(_.isNaN(priority)) { + throw "Priority must be a number"; + } + this.priority = priority; + return this; + }, - withThrottle: function(milliseconds) { - if(_.isNaN(milliseconds)) { - throw "Milliseconds must be a number"; - } - var fn = this.callback; - this.callback = _.throttle(fn, milliseconds); - return this; - } + withThrottle: function(milliseconds) { + if(_.isNaN(milliseconds)) { + throw "Milliseconds must be a number"; + } + var fn = this.callback; + this.callback = _.throttle(fn, milliseconds); + return this; + } }; var bindingsResolver = { @@ -209,9 +224,9 @@ var localBus = { wireTaps: new Array(0), - publish: function(envelope, data) { + publish: function(data, envelope) { _.each(this.wireTaps,function(tap) { - tap(envelope, data); + tap(data, envelope); }); _.each(this.subscriptions[envelope.channel], function(topic) { @@ -279,14 +294,14 @@ var localBus = { }; var publishPicker = { - "2" : function(envelope, payload) { + "2" : function(data, envelope) { if(!envelope.channel) { envelope.channel = DEFAULT_CHANNEL; } - postal.configuration.bus.publish(envelope, payload); + postal.configuration.bus.publish(data, envelope); }, "3" : function(channel, topic, payload) { - postal.configuration.bus.publish({ channel: channel, topic: topic }, payload); + postal.configuration.bus.publish(payload, { channel: channel, topic: topic }); } }; @@ -343,7 +358,7 @@ var postal = { var newEnv = env; newEnv.topic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; newEnv.channel = destChannel; - postal.publish(newEnv, msg); + postal.publish(msg, newEnv); } }) ); @@ -352,5 +367,8 @@ var postal = { return result; } }; - -global.postal = postal; })(window); \ No newline at end of file + + + global.postal = postal; + return postal; +})); \ No newline at end of file diff --git a/lib/browser/amd/postal.diagnostics.js b/lib/browser/amd/postal.diagnostics.js deleted file mode 100644 index 9f9ee66..0000000 --- a/lib/browser/amd/postal.diagnostics.js +++ /dev/null @@ -1,20 +0,0 @@ -define(['postal', 'underscore'], function(postal, _) { -postal.addWireTap(function(data, envelope) { - var all = _.extend(envelope, { data: data }); - if(!JSON) { - throw "This browser or environment does not provide JSON support"; - } - try { - console.log(JSON.stringify(all)); - } - catch(exception) { - try { - all.data = "ERROR: " + exception.message; - console.log(JSON.stringify(all)); - } - catch(ex) { - console.log("Unable to parse data to JSON: " + exception); - } - } -}); -}); \ No newline at end of file diff --git a/lib/browser/amd/postal.diagnostics.min.gz.js b/lib/browser/amd/postal.diagnostics.min.gz.js deleted file mode 100644 index be522d397c8ad15855ff9721ab49c9793e4fabf7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 243 zcmVhiwFR+kY-N+1C5TcPQ)+_hF`^GN<@_hxYLa+44hOBF(3vvaob3}IC9d$ zsmi-Um|$bG_5J^6dm2L|{iYJ}b*Z*C2Gkq<-DAN6R&xxazI%K=KbbNMf2hkGG7R5SCIJ?pxCP;OrC7i-Wr$oL z32(s@v>xO&C6=O&MQ*#}n0X<)@T42Hxk!jp7;oAeQymsJYchMwdfT2jsfWHl_Xkk- tWUrJnPSpKNGnzl=)Jw!+At($9B@;Az+nV4= 0; idx--) { - if(subs[idx].priority <= subDef.priority) { - subs.splice(idx + 1, 0, subDef); - found = true; - break; - } - } - if(!found) { - subs.unshift(subDef); - } - return subDef; - }, - - unsubscribe: function(config) { - if(this.subscriptions[config.channel][config.topic]) { - var len = this.subscriptions[config.channel][config.topic].length, - idx = 0; - for ( ; idx < len; idx++ ) { - if (this.subscriptions[config.channel][config.topic][idx] === config) { - this.subscriptions[config.channel][config.topic].splice( idx, 1 ); - break; - } - } - } - }, - - addWireTap: function(callback) { - var self = this; - self.wireTaps.push(callback); - return function() { - var idx = self.wireTaps.indexOf(callback); - if(idx !== -1) { - self.wireTaps.splice(idx,1); - } - }; - } -}; - -var publishPicker = { - "2" : function(envelope, payload) { - if(!envelope.channel) { - envelope.channel = DEFAULT_CHANNEL; - } - postal.configuration.bus.publish(envelope, payload); - }, - "3" : function(channel, topic, payload) { - postal.configuration.bus.publish({ channel: channel, topic: topic }, payload); - } -}; - -// save some setup time, albeit tiny -localBus.subscriptions[SYSTEM_CHANNEL] = {}; - -var postal = { - configuration: { - bus: localBus, - resolver: bindingsResolver - }, - - channel: function(options) { - var channel = options.channel || DEFAULT_CHANNEL, - tpc = options.topic; - return new ChannelDefinition(channel, tpc); - }, - - subscribe: function(options) { - var callback = options.callback, - topic = options.topic, - channel = options.channel || DEFAULT_CHANNEL; - return new SubscriptionDefinition(channel, topic, callback); - }, - - publish: function() { - var len = arguments.length; - if(publishPicker[len]) { - publishPicker[len].apply(this, arguments); - } - }, - - addWireTap: function(callback) { - return this.configuration.bus.addWireTap(callback); - }, - - linkChannels: function(sources, destinations) { - var result = []; - if(!_.isArray(sources)) { - sources = [sources]; - } - if(!_.isArray(destinations)) { - destinations = [destinations]; - } - _.each(sources, function(source){ - var sourceTopic = source.topic || "*"; - _.each(destinations, function(destination) { - var destChannel = destination.channel || DEFAULT_CHANNEL; - result.push( - postal.subscribe({ - channel: source.channel || DEFAULT_CHANNEL, - topic: source.topic || "*", - callback : function(msg, env) { - var newEnv = env; - newEnv.topic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; - newEnv.channel = destChannel; - postal.publish(newEnv, msg); - } - }) - ); - }); - }); - return result; - } -}; - -return postal; }); \ No newline at end of file diff --git a/lib/browser/amd/postal.min.gz.js b/lib/browser/amd/postal.min.gz.js deleted file mode 100644 index 6f0e3e161d0867472d86c8f09928b2a5a65d0865..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1538 zcmV+d2L1UTiwFR+kY-N+1HD*Vlbbda{wpl)I4COGY*(rrBv{>bNty zVJnM)fY?hg|Gh^bK-k_(U-Gg>w{t$7>!%emR;dhDew7MYlsqe>zs7Nua;>s7ps2p4 zg~y2h!pEHaw7`Nauq1w-m6|3#j>)hW)eYXb!}erUNL>}FH_Y>HL6EbHevO8z{HP0+ zZeaCQAP6UfFw6)?OBk8ucV8-+1ROI2(=_Bs2BtCuyI_I3)g8Tk0|H7S zx^b5NMpKbULE_=KmbaRuRg#>%#o&V0(g^nS_o^-IrNk}brEmAu$u52T+3^j z>U)-PFNInutU*nr+$gBh6LEm*Q)r#7{}*o|ciHv*c>Co6?=3bOM_v|HE zAiXWJ8~<`Ey=$6O(gW*WDz|d>bX&R`({X_z~s>eSs6E5*`omarSEPfue3b0_?a>Juj8Dx z_+LlXes)^kGsoP$X#XBtupZe2^%D_&D5PkO;faspzE4Y!?i z1&X2BjE0hOkTq#){LmRtJQ3!DCgbf>-C$D9sDtjUWEpIi_veVU%y!1N<#0x84lM-P89)6ZF3x9eXuR%QDptb6R2DSLyK|)^%5j#oqS6- zXsgn3L)V5slbS>1$tCb2p6zm4$ODc5z^pXbwqonqY%*NYHR_a}Ei58aGyasDcei=q z|LdPqXc&1yAqHN23SVrn|LIfcBWLotnGB!1bKjY%)$92`n9jfA#r)?rET8+|`%u#i z>yV`eGxfI>v)L5mem@Z zURyr9bCX1-?LL6u?wqJxvA%ygyjBdN#bolZ+ZVFTl51In)`5YrRE%}MZGC}QfO)5S z;S-gE1_ma07|2EzdtwYB;t%Bzj6E*c1|){=vjVkiu%^XG)-6r?EC^~426;op%^is6jzOP1W@YO5QD9XP1(n^!6&I79rN>1 z{QWEGX}j&+0NbLw9e>FpetwRgtZxM-BZOL(^SDB56^<{EH3sA+72@wMrH+GU(oflt zdRr z+NzD5m!^5gi3jt!m0~NzC<7AQ6&_6-VP*wciS^J4I6nNB zLp(mJ{pWskvH^7aglkLLMg7isqz;k8U5|D_T1i{`!8hxCY@C#W>=0;b--)if(f[b].priority<=a.priority){f.splice(b+1,0,a),c=!0;break}return c||f.unshift(a),a},unsubscribe:function(a){if(this.subscriptions[a.channel][a.topic]){var b=this.subscriptions[a.channel][a.topic].length,c=0;for(;cJ93Vu?!rAtC^W)^rV+gfj#dUuxPwPir_DmmgJFAIyjxw?!78s9# zDOPRa3A2b)L}EGykD~60JfuWi!uq z4F7wpY~8xufp?FWxhT#KL%2ktGiEc~lGn(|VB6buK>>gGa&%Wp!SwAKP;rNw-N(Va zyq}IARwv=_<~+p${jVw{zyg$(D4ed86arI$#0pt>iGiT@pxkn1(PppmSUso0Gr5^Z zQ?IV5#5Bg?VZ61QNA;5lc70}D9dT&hy6)KRpm`1MN~PdP?Y}T&|GaBH6ZSKKGGvrO WQ0diGz!Pt`ZL>eM047u`0RR9%>TIe2 diff --git a/lib/browser/standard/postal.diagnostics.min.js b/lib/browser/standard/postal.diagnostics.min.js index 9ba4bdd..6dffe94 100644 --- a/lib/browser/standard/postal.diagnostics.min.js +++ b/lib/browser/standard/postal.diagnostics.min.js @@ -1 +1 @@ -postal.addWireTap(function(a,b){var c=_.extend(b,{data:a});if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(c))}catch(d){try{c.data="ERROR: "+d.message,console.log(JSON.stringify(c))}catch(e){console.log("Unable to parse data to JSON: "+d)}}}) \ No newline at end of file +(function(a,b,c){typeof define=="function"&&define.amd?define(["postal","underscore"],function(d,e){return c(d,e,a,b)}):c(a.postal,a._,a,b)})(this,document,function(a,b,c,d,e){return a.addWireTap(function(a,c){var d=b.extend(c,{data:a});if(!JSON)throw"This browser or environment does not provide JSON support";try{console.log(JSON.stringify(d))}catch(e){try{d.data="ERROR: "+e.message,console.log(JSON.stringify(d))}catch(f){console.log("Unable to parse data to JSON: "+e)}}})}) \ No newline at end of file diff --git a/lib/browser/standard/postal.js b/lib/browser/standard/postal.js index 66cd36a..7fe0a44 100644 --- a/lib/browser/standard/postal.js +++ b/lib/browser/standard/postal.js @@ -1,4 +1,3 @@ -(function(global, undefined) { /* postal.js Author: Jim Cowart @@ -6,6 +5,18 @@ Version 0.4.0 */ +(function(root, doc, factory) { + if (typeof define === "function" && define.amd) { + // AMD. Register as an anonymous module. + define(["underscore"], function(_) { + return factory(_, root, doc); + }); + } else { + // Browser globals + factory(root._, root, doc); + } +}(this, document, function(_, global, document, undefined) { + var DEFAULT_CHANNEL = "/", DEFAULT_PRIORITY = 50, DEFAULT_DISPOSEAFTER = 0, @@ -34,8 +45,14 @@ var ChannelDefinition = function(channelName, defaultTopic) { }; ChannelDefinition.prototype = { - subscribe: function(callback) { - return new SubscriptionDefinition(this.channel, this.topic, callback); + subscribe: function() { + var len = arguments.length; + if(len === 1) { + return new SubscriptionDefinition(this.channel, this.topic, arguments[0]); + } + else if (len === 2) { + return new SubscriptionDefinition(this.channel, arguments[0], arguments[1]); + } }, publish: function(data, envelope) { @@ -43,142 +60,140 @@ ChannelDefinition.prototype = { env.channel = this.channel; env.timeStamp = new Date(); env.topic = env.topic || this.topic; - postal.configuration.bus.publish(env, data); + postal.configuration.bus.publish(data, env); } }; var SubscriptionDefinition = function(channel, topic, callback) { - this.channel = channel; - this.topic = topic; - this.callback = callback; - this.priority = DEFAULT_PRIORITY; - this.constraints = new Array(0); - this.maxCalls = DEFAULT_DISPOSEAFTER; - this.onHandled = NO_OP; - this.context = null; + this.channel = channel; + this.topic = topic; + this.callback = callback; + this.priority = DEFAULT_PRIORITY; + this.constraints = new Array(0); + this.maxCalls = DEFAULT_DISPOSEAFTER; + this.onHandled = NO_OP; + this.context = null; + postal.publish({ + event: "subscription.created", + channel: channel, + topic: topic + },{ + channel: SYSTEM_CHANNEL, + topic: "subscription.created" + }); postal.configuration.bus.subscribe(this); - postal.publish({ - channel: SYSTEM_CHANNEL, - topic: "subscription.created" - }, - { - event: "subscription.created", - channel: channel, - topic: topic - }); }; SubscriptionDefinition.prototype = { - unsubscribe: function() { - postal.configuration.bus.unsubscribe(this); - postal.publish({ - channel: SYSTEM_CHANNEL, - topic: "subscription.removed" - }, - { - event: "subscription.removed", - channel: this.channel, - topic: this.topic - }); - }, + unsubscribe: function() { + postal.configuration.bus.unsubscribe(this); + postal.publish({ + event: "subscription.removed", + channel: this.channel, + topic: this.topic + },{ + channel: SYSTEM_CHANNEL, + topic: "subscription.removed" + }); + }, - defer: function() { - var fn = this.callback; - this.callback = function(data) { - setTimeout(fn,0,data); - }; - return this; - }, + defer: function() { + var fn = this.callback; + this.callback = function(data) { + setTimeout(fn,0,data); + }; + return this; + }, - disposeAfter: function(maxCalls) { - if(_.isNaN(maxCalls) || maxCalls <= 0) { - throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero."; - } + disposeAfter: function(maxCalls) { + if(_.isNaN(maxCalls) || maxCalls <= 0) { + throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero."; + } - var fn = this.onHandled; - var dispose = _.after(maxCalls, _.bind(function() { - this.unsubscribe(this); - }, this)); + 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; - }, + this.onHandled = function() { + fn.apply(this.context, arguments); + dispose(); + }; + return this; + }, - ignoreDuplicates: function() { - this.withConstraint(new DistinctPredicate()); - return this; - }, + ignoreDuplicates: function() { + this.withConstraint(new DistinctPredicate()); + return this; + }, - whenHandledThenExecute: function(callback) { - if(! _.isFunction(callback)) { - throw "Value provided to 'whenHandledThenExecute' must be a function"; - } - this.onHandled = callback; - return this; - }, + whenHandledThenExecute: function(callback) { + if(! _.isFunction(callback)) { + throw "Value provided to 'whenHandledThenExecute' must be a function"; + } + this.onHandled = callback; + return this; + }, - withConstraint: function(predicate) { - if(! _.isFunction(predicate)) { - throw "Predicate constraint must be a function"; - } - this.constraints.push(predicate); - return this; - }, + withConstraint: function(predicate) { + if(! _.isFunction(predicate)) { + throw "Predicate constraint must be a function"; + } + this.constraints.push(predicate); + return this; + }, - withConstraints: function(predicates) { - var self = this; - if(_.isArray(predicates)) { - _.each(predicates, function(predicate) { self.withConstraint(predicate); } ); - } - return self; - }, + withConstraints: function(predicates) { + var self = this; + if(_.isArray(predicates)) { + _.each(predicates, function(predicate) { self.withConstraint(predicate); } ); + } + return self; + }, - withContext: function(context) { - this.context = context; - return this; - }, + withContext: function(context) { + this.context = context; + return this; + }, - withDebounce: function(milliseconds) { - if(_.isNaN(milliseconds)) { - throw "Milliseconds must be a number"; - } - var fn = this.callback; - this.callback = _.debounce(fn, milliseconds); - return this; - }, + withDebounce: function(milliseconds) { + if(_.isNaN(milliseconds)) { + throw "Milliseconds must be a number"; + } + var fn = this.callback; + this.callback = _.debounce(fn, milliseconds); + return this; + }, - withDelay: function(milliseconds) { - if(_.isNaN(milliseconds)) { - throw "Milliseconds must be a number"; - } - var fn = this.callback; - this.callback = function(data) { - setTimeout(fn, milliseconds, data); - }; - return this; - }, + withDelay: function(milliseconds) { + if(_.isNaN(milliseconds)) { + throw "Milliseconds must be a number"; + } + var fn = this.callback; + this.callback = function(data) { + setTimeout(fn, milliseconds, data); + }; + return this; + }, - withPriority: function(priority) { - if(_.isNaN(priority)) { - throw "Priority must be a number"; - } - this.priority = priority; - return this; - }, + withPriority: function(priority) { + if(_.isNaN(priority)) { + throw "Priority must be a number"; + } + this.priority = priority; + return this; + }, - withThrottle: function(milliseconds) { - if(_.isNaN(milliseconds)) { - throw "Milliseconds must be a number"; - } - var fn = this.callback; - this.callback = _.throttle(fn, milliseconds); - return this; - } + withThrottle: function(milliseconds) { + if(_.isNaN(milliseconds)) { + throw "Milliseconds must be a number"; + } + var fn = this.callback; + this.callback = _.throttle(fn, milliseconds); + return this; + } }; var bindingsResolver = { @@ -209,9 +224,9 @@ var localBus = { wireTaps: new Array(0), - publish: function(envelope, data) { + publish: function(data, envelope) { _.each(this.wireTaps,function(tap) { - tap(envelope, data); + tap(data, envelope); }); _.each(this.subscriptions[envelope.channel], function(topic) { @@ -279,14 +294,14 @@ var localBus = { }; var publishPicker = { - "2" : function(envelope, payload) { + "2" : function(data, envelope) { if(!envelope.channel) { envelope.channel = DEFAULT_CHANNEL; } - postal.configuration.bus.publish(envelope, payload); + postal.configuration.bus.publish(data, envelope); }, "3" : function(channel, topic, payload) { - postal.configuration.bus.publish({ channel: channel, topic: topic }, payload); + postal.configuration.bus.publish(payload, { channel: channel, topic: topic }); } }; @@ -343,7 +358,7 @@ var postal = { var newEnv = env; newEnv.topic = _.isFunction(destination.topic) ? destination.topic(env.topic) : destination.topic || env.topic; newEnv.channel = destChannel; - postal.publish(newEnv, msg); + postal.publish(msg, newEnv); } }) ); @@ -352,5 +367,8 @@ var postal = { return result; } }; - -global.postal = postal; })(window); \ No newline at end of file + + + global.postal = postal; + return postal; +})); \ No newline at end of file diff --git a/lib/browser/standard/postal.min.gz.js b/lib/browser/standard/postal.min.gz.js index d40437283e9213fd5317dfcfb619ba068d3a56c5..9b510fc904f5e379500df938e617ad95a897a2e1 100644 GIT binary patch literal 1615 zcmV-V2C(@biwFQ#8E8)c1HD*Vlbbda{wpl)I4COQT%vM|*Xj%Pew)jN()h;&$uq zY}U^CbSGZ7KfRS(r9u`Z&kE_TaW6}teIZpxxSz0?Sdc|%WjSO zarsFVak_!k*PbAp5W+Ac90f4a%kTfIXyS2z4Aa!-Nd`)Dgz$x^YkW;?+&~@Z5EZ3;UdBu{r+y-M+HO@w@eI4>%=aEqEt9U0r zDZ0yvzVjVbQcpI7nE!`^1v~e7mPYZWDyY85XI1H&+u>4<>N?cdCgjLvALhLsaGE5H z@@vA|abCn(5vzL=ne{9!RY7A=x`FzpDCoVnK<(;|-n|6@CE2v&Ed7Yild&oKA(@WjD4?W5;Y%8H{pP&rVzo-UoR`7RLcHvT7I4V^jBMz*|eH zuD}Up@RnE{{#I1qr-u{A`7IpMRx06ZKZQVlS(?k z?oBMDa8%~lf*h~&8074#QaMaI)JdxygSAa#!~($5)cGol%y+{!n8v<^bpoPVoX0Tr zS)2;bqM_M*WT;jxghq@E?rNRqKF#yw-n0F8hF*K-taQpoP_syvA~Krd};*!SGV zYWubiBu~fD7i47IZe@o8_)Fj4NnR;AW&A=@{>w0D82|H#?PsUuJ+tWU%hm&2unyS> z^%D_&ETm}2aE8or_HHZ5KsZ4xF?13`IRY{GhF!MW1jW#7Mtw;+$g)bPw(i6j6iP1U z{Y~21fqh_tv&m-ELibLx47SUMW5h7?Tbw|ikRU^pN6tU;^v9!@`odhT*xb$m%*r!IO?LB#>E7$NuTYxQ6ST1A3(5oj;>tw z-PgGNHV4FLa$4;RS!T(NEPP`?PncHBv}a9z0bhW5D|_J+nFD|VOt7$dJq#{1dO>)@ zf+;|vuRbeKbq%}@N!mjoQ+EL;S{G^>ef?U%lAIuhTEv7B40~o*W9sPfJ@B(321J6u zr*0ngl=>wRnZAC$45O^@LiRIRgls-X5HbhH${D$f$YHM@@Y>QSq{)&bC@)AAyz7qaXwk!i_lYSH45yKv6#N^%YkaJPUF9?}0 zXESTnf}%lWWdCxK6`RIBvfVksRkW N`XBpVJVn?N004tb8Ik}1 literal 1515 zcmV0CeAiz~@hl1YzZhU!~dIGni8T^x}gS6Gl0 zj)|LRrJ|{eH)Pn2>IQG^p*~5&La3^c&M-fSJiF-EuKlF^po&CpVD+WP2_uA1ObJ5) zjP&x`e=C}L3{wQt*FH}(DPS|gXS``}LZ+D2HYt6+rBaF%1hqqz2dSn^*QmB!_8q|cJ>?o+@ zOdOzk4z0EI|KcsgF1vjkZ@)a?od#YjaP%JB0Y@ShMR1&TWa8C6&!(=nGqM!w5@Io{ z6okwQ; zXPKeh{J>G&wGf(EWDHm9Jo9Our}v&6znB)AY6o5ek8^LZQL>R)A>LGZns9(#+TO7B z>=U(pJtUH+^Jt5VjQv*hD8PR5?VaG262}%l(UW$s?I`-m-Q zk8Fba3lY69MARC?87s$0-Byu-bb?%>Xcj{`0Wrpg-A=j!MbTn$?F-657Pb$uV`o6| zM3@blw72KF!K9km7J4IC2HVBsIbtmHTarSZ5FjHe9|Zp-(x1-WYdGB5wq{q0#bmgqSEy5ZwhB8wp=-t;V)OPc_uPNna|#V3PbtK}^N;@X z4R$|%^j&04KGT!_GkfkEJ+*$d{2SBdXS`bebOp=j?zb)if(NWal;FLgKt_wj6yxDA z9h162_!u@h18NWfF-r$_)%Lei+hU&-;*#cNFd-82EXpLWDWBarY=<0g_W=ZZ=S1b2 z_Wko}a?%{&lgZ<5Ux+eGZ$;r72YS3Ge%*IlUmzA>-l|@BrgBhE!vqfl*~nstk0C_d zp&Wv-#|6`X_|SdU5Mfu)dB!5`A&{wAz&wR*qZo4>0c$^Dm{5RYPwZ-49Y4KiZ43h< zLEx8eo^VR2!zA`oAvbEfya?m0@Iv-8S%qx5M36Eu#MqGiF9;oeqdN9W-Gm_*&o6++ zKt94V0}cYExM_?dfKtbr7;KYRmwikdJWKuPn4c%{+g<4NZgV%lw(4%jU$Bs$pQA6< zw+55J3(2sY(+Z7MIKDu}7?7JZivH?S>O?ZM9>cbfpAoXW zSS+kn3yKD@BlY)3IX_$g0%-V6IC}E=(^dieLvZ}^dFo94@KK<9RuiU2CXIFIw-s1I zO}FI*H3$q++}DZVrWvUFL!(+ZgQO(JJ)!l|OrVQIPv^6iVk*Qa0}|X89!?&w0tx_| zd#Lu0g@M+Z$P9N-!xszvX|Z67{XQBzM>Ofk@Z%xPw{V)so7WxYa_03Mk@eLarPE=b(V~qWdls4e24QCur3E70|T?-GxIak?r3#L^jCV RLw;s^^dC9+bFbzR006^W=QaQU diff --git a/lib/browser/standard/postal.min.js b/lib/browser/standard/postal.min.js index 7edd507..0c85089 100644 --- a/lib/browser/standard/postal.min.js +++ b/lib/browser/standard/postal.min.js @@ -1 +1 @@ -(function(a,b){var c="/",d=50,e=0,f="postal",g=function(){},h=function(){var a;return function(b){var c=!1;return _.isString(b)?(c=b===a,a=b):(c=_.isEqual(b,a),a=_.clone(b)),!c}},i=function(a,b){this.channel=a,this.topic=b||""};i.prototype={subscribe:function(a){return new j(this.channel,this.topic,a)},publish:function(a,b){var c=b||{};c.channel=this.channel,c.timeStamp=new Date,c.topic=c.topic||this.topic,n.configuration.bus.publish(c,a)}};var j=function(a,b,c){this.channel=a,this.topic=b,this.callback=c,this.priority=d,this.constraints=new Array(0),this.maxCalls=e,this.onHandled=g,this.context=null,n.configuration.bus.subscribe(this),n.publish({channel:f,topic:"subscription.created"},{event:"subscription.created",channel:a,topic:b})};j.prototype={unsubscribe:function(){n.configuration.bus.unsubscribe(this),n.publish({channel:f,topic:"subscription.removed"},{event:"subscription.removed",channel:this.channel,topic:this.topic})},defer:function(){var a=this.callback;return this.callback=function(b){setTimeout(a,0,b)},this},disposeAfter:function(a){if(_.isNaN(a)||a<=0)throw"The value provided to disposeAfter (maxCalls) must be a number greater than zero.";var b=this.onHandled,c=_.after(a,_.bind(function(){this.unsubscribe(this)},this));return this.onHandled=function(){b.apply(this.context,arguments),c()},this},ignoreDuplicates:function(){return this.withConstraint(new h),this},whenHandledThenExecute:function(a){if(!_.isFunction(a))throw"Value provided to 'whenHandledThenExecute' must be a function";return this.onHandled=a,this},withConstraint:function(a){if(!_.isFunction(a))throw"Predicate constraint must be a function";return this.constraints.push(a),this},withConstraints:function(a){var b=this;return _.isArray(a)&&_.each(a,function(a){b.withConstraint(a)}),b},withContext:function(a){return this.context=a,this},withDebounce:function(a){if(_.isNaN(a))throw"Milliseconds must be a number";var b=this.callback;return this.callback=_.debounce(b,a),this},withDelay:function(a){if(_.isNaN(a))throw"Milliseconds must be a number";var b=this.callback;return this.callback=function(c){setTimeout(b,a,c)},this},withPriority:function(a){if(_.isNaN(a))throw"Priority must be a number";return this.priority=a,this},withThrottle:function(a){if(_.isNaN(a))throw"Milliseconds must be a number";var b=this.callback;return this.callback=_.throttle(b,a),this}};var k={cache:{},compare:function(a,b){if(this.cache[b]&&this.cache[b][a])return!0;var c=new RegExp("^"+a.replace(/\./g,"\\.").replace(/\*/g,".*").replace(/#/g,"[A-Z,a-z,0-9]*")+"$"),d=c.test(b);return d&&(this.cache[b]||(this.cache[b]={}),this.cache[b][a]=!0),d}},l={subscriptions:{},wireTaps:new Array(0),publish:function(a,b){_.each(this.wireTaps,function(c){c(a,b)}),_.each(this.subscriptions[a.channel],function(c){_.each(c,function(c){n.configuration.resolver.compare(c.topic,a.topic)&&_.all(c.constraints,function(a){return a(b)})&&typeof c.callback=="function"&&(c.callback.apply(c.context,[b,a]),c.onHandled())})})},subscribe:function(a){var b,c,d,e=this.subscriptions[a.channel],f;e||(e=this.subscriptions[a.channel]={}),f=this.subscriptions[a.channel][a.topic],f||(f=this.subscriptions[a.channel][a.topic]=new Array(0)),b=f.length-1;for(;b>=0;b--)if(f[b].priority<=a.priority){f.splice(b+1,0,a),c=!0;break}return c||f.unshift(a),a},unsubscribe:function(a){if(this.subscriptions[a.channel][a.topic]){var b=this.subscriptions[a.channel][a.topic].length,c=0;for(;c=0;b--)if(f[b].priority<=a.priority){f.splice(b+1,0,a),c=!0;break}return c||f.unshift(a),a},unsubscribe:function(a){if(this.subscriptions[a.channel][a.topic]){var b=this.subscriptions[a.channel][a.topic].length,c=0;for(;c