Skip to content

Instantly share code, notes, and snippets.

@kav2k
Last active August 29, 2015 14:01
Show Gist options
  • Save kav2k/799c75f357a99840ef0e to your computer and use it in GitHub Desktop.
Save kav2k/799c75f357a99840ef0e to your computer and use it in GitHub Desktop.
var openPorts = ( function() {
var index = 0;
var ports = {};
var op = {
getPorts: function() {
var result = {};
for(var id in ports){
result[id] = ports[id];
}
return result;
},
getPortsArray: function() {
var result = [];
for(var id in ports){
result.push(ports[id]);
}
return result;
},
get: function(id) {
return ports[id];
},
add: function(port) {
var id = index;
ports[id] = port;
port.onDisconnect.addListener(function() {
op.remove(id);
});
index++;
return id;
},
remove: function(id) {
delete ports[id];
},
messageAll: function(message) {
for(var id in ports){
ports[id].postMessage(message);
}
}
};
return op;
})();
chrome.runtime.onConnect.addListener( function(port) { openPorts.add(port); } );
// ...
openPorts.messageAll({hello: "world"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment