You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			87 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			JavaScript
		
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			JavaScript
		
	
/* global Event, textsecure, window */
 | 
						|
 | 
						|
/* eslint-disable more/no-then */
 | 
						|
 | 
						|
// eslint-disable-next-line func-names
 | 
						|
(function() {
 | 
						|
  window.textsecure = window.textsecure || {};
 | 
						|
 | 
						|
  function SyncRequest(sender, receiver) {
 | 
						|
    if (
 | 
						|
      !(sender instanceof textsecure.MessageSender) ||
 | 
						|
      !(receiver instanceof textsecure.MessageReceiver)
 | 
						|
    ) {
 | 
						|
      throw new Error(
 | 
						|
        'Tried to construct a SyncRequest without MessageSender and MessageReceiver'
 | 
						|
      );
 | 
						|
    }
 | 
						|
    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);
 | 
						|
 | 
						|
    window.log.info('SyncRequest created. Sending contact sync message...');
 | 
						|
    sender
 | 
						|
      .sendRequestContactSyncMessage()
 | 
						|
      .then(() => {
 | 
						|
        window.log.info('SyncRequest now sending group sync messsage...');
 | 
						|
        return sender.sendRequestGroupSyncMessage();
 | 
						|
      })
 | 
						|
      .catch(error => {
 | 
						|
        window.log.error(
 | 
						|
          'SyncRequest error:',
 | 
						|
          error && error.stack ? error.stack : error
 | 
						|
        );
 | 
						|
      });
 | 
						|
    this.timeout = setTimeout(this.onTimeout.bind(this), 60000);
 | 
						|
  }
 | 
						|
 | 
						|
  SyncRequest.prototype = new textsecure.EventTarget();
 | 
						|
  SyncRequest.prototype.extend({
 | 
						|
    constructor: SyncRequest,
 | 
						|
    onContactSyncComplete() {
 | 
						|
      this.contactSync = true;
 | 
						|
      this.update();
 | 
						|
    },
 | 
						|
    onGroupSyncComplete() {
 | 
						|
      this.groupSync = true;
 | 
						|
      this.update();
 | 
						|
    },
 | 
						|
    update() {
 | 
						|
      if (this.contactSync && this.groupSync) {
 | 
						|
        this.dispatchEvent(new Event('success'));
 | 
						|
        this.cleanup();
 | 
						|
      }
 | 
						|
    },
 | 
						|
    onTimeout() {
 | 
						|
      if (this.contactSync || this.groupSync) {
 | 
						|
        this.dispatchEvent(new Event('success'));
 | 
						|
      } else {
 | 
						|
        this.dispatchEvent(new Event('timeout'));
 | 
						|
      }
 | 
						|
      this.cleanup();
 | 
						|
    },
 | 
						|
    cleanup() {
 | 
						|
      clearTimeout(this.timeout);
 | 
						|
      this.receiver.removeEventListener('contactsync', this.oncontact);
 | 
						|
      this.receiver.removeEventListener('groupSync', this.ongroup);
 | 
						|
      delete this.listeners;
 | 
						|
    },
 | 
						|
  });
 | 
						|
 | 
						|
  textsecure.SyncRequest = function SyncRequestWrapper(sender, receiver) {
 | 
						|
    const syncRequest = new SyncRequest(sender, receiver);
 | 
						|
    this.addEventListener = syncRequest.addEventListener.bind(syncRequest);
 | 
						|
    this.removeEventListener = syncRequest.removeEventListener.bind(
 | 
						|
      syncRequest
 | 
						|
    );
 | 
						|
  };
 | 
						|
 | 
						|
  textsecure.SyncRequest.prototype = {
 | 
						|
    constructor: textsecure.SyncRequest,
 | 
						|
  };
 | 
						|
})();
 |