Skip to content
This repository was archived by the owner on Dec 1, 2017. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions static/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ <h4 class="media-heading"><%= filename %></h4>
<script src="vendor/state-machine-2.2.0.js"></script>
<script src="vendor/tnetbin-0.2.1.js"></script>
<script src="/config.js"></script>
<script src="js/eventrouter.js"></script>
<script src="js/webrtc.js"></script>
<script src="js/validate.js"></script>
<script src="js/payloads.js"></script>
Expand Down
94 changes: 94 additions & 0 deletions static/js/eventrouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* jshint unused:false, maxlen: 200 */

/**
* The general event router.
*
* All the events that are passed by an app port (worker to subworker,
* application to worker, etc.) sould be routed with this event router.
*/

var EventRouter = (function() {
"use strict";

var DEFAULT_ROUTES = [
// Worker → SidebarApp
{from: "Worker", topic: "social.user-profile", to: "SidebarApp"},
{from: "Worker", topic: "talkilla.worker-ready", to: "SidebarApp"},
{from: "Worker", topic: "talkilla.spa-connected", to: "SidebarApp"},
{from: "Worker", topic: "talkilla.users", to: "SidebarApp", callable: "userListReceived"},
{from: "Worker", topic: "talkilla.error", to: "SidebarApp"},
{from: "Worker", topic: "talkilla.server-reconnection", to: "SidebarApp"},
{from: "Worker", topic: "talkilla.reauth-needed", to: "SidebarApp"},

// Worker → ChatApp
{from: "Worker", topic: "talkilla.user-joined", to: "ChatApp"},
{from: "Worker", topic: "talkilla.user-left", to: "ChatApp"},
{from: "Worker", topic: "talkilla.move-accept", to: "ChatApp"},
];


/**
* Route the events to the appropriate context, using appPorts.
*
* @param {String} name Name of the context. To be matched with the
* ones in the from/to/via of the routes.
* @param {Object} context Object the router is routing for.
* @param {AppPort} appPort AppPort to send and receive message from/to.
* @param {List} routes List of routes
*/
function EventRouter(name, context, appPort, routes) {
this.routes = routes || DEFAULT_ROUTES;
this.name = name;
this.appPort = appPort;
this.context = context;

// Starts to listen to the appPort.
appPort.onmessage = function(topic, event) {
event.topic = topic;
this._onMessage(event);
}.bind(this);
}

/**
* Route a message to the appropriate context.
*
* @param {String} topic Topic of the event.
* @param {Object} data The data to send trough.
*/
EventRouter.prototype.send = function(topic, data) {
// XXX Serialize data.
var route = this._findRoute(topic);
this.appPort.postMessage({
"from": this.name,
"to": route.to,
"topic": topic,
"callable": route.callable,
"data": data
});
};

EventRouter.prototype._onMessage = function(routeEvent){
if (routeEvent.to === this.name) {
// XXX Deserialize data.
this.context[routeEvent.callable](routeEvent.data);
} else if (routeEvent.via === this.name) {
// Relay the message to the real destination in case we are a proxy.
routeEvent.from = this.name;
this.appPort.postMessage(routeEvent);
}
};

EventRouter.prototype._findRoute = function(topic) {
var route = this.routes.filter(function(route) {
return route.topic === topic;
}).shift();

if (!route){
throw "Event route not found for topic " + topic;
}
return route;
};

return EventRouter;
})();

95 changes: 52 additions & 43 deletions static/js/worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global indexedDB, importScripts, SPA, HTTP, ContactsDB, SPADB,
CurrentUsers, loadConfig, payloads, Conversation, dump, ConversationList */
CurrentUsers, loadConfig, payloads, Conversation, dump, ConversationList,
EventRouter */
/* jshint unused:false */
"use strict";

Expand All @@ -15,7 +16,8 @@ importScripts(
'worker/users.js',
'worker/spa.js',
'worker/conversation.js',
'worker/conversationList.js'
'worker/conversationList.js',
'eventrouter.js'
);

var gConfig = loadConfig();
Expand Down Expand Up @@ -121,13 +123,13 @@ UserData.prototype = {

// This needs to be sent to the browser for Firefox 28 and earlier
// (pre bug 935640).
browserPort.postEvent('social.user-profile', userData);
browserPort.postMessage('social.user-profile', userData);

// XXX This could be simplified to just send the userName (and renamed).
tkWorker.ports.broadcastEvent('social.user-profile', userData);
tkWorker.router.send('social.user-profile', userData);

// This is needed for Firefox 29 onwards (post bug 935640).
browserPort.postEvent('social.ambient-notification', {
browserPort.postMessage('social.ambient-notification', {
iconURL: iconURL
});
}
Expand All @@ -149,7 +151,7 @@ function _setupSPA(spa) {
// but we don't have enough info for the worker for that yet
tkWorker.loadContacts();

tkWorker.ports.broadcastEvent("talkilla.spa-connected",
tkWorker.router.send("talkilla.spa-connected",
{"capabilities": data.capabilities});
});

Expand All @@ -165,15 +167,15 @@ function _setupSPA(spa) {
tkWorker.spa.usernameFieldType);
});

