Created
January 15, 2019 06:46
-
-
Save dlucidone/7c15477ac7af482ab4003ed83f538bd1 to your computer and use it in GitHub Desktop.
JS Bin PubSub // source https://jsbin.com/jamevox
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 name="description" content="PubSub"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var events = (function(){ | |
var topics = {}; | |
var hOP = topics.hasOwnProperty; | |
return { | |
subscribe: function(topic, listener) { | |
// Create the topic's object if not yet created | |
if(!hOP.call(topics, topic)) topics[topic] = []; | |
// Add the listener to queue | |
var index = topics[topic].push(listener) -1; | |
// Provide handle back for removal of topic | |
return { | |
remove: function() { | |
delete topics[topic][index]; | |
} | |
}; | |
}, | |
publish: function(topic, info) { | |
// If the topic doesn't exist, or there's no listeners in queue, just leave | |
if(!hOP.call(topics, topic)) return; | |
// Cycle through topics queue, fire! | |
topics[topic].forEach(function(item) { | |
item(info != undefined ? info : {}); | |
}); | |
} | |
}; | |
})(); | |
var subscription = events.subscribe('/page/load', function(obj) { | |
console.log("subscribe",obj) | |
}); | |
events.publish('/page/load', { | |
url: '/some/url/path' // any argument | |
}); | |
// ...sometime later where I no longer want subscription... | |
subscription.remove(); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var events = (function(){ | |
var topics = {}; | |
var hOP = topics.hasOwnProperty; | |
return { | |
subscribe: function(topic, listener) { | |
// Create the topic's object if not yet created | |
if(!hOP.call(topics, topic)) topics[topic] = []; | |
// Add the listener to queue | |
var index = topics[topic].push(listener) -1; | |
// Provide handle back for removal of topic | |
return { | |
remove: function() { | |
delete topics[topic][index]; | |
} | |
}; | |
}, | |
publish: function(topic, info) { | |
// If the topic doesn't exist, or there's no listeners in queue, just leave | |
if(!hOP.call(topics, topic)) return; | |
// Cycle through topics queue, fire! | |
topics[topic].forEach(function(item) { | |
item(info != undefined ? info : {}); | |
}); | |
} | |
}; | |
})(); | |
var subscription = events.subscribe('/page/load', function(obj) { | |
console.log("subscribe",obj) | |
}); | |
events.publish('/page/load', { | |
url: '/some/url/path' // any argument | |
}); | |
// ...sometime later where I no longer want subscription... | |
subscription.remove(); | |
</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
var events = (function(){ | |
var topics = {}; | |
var hOP = topics.hasOwnProperty; | |
return { | |
subscribe: function(topic, listener) { | |
// Create the topic's object if not yet created | |
if(!hOP.call(topics, topic)) topics[topic] = []; | |
// Add the listener to queue | |
var index = topics[topic].push(listener) -1; | |
// Provide handle back for removal of topic | |
return { | |
remove: function() { | |
delete topics[topic][index]; | |
} | |
}; | |
}, | |
publish: function(topic, info) { | |
// If the topic doesn't exist, or there's no listeners in queue, just leave | |
if(!hOP.call(topics, topic)) return; | |
// Cycle through topics queue, fire! | |
topics[topic].forEach(function(item) { | |
item(info != undefined ? info : {}); | |
}); | |
} | |
}; | |
})(); | |
var subscription = events.subscribe('/page/load', function(obj) { | |
console.log("subscribe",obj) | |
}); | |
events.publish('/page/load', { | |
url: '/some/url/path' // any argument | |
}); | |
// ...sometime later where I no longer want subscription... | |
subscription.remove(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment