session-desktop/libtextsecure/sync_request.js

66 lines
2 KiB
JavaScript
Raw Normal View History

/*
* vim: ts=4:sw=4:expandtab
*/
;(function () {
'use strict';
window.textsecure = window.textsecure || {};
function SyncRequest(sender, receiver) {
this.receiver = receiver;
this.oncontact = this.onContactSyncComplete.bind(this);
receiver.addEventListener('contactsync', this.oncontact);
this.ongroup = this.onGroupSyncComplete.bind(this);
receiver.addEventListener('groupsync', this.ongroup);
sender.sendRequestContactSyncMessage().then(function() {
sender.sendRequestGroupSyncMessage();
});
this.timeout = setTimeout(this.onTimeout.bind(this), 60000);
}
SyncRequest.prototype = new textsecure.EventTarget();
SyncRequest.prototype.extend({
constructor: SyncRequest,
onContactSyncComplete: function() {
this.contactSync = true;
this.update();
},
onGroupSyncComplete: function() {
this.groupSync = true;
this.update();
},
update: function() {
if (this.contactSync && this.groupSync) {
this.dispatchEvent(new Event('success'));
this.cleanup();
}
},
onTimeout: function() {
this.dispatchEvent(new Event('timeout'));
this.cleanup();
},
cleanup: function() {
clearTimeout(this.timeout);
this.receiver.removeEventListener('contactsync', this.oncontact);
this.receiver.removeEventListener('groupSync', this.ongroup);
delete this.listeners;
}
});
textsecure.SyncRequest = function(sender, receiver) {
var syncRequest = new SyncRequest(sender, receiver);
this.addEventListener = syncRequest.addEventListener.bind(syncRequest);
this.removeEventListener = syncRequest.removeEventListener.bind(syncRequest);
};
textsecure.SyncRequest.prototype = {
constructor: textsecure.SyncRequest
};
}());