src/client.js
// returns a promise which resolves with the result of the send request
returns a promise which resolves with the result of the send request
returns a promise which resolves with the result of the send request
function _sendEvent(client, room, event, callback) {
    return q().then(function() {
        let encryptionPromise = null;
        if (client._crypto) {
            encryptionPromise = client._crypto.encryptEventIfNeeded(event, room);
        }
        if (encryptionPromise) {
            _updatePendingEventStatus(room, event, EventStatus.ENCRYPTING);
            encryptionPromise = encryptionPromise.then(function() {
                _updatePendingEventStatus(room, event, EventStatus.SENDING);
            });
        }
        return encryptionPromise;
    }).then(function() {
        let promise;
        if (client.scheduler) {
            promise = client.scheduler.queueEvent(event);
            if (promise && client.scheduler.getQueueForEvent(event).length > 1) {
                _updatePendingEventStatus(room, event, EventStatus.QUEUED);
            }
        }
        if (!promise) {
            promise = _sendEventHttpRequest(client, event);
        }
        return promise;
    }).then(function(res) {  
        if (room) {
            room.updatePendingEvent(event, EventStatus.SENT, res.event_id);
        }
        if (callback) {
            callback(null, res);
        }
        return res;
    }, function(err) {
        console.error("Error sending event", err.stack || err);
        try {
            _updatePendingEventStatus(room, event, EventStatus.NOT_SENT);
            event.error = err;
            if (callback) {
                callback(err);
            }
        } catch (err2) {
            console.error("Exception in error handler!", err2.stack || err);
        }
        throw err;
    });
}
function _sendEvent ( client , room , event , callback ) { return q ( ) . then ( function ( ) { let encryptionPromise = null ; if ( client . _crypto ) { encryptionPromise = client . _crypto . encryptEventIfNeeded ( event , room ) ; } if ( encryptionPromise ) { _updatePendingEventStatus ( room , event , EventStatus . ENCRYPTING ) ; encryptionPromise = encryptionPromise . then ( function ( ) { _updatePendingEventStatus ( room , event , EventStatus . SENDING ) ; } ) ; } return encryptionPromise ; } ) . then ( function ( ) { let promise ; if ( client . scheduler ) { promise = client . scheduler . queueEvent ( event ) ; if ( promise && client . scheduler . getQueueForEvent ( event ) . length > 1 ) { _updatePendingEventStatus ( room , event , EventStatus . QUEUED ) ; } } if ( ! promise ) { promise = _sendEventHttpRequest ( client , event ) ; } return promise ; } ) . then ( function ( res ) { if ( room ) { room . updatePendingEvent ( event , EventStatus . SENT , res . event_id ) ; } if ( callback ) { callback ( null , res ) ; } return res ; } , function ( err ) { console . error ( "Error sending event" , err . stack || err ) ; try { _updatePendingEventStatus ( room , event , EventStatus . NOT_SENT ) ; event . error = err ; if ( callback ) { callback ( err ) ; } } catch ( err2 ) { console . error ( "Exception in error handler!" , err2 . stack || err ) ; } throw err ; } ) ; }
returns a promise which resolves with the result of the send request
returns a promise which resolves with the result of the send request
// Add an extra q() to turn synchronous exceptions into promise rejections, // so that we can handle synchronous and asynchronous exceptions with the // same code path. // this event may be queued // if this returns a promsie then the scheduler has control now and will // resolve/reject when it is done. Internally, the scheduler will invoke // processFn which is set to this._sendEventHttpRequest so the same code // path is executed regardless. // event is processed FIFO so if the length is 2 or more we know // this event is stuck behind an earlier event. // the request was sent OK // the request failed to send.