Last active
August 29, 2015 14:01
-
-
Save kav2k/799c75f357a99840ef0e to your computer and use it in GitHub Desktop.
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 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