tkWorker.ports.broadcastEvent("talkilla.users", tkWorker.users.toArray());
tkWorker.router.send("talkilla.users", tkWorker.users.toArray());
});

spa.on("userJoined", function(userId) {
tkWorker.users.set(userId, {presence: "connected"},
tkWorker.spa.usernameFieldType);

tkWorker.ports.broadcastEvent("talkilla.users", tkWorker.users.toArray());
tkWorker.ports.broadcastEvent("talkilla.user-joined", userId);
tkWorker.router.send("talkilla.users", tkWorker.users.toArray());
tkWorker.router.send("talkilla.user-joined", userId);
});

spa.on("userLeft", function(userId) {
Expand All @@ -183,8 +185,8 @@ function _setupSPA(spa) {
tkWorker.users.set(userId, {presence: "disconnected"},
tkWorker.spa.usernameFieldType);

tkWorker.ports.broadcastEvent("talkilla.users", tkWorker.users.toArray());
tkWorker.ports.broadcastEvent("talkilla.user-left", userId);
tkWorker.router.send("talkilla.users", tkWorker.users.toArray());
tkWorker.router.send("talkilla.user-left", userId);
});

spa.on("offer", function(offerMsg) {
Expand Down Expand Up @@ -214,21 +216,21 @@ function _setupSPA(spa) {
});

spa.on("move-accept", function(moveAcceptMsg) {
tkWorker.ports.broadcastEvent("talkilla.move-accept",
tkWorker.router.send("talkilla.move-accept",
moveAcceptMsg);
});

spa.on("error", function(event) {
tkWorker.ports.broadcastEvent("talkilla.error", event);
tkWorker.router.send("talkilla.error", event);
});

spa.on("reconnection", function(reconnectionMsg) {
tkWorker.ports.broadcastEvent('talkilla.server-reconnection',
tkWorker.router.send('talkilla.server-reconnection',
reconnectionMsg);
});

spa.on("reauth-needed", function(event) {
tkWorker.ports.broadcastEvent('talkilla.reauth-needed');
tkWorker.router.send('talkilla.reauth-needed');
tkWorker.closeSession();
});

Expand All @@ -241,7 +243,7 @@ function _setupSPA(spa) {
var handlers = {
// SocialAPI events
'social.port-closing': function() {
// broadcastDebug won't work here, because the port is dead, so we
// debug won't work here, because the port is dead, so we
// use dump
dump("social.port-closing called; about to remove port. this = " + this);
tkWorker.ports.remove(this);
Expand All @@ -263,7 +265,7 @@ var handlers = {

// Talkilla events
'talkilla.contacts': function(event) {
tkWorker.ports.broadcastDebug('talkilla.contacts', event.data.contacts);
// XXX REFACTOR tkWorker.router.debug('talkilla.contacts', event.data.contacts);
tkWorker.updateContactsFromSource(event.data.contacts, event.data.source);
},

Expand Down Expand Up @@ -348,7 +350,8 @@ var handlers = {

tkWorker.spaDb.store(spec, function(err) {
if (err)
return tkWorker.ports.broadcastError("Error adding SPA", err);
dump("error adding spa" + err);
// XXX Refactor return tkWorker.router.broadcastError("Error adding SPA", err);

// Try starting the SPA even if there's an error adding it - at least
// the user can possibly get into it.
Expand Down Expand Up @@ -395,7 +398,7 @@ Port.prototype = {
* @param {String} error
*/
error: function(error) {
this.postEvent("talkilla.error", error);
this.postMessage("talkilla.error", error);
},

/**
Expand All @@ -415,13 +418,16 @@ Port.prototype = {
* @param {String} topic
* @param {Mixed} data
*/
postEvent: function(topic, data) {
postMessage: function(topic, data) {
// FIXME: for no obvious reason, this may eventually fail if the port is
// closed, while it should never be the case
this.port.postMessage({topic: topic, data: data});
}
};

/**
* Mimics the ports API but broadcast messages to a collection of ports.
**/
function PortCollection() {
this.ports = {};
}
Expand Down Expand Up @@ -467,28 +473,27 @@ PortCollection.prototype = {
* @param {String} topic
* @param {Mixed} data
*/
broadcastEvent: function(topic, data) {
postMessage: function(topic, data) {
for (var id in this.ports)
this.ports[id].postEvent(topic, data);
this.ports[id].postMessage(topic, data);
},

/**
* Broadcast debug informations to all ports.
*/
broadcastDebug: function(label, data) {
debug: function(label, data) {
if (!gConfig.DEBUG)
return;
for (var id in this.ports)
this.ports[id].postEvent("talkilla.debug", {label: label, data: data});
this.postMessage("talkilla.debug", {label: label, data: data});
},

/**
* Broadcasts an error message to all ports.
* @param {String} message
*/
broadcastError: function(message) {
error: function(message) {
this.postMessage("talkilla.error", message);
},

onmessage: function(cb) {
for (var id in this.ports)
this.ports[id].error(message);
this.ports[id].onmessage(cb);
}
};

Expand All @@ -515,6 +520,7 @@ function TkWorker(options) {
this.user = options.user || new UserData({}, gConfig);
this.users = options.users || new CurrentUsers();
this.ports = options.ports;
this.router = new EventRouter("Worker", this, this.ports);
this.initialized = false;
// XXX In future, this may switch to supporting multiple SPAs
this.spa = undefined;
Expand All @@ -537,7 +543,9 @@ TkWorker.prototype = {
// Now we're set up load the spa info
this.loadSPAs(function(err) {
if (err)
tkWorker.ports.broadcastError("Error loading spa specs");
dump("Error loading spa specs");
// XXX Refactor tkWorker.router.broadcastError("Error loading spa specs");


// Even if there were errors, assume initialization is complete
// so that we can continue to function.
Expand All @@ -557,21 +565,21 @@ TkWorker.prototype = {
onInitializationComplete: function(port) {
// Post to the port if specified, else to all ports.
if (port)
port.postEvent('talkilla.worker-ready');
port.postMessage('talkilla.worker-ready');
else
this.ports.broadcastEvent('talkilla.worker-ready');
this.router.send('talkilla.worker-ready');

if (this.spa && this.spa.connected) {
this.user.send();
if (port) {
port.postEvent("talkilla.spa-connected",
port.postMessage("talkilla.spa-connected",
{capabilities: this.spa.capabilities});
port.postEvent('talkilla.users', this.users.toArray());
port.postMessage('talkilla.users', this.users.toArray());
}
else {
this.ports.broadcastEvent("talkilla.spa-connected",
this.router.send("talkilla.spa-connected",
{capabilities: this.spa.capabilities});
this.ports.broadcastEvent('talkilla.users', this.users.toArray());
this.router.send('talkilla.users', this.users.toArray());
}
}
},
Expand All @@ -596,7 +604,8 @@ TkWorker.prototype = {
loadContacts: function(cb) {
this.contactsDb.all(function(err, contacts) {
if (err) {
this.ports.broadcastError(err);
// XXX Refactor this.router.broadcastError(err);
dump(err);
if (typeof cb === "function")
return cb.call(this, err);
return;
Expand Down Expand Up @@ -624,7 +633,7 @@ TkWorker.prototype = {

this.contactsDb.add(contact, function(err) {
if (err)
tkWorker.ports.broadcastError(err);
tkWorker.router.broadcastError(err);

callback(err);
});
Expand All @@ -644,7 +653,7 @@ TkWorker.prototype = {
*/
updateContactList: function(contacts) {
this.users.updateContacts(contacts, this.spa.usernameFieldType);
this.ports.broadcastEvent("talkilla.users", this.users.toArray());
this.router.send("talkilla.users", this.users.toArray());
},

/**
Expand Down Expand Up @@ -673,8 +682,8 @@ TkWorker.prototype = {
*/
loadSPAs: function(callback) {
this.spaDb.all(function(err, specs) {
tkWorker.ports.broadcastDebug("loaded specs", specs);

// XXX Refactor tkWorker.router.broadcastDebug("loaded specs", specs);
dump("loaded specs" + specs);
if (err) {
if (callback)
callback(err);
Expand Down
1 change: 1 addition & 0 deletions static/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<script src="vendor/bootstrap-2.1.1/js/bootstrap.js"></script>
<script src="vendor/state-machine-2.2.0.js"></script>
<script src="/config.js"></script>
<script src="js/eventrouter.js"></script>
<script src="js/validate.js"></script>
<script src="js/payloads.js"></script>
<script src="js/http.js"></script>
Expand Down
Loading