var events = (function(){ var topics = {}; return { subscribe: function(topic, listener) { // topic, if(!topics[topic]) topics[topic] = { queue: [] }; // listener var index = topics[topic].queue.push(listener) -1; // return { remove: function() { delete topics[topic].queue[index]; } }; }, publish: function(topic, info) { // , if(!topics[topic] || !topics[topic].queue.length) return; // var items = topics[topic].queue; items.forEach(function(item) { item(info || {}); }); } }; })();
events.publish('/page/load', { url: '/some/url/path' // });
var subscription = events.subscribe('/page/load', function(obj) { // -, }); // ... ... subscription.remove();
Source: https://habr.com/ru/post/229589/
All Articles