Skip to content

Commit

Permalink
Socket posting methods now take object param.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjNford committed Mar 13, 2012
1 parent 84cd374 commit 1a918a7
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 26 deletions.
15 changes: 13 additions & 2 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ window.App = {
bind: function() {

// Update notifications.
Socket.postMessage("window", "Notifier", "update");
Socket.postMessage({
namespace: "window",
literal: "Notifier",
method: "update"
});

// Create user link tooltips.
jQuery('.user_links [tooltip]').each(function() {
Expand Down Expand Up @@ -55,7 +59,14 @@ window.App = {
// Set log out click events.
jQuery('.user_links li[rel="log_out"]').on('click', function() {
Storage.clear();
Socket.postMessage("window", "Notifier", "update");

// Remove notifications.
Socket.postMessage({
namespace: "window",
literal: "Notifier",
method: "update"
});

App.close();
});

Expand Down
23 changes: 20 additions & 3 deletions js/follows.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@

if(context[type] == 0) {
getComplete();
Socket.postMessage(name, "display", "append", [context.id, null, name]);

Socket.postMessage({
namespace: name,
literal: "display",
method: "append",
args: [context.id, null, name]
});
}
else {
getFollows([], 1);
Expand Down Expand Up @@ -179,7 +185,13 @@
users[index].name = json.name;
users[index].created_at = json.created_at;
cacheBuffer = cacheBuffer.concat(users[index]);
Socket.postMessage(name, "display", "append", [context.id, users[index], name]);

Socket.postMessage({
namespace: name,
literal: "display",
method: "append",
args: [context.id, users[index], name]
});

if(index < users.length - 1) {
getUserNames(users, ++index);
Expand Down Expand Up @@ -207,7 +219,12 @@
* @param name Type to refresh.
*/
refresh: function(context, name) {
Socket.postTask(name, "load", "github", [context, OAuth2.getToken(), name]);
Socket.postTask({
namespace: name,
literal: "load",
method: "github",
args: [context, OAuth2.getToken(), name]
});
}
}
};
Expand Down
10 changes: 8 additions & 2 deletions js/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@
chrome.browserAction.setBadgeText({
text: count
});

Socket.postMessage("App", "update", "notifications", [count]);

// Send notification count to popup.
Socket.postMessage({
namespace: "App",
literal: "update",
method: "notifications",
args: [count]
});
},

/**
Expand Down
41 changes: 36 additions & 5 deletions js/repos.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,15 @@
jQuery.getJSON("https://api.github.com/repos/" + context.login + "/" + buffer[index].name, {access_token: token})
.success(function(json) {
buffer[index] = json;
Socket.postMessage("Repos", "display", "append", [context.id, json]);

Socket.postMessage({
namespace: "Repos",
literal: "display",
method: "append",
args: [context.id, json]
});


getParents(buffer, ++index);
})

Expand All @@ -312,7 +320,14 @@
// jQuery.getJSON("https://api.github.com/repos" + buffer[index].owner.login + "/" + buffer[index].name, {access_token: token})
// .success(function(json) {
// buffer[index] = json;
// Socket.postMessage("Repos", "display", "append", [context.id, json]);
//
// Socket.postMessage({
// namespace: "Repos",
// literal: "display",
// method: "append",
// args: [context.id, json]
// });
//
// getParents(buffer, ++index);
// });

Expand All @@ -322,15 +337,26 @@
});
}
else {
Socket.postMessage("Repos", "display", "append", [context.id, buffer[index]]);
Socket.postMessage({
namespace: "Repos",
literal: "display",
method: "append",
args: [context.id, buffer[index]]
});

getParents(buffer, ++index);
}
}
else {

// Account for user having no data.
if(buffer.length == 0) {
Socket.postMessage("Repos", "display", "append", [context.id, null]);
Socket.postMessage({
namespace: "Repos",
literal: "display",
method: "append",
args: [context.id, null]
});
}

Cache.save(context.id, "Repos", buffer);
Expand All @@ -345,7 +371,12 @@
* @param context Context requesting refresh.
*/
refresh: function(context) {
Socket.postTask("Repos", "load", "github", [context, OAuth2.getToken()]);
Socket.postTask({
namespace: "Repos",
literal: "load",
method: "github",
args: [context, OAuth2.getToken()]
});
}
}
};
Expand Down
7 changes: 6 additions & 1 deletion js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ window.Settings = {
Storage.save(Settings.PREF_NOTIFICATIONS, false);
Settings.notificationsButton.removeClass('positive').addClass('negative');
}
Socket.postMessage("window", "Notifier", "update");

Socket.postMessage({
namespace: "window",
literal: "Notifier",
method: "update"
});
});

// Toggle caching preference.
Expand Down
23 changes: 12 additions & 11 deletions js/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
*/
onMessage: function(msg) {
try {

// Increment task count if task message is posted to background.
if(msg.type === "task") {
Socket.tasks++;
Expand All @@ -84,7 +83,7 @@
catch(UnknownDestination) {
// Catch errors for unknown message destinations.
}
},
},

/**
* Post Message
Expand All @@ -96,11 +95,12 @@
* @param - method - Method of message destination.
* @param - args - Array of arguments to pass through socket.
*/
postMessage: function(namespace, literal, method, args) {
postMessage: function(msg) {
try {
this.port.postMessage({type: "message", namespace: namespace, literal: literal, method: method, args: args});
msg.type = "message";
this.port.postMessage(msg);
}
catch(portError) {
catch(SocketPostError) {
// Catch errors just in case.
}
},
Expand All @@ -116,17 +116,18 @@
* @param - method - Method of message destination.
* @param - args - Array of arguments to pass through socket.
*/
postTask: function(namespace, literal, method, args) {
postTask: function(msg) {

// Display loading notification for background task.
if(this.port.name == "popupToBackground") {
if(this.port.name === "popupToBackground") {
jQuery('.user_links.loading').show();
}

try {
this.port.postMessage({type: "task", namespace: namespace, literal: literal, method: method, args: args});
msg.type = "task";
this.port.postMessage(msg);
}
catch(disconnectedPortError) {
catch(SocketPostError) {
// Catch errors just in case.
}
},
Expand All @@ -138,11 +139,11 @@
* will decrement the background page task counter.
*/
postTaskComplete: function() {
if(--this.tasks == 0) {
if(--this.tasks === 0) {
try {
this.port.postMessage({type: "taskComplete"});
}
catch(disconnectedPortError) {
catch(SocketPostError) {
// Catch errors just in case.
}
}
Expand Down
16 changes: 14 additions & 2 deletions js/watched.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@
.success(function(json) {
if(json.length > 0) {
json = Watched.filter.data.removeUserRepos(json, context.login);
Socket.postMessage("Watched", "display", "append", [context.id, json]);

Socket.postMessage({
namespace: "Watched",
literal: "display",
method: "append",
args: [context.id, json]
});

getWatchedRepos(buffer.concat(json), ++page);
}
else {
Expand All @@ -165,7 +172,12 @@
* @param context Context requesting refresh.
*/
refresh: function(context) {
Socket.postTask("Watched", "load", "github", [context, OAuth2.getToken()]);
Socket.postTask({
namespace: "Watched",
literal: "load",
method: "github",
args: [context, OAuth2.getToken()]
});
}
}
};
Expand Down

0 comments on commit 1a918a7

Please sign in to comment.