Created
July 26, 2016 10:24
-
-
Save anonymous/3f271f89799cde83496282732889cafa to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/gupusu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
console.clear(); | |
/*! | |
* Pub/Sub implementation | |
* http://addyosmani.com/ | |
* Licensed under the GPL | |
*/ | |
var pubsubz = {}; | |
(function (q) { | |
var topics = {}, | |
subUid = -1; | |
q.publish = function (topic, args) { | |
if (!topics[topic]) { | |
return false; | |
} | |
setTimeout(function () { | |
var subscribers = topics[topic], | |
len = subscribers ? subscribers.length : 0; | |
while (len--) { | |
subscribers[len].func(topic, args); | |
} | |
}, 0); | |
return true; | |
}; | |
q.subscribe = function (topic, func) { | |
if (!topics[topic]) { | |
topics[topic] = []; | |
} | |
var token = (++subUid).toString(); | |
topics[topic].push({ | |
token: token, | |
func: func | |
}); | |
return token; | |
}; | |
q.unsubscribe = function (token) { | |
for (var m in topics) { | |
if (topics[m]) { | |
for (var i = 0, j = topics[m].length; i < j; i++) { | |
if (topics[m][i].token === token) { | |
topics[m].splice(i, 1); | |
return token; | |
} | |
} | |
} | |
} | |
return false; | |
}; | |
}(pubsubz)); | |
var testSubscriber = function( topics , data ){ | |
console.log( topics + ": " + data ); | |
}; | |
var testSubscription = pubsubz.subscribe( 'example1', testSubscriber ); | |
pubsubz.publish( 'example1', 'hello world!' ); | |
pubsubz.publish( 'example1', ['test','a','b','c'] ); | |
pubsubz.publish( 'example1', [{'color':'blue'},{'text':'hello'}] ); | |
setTimeout(function(){ | |
pubsubz.unsubscribe( testSubscription ); | |
}, 0); | |
pubsubz.publish( 'example1', 'hello again!' ); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.clear(); | |
/*! | |
* Pub/Sub implementation | |
* http://addyosmani.com/ | |
* Licensed under the GPL | |
*/ | |
var pubsubz = {}; | |
(function (q) { | |
var topics = {}, | |
subUid = -1; | |
q.publish = function (topic, args) { | |
if (!topics[topic]) { | |
return false; | |
} | |
setTimeout(function () { | |
var subscribers = topics[topic], | |
len = subscribers ? subscribers.length : 0; | |
while (len--) { | |
subscribers[len].func(topic, args); | |
} | |
}, 0); | |
return true; | |
}; | |
q.subscribe = function (topic, func) { | |
if (!topics[topic]) { | |
topics[topic] = []; | |
} | |
var token = (++subUid).toString(); | |
topics[topic].push({ | |
token: token, | |
func: func | |
}); | |
return token; | |
}; | |
q.unsubscribe = function (token) { | |
for (var m in topics) { | |
if (topics[m]) { | |
for (var i = 0, j = topics[m].length; i < j; i++) { | |
if (topics[m][i].token === token) { | |
topics[m].splice(i, 1); | |
return token; | |
} | |
} | |
} | |
} | |
return false; | |
}; | |
}(pubsubz)); | |
var testSubscriber = function( topics , data ){ | |
console.log( topics + ": " + data ); | |
}; | |
var testSubscription = pubsubz.subscribe( 'example1', testSubscriber ); | |
pubsubz.publish( 'example1', 'hello world!' ); | |
pubsubz.publish( 'example1', ['test','a','b','c'] ); | |
pubsubz.publish( 'example1', [{'color':'blue'},{'text':'hello'}] ); | |
setTimeout(function(){ | |
pubsubz.unsubscribe( testSubscription ); | |
}, 0); | |
pubsubz.publish( 'example1', 'hello again!' ); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
/*! | |
* Pub/Sub implementation | |
* http://addyosmani.com/ | |
* Licensed under the GPL | |
*/ | |
var pubsubz = {}; | |
(function (q) { | |
var topics = {}, | |
subUid = -1; | |
q.publish = function (topic, args) { | |
if (!topics[topic]) { | |
return false; | |
} | |
setTimeout(function () { | |
var subscribers = topics[topic], | |
len = subscribers ? subscribers.length : 0; | |
while (len--) { | |
subscribers[len].func(topic, args); | |
} | |
}, 0); | |
return true; | |
}; | |
q.subscribe = function (topic, func) { | |
if (!topics[topic]) { | |
topics[topic] = []; | |
} | |
var token = (++subUid).toString(); | |
topics[topic].push({ | |
token: token, | |
func: func | |
}); | |
return token; | |
}; | |
q.unsubscribe = function (token) { | |
for (var m in topics) { | |
if (topics[m]) { | |
for (var i = 0, j = topics[m].length; i < j; i++) { | |
if (topics[m][i].token === token) { | |
topics[m].splice(i, 1); | |
return token; | |
} | |
} | |
} | |
} | |
return false; | |
}; | |
}(pubsubz)); | |
var testSubscriber = function( topics , data ){ | |
console.log( topics + ": " + data ); | |
}; | |
var testSubscription = pubsubz.subscribe( 'example1', testSubscriber ); | |
pubsubz.publish( 'example1', 'hello world!' ); | |
pubsubz.publish( 'example1', ['test','a','b','c'] ); | |
pubsubz.publish( 'example1', [{'color':'blue'},{'text':'hello'}] ); | |
setTimeout(function(){ | |
pubsubz.unsubscribe( testSubscription ); | |
}, 0); | |
pubsubz.publish( 'example1', 'hello again!' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment