Refactor onServerEvent for sanity.
This commit is contained in:
@@ -491,6 +491,61 @@ Cosmopolite.prototype.onSocketMessage_ = function(msg) {
|
|||||||
this.onServerEvent_(JSON.parse(msg.data));
|
this.onServerEvent_(JSON.parse(msg.data));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback on receiving a 'login' event from the server
|
||||||
|
*
|
||||||
|
* @param {!Object} e Event object
|
||||||
|
*/
|
||||||
|
Cosmopolite.prototype.onLogin_ = function(e) {
|
||||||
|
if ('onLogin' in this.callbacks_) {
|
||||||
|
this.callbacks_['onLogin'](
|
||||||
|
e['google_user'],
|
||||||
|
this.urlPrefix_ + '/auth/logout');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback on receiving a 'logout' event from the server
|
||||||
|
*
|
||||||
|
* @param {!Object} e Event object
|
||||||
|
*/
|
||||||
|
Cosmopolite.prototype.onLogout_ = function(e) {
|
||||||
|
if ('onLogout' in this.callbacks_) {
|
||||||
|
this.callbacks_['onLogout'](
|
||||||
|
this.urlPrefix_ + '/auth/login');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback on receiving a 'message' event from the server
|
||||||
|
*
|
||||||
|
* @param {!Object} e Event object
|
||||||
|
*/
|
||||||
|
Cosmopolite.prototype.onMessage_ = function(e) {
|
||||||
|
var subscription = this.subscriptions_[e['subject']['name']];
|
||||||
|
if (!subscription) {
|
||||||
|
console.log(
|
||||||
|
this.loggingPrefix_(),
|
||||||
|
'message from unrecognized subject:', e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var duplicate = subscription.messages.some(function(message) {
|
||||||
|
return message['id'] == e.id;
|
||||||
|
});
|
||||||
|
if (duplicate) {
|
||||||
|
console.log(this.loggingPrefix_(), 'duplicate message:', e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
e['message'] = JSON.parse(e['message']);
|
||||||
|
subscription.messages.push(e);
|
||||||
|
if (e['key']) {
|
||||||
|
subscription.keys[e['key']] = e;
|
||||||
|
}
|
||||||
|
if ('onMessage' in this.callbacks_) {
|
||||||
|
this.callbacks_['onMessage'](e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback for Cosmopolite event (received via channel or pseudo-channel)
|
* Callback for Cosmopolite event (received via channel or pseudo-channel)
|
||||||
*
|
*
|
||||||
@@ -505,41 +560,13 @@ Cosmopolite.prototype.onServerEvent_ = function(e) {
|
|||||||
}
|
}
|
||||||
switch (e['event_type']) {
|
switch (e['event_type']) {
|
||||||
case 'login':
|
case 'login':
|
||||||
if ('onLogin' in this.callbacks_) {
|
this.onLogin_(e);
|
||||||
this.callbacks_['onLogin'](
|
|
||||||
e['google_user'],
|
|
||||||
this.urlPrefix_ + '/auth/logout');
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'logout':
|
case 'logout':
|
||||||
if ('onLogout' in this.callbacks_) {
|
this.onLogout_(e);
|
||||||
this.callbacks_['onLogout'](
|
|
||||||
this.urlPrefix_ + '/auth/login');
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'message':
|
case 'message':
|
||||||
var subscription = this.subscriptions_[e['subject']['name']];
|
this.onMessage_(e);
|
||||||
if (!subscription) {
|
|
||||||
console.log(
|
|
||||||
this.loggingPrefix_(),
|
|
||||||
'message from unrecognized subject:', e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
var duplicate = subscription.messages.some(function(message) {
|
|
||||||
return message['id'] == e.id;
|
|
||||||
});
|
|
||||||
if (duplicate) {
|
|
||||||
console.log(this.loggingPrefix_(), 'duplicate message:', e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
e['message'] = JSON.parse(e['message']);
|
|
||||||
subscription.messages.push(e);
|
|
||||||
if (e['key']) {
|
|
||||||
subscription.keys[e['key']] = e;
|
|
||||||
}
|
|
||||||
if ('onMessage' in this.callbacks_) {
|
|
||||||
this.callbacks_['onMessage'](e);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Client out of date? Force refresh?
|
// Client out of date? Force refresh?
|
||||||
|
|||||||
Reference in New Issue
Block a user