Permalink
Browse files

Changed channel tests from object patterns to pure functions.

  • Loading branch information...
1 parent af4d65d commit cd1692bb63a975fa690d5551ad5e6ffc80826fd3 @bard committed Apr 9, 2009
@@ -72,13 +72,13 @@ window.addEventListener('DOMContentLoaded', function(event) {
if(window.isStandAlone()) {
// This must come after initialization, or channel won't be there.
- channel.on({
- event : 'message',
- direction : 'in',
- stanza : function(s) { return s.body != undefined; }
- }, function(message) {
- popUp();
- });
+ channel.on(
+ function(ev) (ev.name == 'message' &&
+ ev.dir == 'in' &&
+ ev.xml.body != undefined),
+ function(message) {
+ popUp();
+ });
srvWindowWatcher.registerNotification(windowObserver);
}
@@ -79,73 +79,58 @@ function init(xulPanels, xulTabs) {
channel = XMPP.createChannel();
- channel.on({
- event : 'message',
- direction : 'in',
- stanza : function(s) {
- // Allow non-error messages with readable body [1] or
- // error messages in general [2] but not auth requests [3]
- return (((s.@type != 'error' && s.body.text() != undefined) || // [1]
- (s.@type == 'error')) && // [2]
- (s.ns_http_auth::confirm == undefined)) // [3]
- }
- }, function(message) {
- cachePut(message);
- seenDisplayableMessage(message);
- });
-
- channel.on({
- event : 'message',
- direction : 'out',
- stanza : function(s) {
- // Allow messages with readable bodies [1], except if they
- // belong to a groupchat [2] (we show those as they come
- // back)
- return (s.body.text() != undefined &&
- s.@type != 'groupchat');
- }
- }, function(message) {
- cachePut(message);
- seenDisplayableMessage(message);
- });
-
- channel.on({
- event : 'message',
- direction : 'out',
- stanza : function(s) {
- return s.ns_chatstates::active != undefined;
- }
- }, function(message) {
- sentChatActivation(message);
- });
-
- channel.on({
- event : 'message',
- direction : 'in',
- stanza : function(s) {
- return (s.ns_event::x != undefined ||
- s.ns_chatstates::* != undefined);
- }
- }, function(message) {
- receivedChatState(message);
- });
-
- channel.on({
- event : 'presence',
- direction : 'out',
- stanza : function(s) {
- return s.ns_muc::x != undefined && s.@type != 'unavailable';
- }
- }, function(presence) { sentMUCJoinPresence(presence) });
-
- channel.on({
- event : 'presence',
- direction : 'in',
- stanza : function(s) {
- return (s.@type == undefined || s.@type == 'unavailable') &&
- s.ns_muc_user::x == undefined;
- }
- }, receivedContactPresence);
+ channel.on(
+ // Allow non-error messages with readable body or
+ // error messages in general
+ function(ev) (ev.name == 'message' &&
+ ev.dir == 'in' &&
+ ((ev.type != 'error' && ev.xml.body.text() != undefined) ||
+ (ev.type == 'error'))),
+ function(message) {
+ cachePut(message);
+ seenDisplayableMessage(message);
+ });
+
+ channel.on(
+ // Allow messages with readable bodies, except if they belong
+ // to a groupchat (we show those as they come back)
+ function(ev) (ev.name == 'message' &&
+ ev.dir == 'out' &&
+ ev.type != 'groupchat' &&
+ ev.xml.body.text() != undefined),
+ function(message) {
+ cachePut(message);
+ seenDisplayableMessage(message);
+ });
+
+ channel.on(
+ function(ev) (ev.name == 'message' &&
+ ev.dir == 'out' &&
+ ev.xml.ns_chatstates::active != undefined),
+ function(message) {
+ sentChatActivation(message);
+ });
+
+ channel.on(
+ function(ev) (ev.name == 'message' &&
+ ev.dir == 'in' &&
+ (ev.xml.ns_event::x != undefined ||
+ ev.xml.ns_chatstates::* != undefined)),
+ function(message) receivedChatState(message));
+
+ channel.on(
+ function(ev) (ev.name == 'presence' &&
+ ev.dir == 'out' &&
+ ev.type != 'unavailable' &&
+ ev.xml.ns_muc::x != undefined),
+ function(presence) sentMUCJoinPresence(presence));
+
+ channel.on(
+ function(ev) (ev.name == 'presence' &&
+ ev.dir == 'in' &&
+ (!ev.type || ev.type == 'unavailable') &&
+ ev.xml.ns_muc_user::x == undefined),
+ function(presence) receivedContactPresence(presence));
getTabs().addEventListener('select', function(event) {
// It's important that this be a call-by-name rather than a
@@ -52,41 +52,40 @@ accounts.init = function() {
this._channel = XMPP.createChannel();
- this._channel.on({
- event : 'presence',
- direction : 'out',
- stanza : function(s) {
- return (s.@type == undefined || s.@type == 'unavailable') &&
- s.ns_muc_user::x == undefined;
- }
- }, function(presence) {
- var xulAccount = $('#accounts .account[account="' + presence.account + '"]');
- xulAccount.setAttribute(
- 'availability', presence.stanza.@type == undefined ?
- 'available' : 'unavailable');
- xulAccount.setAttribute(
- 'show', presence.stanza.show.toString());
-
- $(xulAccount, '> .state-indicator').setAttribute(
- 'progress', 'false');
- });
+ this._channel.on(
+ function(ev) (ev.name == 'presence' &&
+ ev.dir == 'out' &&
+ (!ev || ev.type == 'unavailable') &&
+ !ev.to),
+ function(presence) {
+ var xulAccount = $('#accounts .account[account="' + presence.account + '"]');
+ xulAccount.setAttribute(
+ 'availability', presence.stanza.@type == undefined ?
+ 'available' : 'unavailable');
+ xulAccount.setAttribute(
+ 'show', presence.stanza.show.toString());
+
+ $(xulAccount, '> .state-indicator').setAttribute(
+ 'progress', 'false');
+ });
- this._channel.on({
- event : 'connector',
- }, function(connector) {
- var xulAccount = $('#accounts .account[account="' + connector.account + '"]');
- var xulStatus = $(xulAccount, '> .state-indicator');
- switch(connector.state) {
- case 'disconnected':
- case 'error':
- xulStatus.setAttribute('progress', 'false');
- xulAccount.setAttribute('availability', 'unavailable');
- break;
- case 'connecting':
- xulStatus.setAttribute('progress', 'true');
- break;
- }
- });
+ this._channel.on(
+ function(ev) (ev.name == 'connector' &&
+ ['disconnected', 'error', 'connecting'].indexOf(ev.state) != -1),
+ function(connector) {
+ var xulAccount = $('#accounts .account[account="' + connector.account + '"]');
+ var xulStatus = $(xulAccount, '> .state-indicator');
+ switch(connector.state) {
+ case 'disconnected':
+ case 'error':
+ xulStatus.setAttribute('progress', 'false');
+ xulAccount.setAttribute('availability', 'unavailable');
+ break;
+ case 'connecting':
+ xulStatus.setAttribute('progress', 'true');
+ break;
+ }
+ });
this.update();
@@ -60,9 +60,9 @@ contacts.init = function() {
this._channel.on(
function(ev) (ev.name == 'iq' &&
- ev.dir == 'in' &&
- ev.type == 'result' &&
- ev.xml..ns_vcard::PHOTO != null),
+ ev.dir == 'in' &&
+ ev.type == 'result' &&
+ ev.xml..ns_vcard::PHOTO != null),
function(iq) {
var xulConcreteContact = contacts._findConcreteContact(iq.account, XMPP.JID(iq.from).address);
var xulAvatar = $(xulConcreteContact, '^ .contact .avatar');
@@ -73,8 +73,8 @@ contacts.init = function() {
this._channel.on(
function(ev) (ev.name == 'iq' &&
- ev.dir == 'in' &&
- ev.xml.ns_roster::query != null),
+ ev.dir == 'in' &&
+ ev.xml.ns_roster::query != null),
function(iq) {
for each(var item in iq.stanza..ns_roster::item) {
contacts.updateContactItem(iq.account,
@@ -120,26 +120,26 @@ contacts.init = function() {
this._channel.on(
function(ev) (ev.name == 'presence' &&
- ev.dir == 'in' &&
- (ev.type == 'unavailable' || !ev.type)),
+ ev.dir == 'in' &&
+ (ev.type == 'unavailable' || !ev.type)),
function(presence) contacts.receivedContactPresence(presence));
this._channel.on(
function(ev) (ev.name == 'presence' &&
- ev.dir == 'in' &&
- ev.type == 'subscribe'),
+ ev.dir == 'in' &&
+ ev.type == 'subscribe'),
function(presence) contacts.receivedSubscriptionRequest(presence));
this._channel.on(
function(ev) (ev.name == 'connector' &&
- ev.state == 'active'),
+ ev.state == 'active'),
function(connector) {
$('#widget-contacts').setAttribute('minimized', 'false');
});
this._channel.on(
function(ev) (ev.name == 'connector' &&
- ev.state == 'disconnected'),
+ ev.state == 'disconnected'),
function(connector) {
if(XMPP.accounts.every(XMPP.isDown))
$('#widget-contacts').setAttribute('minimized', 'true');
@@ -101,27 +101,28 @@ function finish() {
function initNetworkReactions() {
channel = XMPP.createChannel();
- channel.on({
- event : 'connector',
- }, function(connector) {
- switch(connector.state) {
- case 'disconnected':
- case 'active':
- updateStatusIndicator();
- break;
- }
- });
-
- channel.on({
- event : 'message',
- direction : 'in',
- stanza : function(s) { return s.@type == 'chat' && s.body.text() != undefined; }
- }, function(message) {
- if(window == getMostRecentWindow() &&
- pref.getBoolPref('getAttentionOnMessage') &&
- isActive())
- window.getAttention();
- });
+ channel.on(
+ function(ev) ev.name == 'connector',
+ function(connector) {
+ switch(connector.state) {
+ case 'disconnected':
+ case 'active':
+ updateStatusIndicator();
+ break;
+ }
+ });
+
+ channel.on(
+ function(ev) (ev.name == 'message' &&
+ ev.dir == 'in' &&
+ ev.type == 'chat' &&
+ ev.xml.body.text != undefined),
+ function(message) {
+ if(window == getMostRecentWindow() &&
+ pref.getBoolPref('getAttentionOnMessage') &&
+ isActive())
+ window.getAttention();
+ });
}
@@ -32,22 +32,17 @@
window.addEventListener('load', function(event) {
const ns_http_auth = 'http://jabber.org/protocol/http-auth';
- channel.on({
- event : 'message',
- direction : 'in',
- stanza : function(s) {
- // Allow non-error messages with readable body [1] or
- // error messages in general [2] but not auth requests [3]
- return (((s.@type != 'error' && s.body.text() != undefined) || // [1]
- (s.@type == 'error')) && // [2]
- (s.ns_http_auth::confirm == undefined)) // [3]
- }
- }, function(message) {
- if(!_('button'))
- return;
- if(isHidden())
- _('button').setAttribute('pending-messages', 'true');
- });
+ channel.on(
+ function(ev) (ev.name == 'message' &&
+ ev.dir == 'in' &&
+ ((ev.type != 'error' && ev.xml.body.text() != undefined) ||
+ ev.type == 'error')),
+ function(message) {
+ if(!_('button'))
+ return;
+ if(isHidden())
+ _('button').setAttribute('pending-messages', 'true');
+ });
var xulPanels = _('panels');
xulPanels.addEventListener('custom/foreground', function(event) {
@@ -33,16 +33,14 @@
window.addEventListener('load', function(event) {
const ns_http_auth = 'http://jabber.org/protocol/http-auth';
- sameplace.channel.on({
- event : 'message',
- direction : 'in',
- stanza : function(s) {
- return s.ns_http_auth::confirm != undefined;
- }
- }, function(message) {
- if(window == sameplace.getMostRecentWindow())
- sameplace.receivedAuthConfirmRequest(message);
- });
+ sameplace.channel.on(
+ function(ev) (ev.name == 'message' &&
+ ev.dir == 'in' &&
+ ev.xml.ns_http_auth::confirm != undefined),
+ function(message) {
+ if(window == sameplace.getMostRecentWindow())
+ sameplace.receivedAuthConfirmRequest(message);
+ });
}, false);
sameplace.receivedAuthConfirmRequest = function(message) {
Oops, something went wrong.

0 comments on commit cd1692b

Please sign in to comment.