Skip to content

Commit

Permalink
Improve Server.connect so child services trigger Server port onConnect.
Browse files Browse the repository at this point in the history
  • Loading branch information
caedesvvv committed Apr 8, 2014
1 parent 7144802 commit 8618a6d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion js/backend/services.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
define(function () {
var allPorts = {};
var instances = {};
var Services = {
/*
* Start the given service
Expand All @@ -12,6 +13,11 @@ define(function () {
if (!allPorts.hasOwnProperty(name)) {
allPorts[name] = [];
}
if (!instances.hasOwnProperty(name)) {
instances[name] = {onConnect: onConnect, onDisconnect: onDisconnect};
} else {
throw Error("Service with duplicate name!");
}
chrome.runtime.onConnect.addListener(function(port) {
if (port.name == name) {
allPorts[name].push(port)
Expand Down Expand Up @@ -48,7 +54,11 @@ define(function () {
if (!allPorts.hasOwnProperty(name)) {
allPorts[name] = [];
}
allPorts[name].push({postMessage: onMessage});
var port = {postMessage: onMessage};
allPorts[name].push(port);
console.log("["+name+"] connect child service");
instances[name].onConnect ? instances[name].onConnect(port) : null;
return port;
}
};
return Services;
Expand Down

0 comments on commit 8618a6d

Please sign in to comment.