Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 7aaed54ea1c0635ae895dd9d17405278874c30e5 @daniellmb committed Apr 21, 2011
Showing with 315 additions and 0 deletions.
  1. +4 −0 History.md
  2. +122 −0 README.md
  3. +1 −0 minpubsub.js
  4. +79 −0 minpubsub.src.js
  5. +109 −0 unit-tests.htm
@@ -0,0 +1,4 @@
+0.0.1 / April 21, 2011
+==================
+
+ * Initial release
122 README.md
@@ -0,0 +1,122 @@
+Minimal PubSub
+=========
+
+An ultra minimalist pub/sub framework weighing in at only **198 bytes** gzipped. Created by rewritting Peter Higgins jQuery plugin to be self contained.
+
+### Features
+
+- Supports
+ - publish
+ - subscribe
+ - unsubscribe
+
+- Tested Environments
+ - ES5
+ - Node.js
+ - Rhino
+ - Development (console, etc.)
+ - Safari 4
+ - Google Chrome 5
+ - Internet Explorer 5 - 9
+ - iPhone Safari
+ - iPad Safari
+ - Firefox 3
+ - Firefox 4
+ - Opera 10.61
+
+### How to use
+
+```javascript
+ //subscribe to a topic
+ var handle = subscribe("/some/topic", function(msg){
+ console.log(msg);
+ });
+
+ //publish topic a few times
+ publish("/some/topic", ["first time"]);
+ publish("/some/topic", ["second time"]);
+
+ //unsubscribe from the topic
+ unsubscribe(handle);
+
+ //subscriber is no longer listening to the topic
+ publish("/some/topic", ["message will not be logged"]);
+```
+
+### Documentation
+
+##### Options:
+
+If you don't want these methods added to the window object simply modify the imediate function to pass the JavaScript object of your choosing.
+
+##### Methods:
+
+- **publish** *(`String` topic, `Array?` args)*
+
+ - summary:
+ - Publish some data on a named topic.
+
+ - topic: `String`
+ - The channel to publish on
+
+ - args: `Array?`
+ - Optional data to publish. Each array item is converted into ordered arguments on the subscribed functions.
+
+ - example:
+ - Publish stuff on '/some/topic'. Anything subscribed will be called with a function signature like: function(a,b,c){ ... }
+
+ ```javascript
+ publish("/some/topic", ["a","b","c"]);
+ ```
+
+- **subscribe** *(`String` topic, `Function` callback)*
+
+ - summary:
+ - Register a callback on a named topic.
+
+ - topic: `String`
+ - The channel to subscribe to
+
+ - callback: `Function`
+ - The handler event. Anytime something is publish'ed on a subscribed channel, the callback will be called with the published array as ordered arguments.
+
+ - returns: `Array`
+ - A handle which can be used to unsubscribe this particular subscription.
+
+ - example:
+
+ ```javascript
+ subscribe("/some/topic", function(a, b, c){ /* handle data */ });
+ ```
+
+- **unsubscribe** *(`Array` handle)*
+
+ - summary:
+ - Disconnect a subscribed function for a topic.
+
+ - handle: `Array`
+ - The return value from a subscribe call.
+
+ - example:
+
+ ```javascript
+ var handle = subscribe("/some/topic", function(){});
+ unsubscribe(handle);
+ ```
+
+
+### License
+
+WTFPL - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+Version 2, December 2004
+
+Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+Everyone is permitted to copy and distribute verbatim or modified
+copies of this license document, and changing it is allowed as long
+as the name is changed.
+
+DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+0. You just DO WHAT THE FUCK YOU WANT TO.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,79 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details. */
+
+(function(d){
+
+ // the topic/subscription hash
+ var cache = d.c_ || {}; //check for "c_" cache for unit testing
+
+ d.publish = function(/* String */ topic, /* Array? */ args){
+ // summary:
+ // Publish some data on a named topic.
+ // topic: String
+ // The channel to publish on
+ // args: Array?
+ // The data to publish. Each array item is converted into an ordered
+ // arguments on the subscribed functions.
+ //
+ // example:
+ // Publish stuff on '/some/topic'. Anything subscribed will be called
+ // with a function signature like: function(a,b,c){ ... }
+ //
+ // publish("/some/topic", ["a","b","c"]);
+
+ var subs = cache[topic],
+ len = subs ? subs.length : 0;
+
+ //can change loop or reverse array if the order matters
+ while(len--){
+ subs[len].apply(d, args || []);
+ }
+ };
+
+ d.subscribe = function(/* String */ topic, /* Function */ callback){
+ // summary:
+ // Register a callback on a named topic.
+ // topic: String
+ // The channel to subscribe to
+ // callback: Function
+ // The handler event. Anytime something is publish'ed on a
+ // subscribed channel, the callback will be called with the
+ // published array as ordered arguments.
+ //
+ // returns: Array
+ // A handle which can be used to unsubscribe this particular subscription.
+ //
+ // example:
+ // subscribe("/some/topic", function(a, b, c){ /* handle data */ });
+
+ if(!cache[topic]){
+ cache[topic] = [];
+ }
+ cache[topic].push(callback);
+ return [topic, callback]; // Array
+ };
+
+ d.unsubscribe = function(/* Array */ handle){
+ // summary:
+ // Disconnect a subscribed function for a topic.
+ // handle: Array
+ // The return value from a subscribe call.
+ // example:
+ // var handle = subscribe("/some/topic", function(){});
+ // unsubscribe(handle);
+
+ var subs = cache[handle[0]],
+ callback = handle[1],
+ len = subs ? subs.length : 0;
+
+ while(len--){
+ if(subs[len] === callback){
+ subs.splice(len, 1);
+ }
+ }
+ };
+
+})(this);
@@ -0,0 +1,109 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
+<title>Unit Testing Minimal PubSub</title>
+<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/3.3.0/build/cssfonts/fonts-min.css" />
+</head>
+<body class="yui3-skin-sam yui-skin-sam">
+<h1>Minimal PubSub Unit Tests</h1>
+<p>
+ An ultra minimalist pub/sub framework weighing in at only 198 bytes gzipped.
+</p>
+<div id="testLogger"></div>
+<script type="text/javascript" src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js"></script>
+<script type="text/javascript">c_ = {};</script>
+<script type="text/javascript" src="minpubsub.js"></script>
+<script type="text/javascript">
+YUI({ filter: 'raw' }).use("node", "console", "test" , function (Y) {
+ Y.namespace("pubsub.test");
+ Y.pubsub.test.PublishTestCase = new Y.Test.Case({
+ name : "Publish Tests",
+
+ setUp : function () {
+ var me = this;
+ me.count = 0;
+
+ c_["/some/topic"] = [
+ function (msg) {
+ me.count++;
+ }
+ ];
+ },
+ tearDown : function () {
+ delete c_["/some/topic"];
+ delete this.count;
+ },
+
+ //tests
+ testHasSub : function () {
+ var Assert = Y.Assert;
+
+ window.publish("/some/topic");
+
+ Assert.areEqual(1, this.count);
+ },
+ testNoSub : function () {
+ var Assert = Y.Assert;
+
+ window.publish("/bogus/topic");
+
+ Assert.areEqual(0, this.count);
+ }
+ });
+
+ Y.pubsub.test.SubscribeTestCase = new Y.Test.Case({
+ name : "Subscribe Tests",
+
+ tearDown : function () {
+ delete c_["/some/topic"];
+ },
+
+ //tests
+ testAddSub : function () {
+ var Assert = Y.Assert,
+ func = function(){};
+
+ window.subscribe("/some/topic", func);
+
+ Assert.areEqual(func, c_["/some/topic"][0]);
+ }
+ });
+
+ Y.pubsub.test.UnsubscribeTestCase = new Y.Test.Case({
+ name : "Unsubscribe Tests",
+
+ setUp : function () {
+ this.func = function () {};
+ c_["/some/topic"] = [this.func];
+ },
+ tearDown : function () {
+ delete c_["/some/topic"];
+ },
+
+ //tests
+ testRemoveSub : function () {
+ var Assert = Y.Assert;
+
+ window.unsubscribe(["/some/topic", this.func]);
+
+ Assert.areNotEqual(this.func, c_["/some/topic"][0]);
+ }
+ });
+
+ Y.pubsub.test.PubSubSuite = new Y.Test.Suite("PubSub Suite");
+ Y.pubsub.test.PubSubSuite.add(Y.pubsub.test.PublishTestCase);
+ Y.pubsub.test.PubSubSuite.add(Y.pubsub.test.SubscribeTestCase);
+ Y.pubsub.test.PubSubSuite.add(Y.pubsub.test.UnsubscribeTestCase);
+
+ var r = new Y.Console({
+ newestOnTop : false,
+ style: 'block'
+ });
+
+ r.render('#testLogger');
+ Y.Test.Runner.add(Y.pubsub.test.PubSubSuite);
+
+ Y.Test.Runner.run();
+});
+</script>

0 comments on commit 7aaed54

Please sign in to comment.