From d18fad58c310d72ff9f5af218576e07b6a2dbdcf Mon Sep 17 00:00:00 2001 From: Todd Kennedy Date: Tue, 19 Nov 2013 10:12:48 -0500 Subject: [PATCH 1/2] Fixing main field so that browserify can work on package. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ca01e3c..3fe094c 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ ], "author": "Daniel Lamb ", "dependencies": {}, - "main": "index", + "main": "minpubsub.src.js", "engines": { "node": "*" }, From 9d6e2c26988d9c9cf41754a52911292e3b1b0b60 Mon Sep 17 00:00:00 2001 From: Todd Kennedy Date: Tue, 19 Nov 2013 10:49:25 -0500 Subject: [PATCH 2/2] adding UMD support, re-minifying to fix failing test --- minpubsub.js | 2 +- minpubsub.src.js | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/minpubsub.js b/minpubsub.js index f826784..412241d 100644 --- a/minpubsub.js +++ b/minpubsub.js @@ -1 +1 @@ -(function(d){var e=d.c_||{};d.publish=function(a,b){for(var c=e[a],f=c?c.length:0;f--;)c[f].apply(d,b||[])};d.subscribe=function(a,b){e[a]||(e[a]=[]);e[a].push(b);return[a,b]};d.unsubscribe=function(a){for(var b=e[a[0]],a=a[1],c=b?b.length:0;c--;)b[c]===a&&b.splice(c,1)}})(this); \ No newline at end of file +(function(d){var MinPubSub={};var cache=d.c_||{};MinPubSub.publish=function(topic,args){var subs=cache[topic],len=subs?subs.length:0;while(len--){subs[len].apply(d,args||[])}};MinPubSub.subscribe=function(topic,callback){if(!cache[topic]){cache[topic]=[]}cache[topic].push(callback);return[topic,callback]};MinPubSub.unsubscribe=function(handle,callback){var subs=cache[callback?handle:handle[0]],callback=callback||handle[1],len=subs?subs.length:0;while(len--){if(subs[len]===callback){subs.splice(len,1)}}};if(typeof module==="object"&&typeof module.exports==="object"){module.exports=exports=MinPubSub}else if(typeof define==="function"&&define.amd){define(function(){return MinPubSub})}else if(typeof window==="object"){window.publish=MinPubSub.publish;window.subscribe=MinPubSub.subscribe;window.unsubscribe=MinPubSub.unsubscribe}})(window); \ No newline at end of file diff --git a/minpubsub.src.js b/minpubsub.src.js index f730121..d7b66ff 100644 --- a/minpubsub.src.js +++ b/minpubsub.src.js @@ -5,11 +5,12 @@ */ (function(d){ + var MinPubSub = {}; // the topic/subscription hash var cache = d.c_ || {}; //check for "c_" cache for unit testing - d.publish = function(/* String */ topic, /* Array? */ args){ + MinPubSub.publish = function(/* String */ topic, /* Array? */ args){ // summary: // Publish some data on a named topic. // topic: String @@ -33,7 +34,7 @@ } }; - d.subscribe = function(/* String */ topic, /* Function */ callback){ + MinPubSub.subscribe = function(/* String */ topic, /* Function */ callback){ // summary: // Register a callback on a named topic. // topic: String @@ -56,7 +57,7 @@ return [topic, callback]; // Array }; - d.unsubscribe = function(/* Array */ handle, /* Function? */ callback){ + MinPubSub.unsubscribe = function(/* Array */ handle, /* Function? */ callback){ // summary: // Disconnect a subscribed function for a topic. // handle: Array @@ -76,4 +77,18 @@ } }; -})(this); \ No newline at end of file + // UMD definition to allow for CommonJS, AMD and legacy window + if (typeof module === 'object' && typeof module.exports === 'object') { + // CommonJS, just export + module.exports = exports = MinPubSub; + } else if (typeof define === 'function' && define.amd) { + // AMD support + define(function () { return MinPubSub; }); + } else if (typeof window === 'object') { + // If no AMD and we are in the browser, attach to window + window.publish = MinPubSub.publish; + window.subscribe = MinPubSub.subscribe; + window.unsubscribe = MinPubSub.unsubscribe; + } + +})(window); \ No newline at end of file