Эх сурвалжийг харах

[core] Add Pub-Sub functionality to Hue

This commit adds publication and subscription functionality to Hue.
Johan Ahlen 10 жил өмнө
parent
commit
4cdc3a27fe

+ 31 - 0
desktop/core/src/desktop/static/desktop/js/hue.utils.js

@@ -169,3 +169,34 @@ function s4() {
 function UUID() {
   return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
 }
+
+// Based on original pub/sub implementation from http://davidwalsh.name/pubsub-javascript
+var huePubSub = (function () {
+  var topics = {};
+  var hOP = topics.hasOwnProperty;
+
+  return {
+    subscribe: function (topic, listener) {
+      if(! hOP.call(topics, topic)) {
+        topics[topic] = [];
+      }
+
+      var index = topics[topic].push(listener) - 1;
+
+      return {
+        remove: function () {
+          delete topics[topic][index];
+        }
+      };
+    },
+    publish: function (topic, info) {
+      if (! hOP.call(topics, topic)) {
+        return;
+      }
+
+      topics[topic].forEach(function (item) {
+        item(info != undefined ? info : {});
+      });
+    }
+  };
+})();