mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-09-24 01:06:07 +08:00
b47e39e229
Summary: We were mostly ignoring errors in our delta streaming connections. This commit makes it so that if we get an authentication error from n1Cloud we mark the account as invalid so users know to re authenticate, and can properly receive metadata deltas If the error is not an authentication error, it automatically retries the delta connection. Addresses T7744 Test Plan: manual Reviewers: evan, spang, halla Reviewed By: halla Differential Revision: https://phab.nylas.com/D3813
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
import _ from 'underscore'
|
|
import {NylasLongConnection, DatabaseStore} from 'nylas-exports'
|
|
|
|
class DeltaStreamingConnection extends NylasLongConnection {
|
|
constructor(api, accountId, opts = {}) {
|
|
opts.throttleResultsInterval = 1000
|
|
opts.closeIfDataStopsInterval = 15 * 1000
|
|
super(api, accountId, opts)
|
|
|
|
const {getCursor, setCursor} = opts
|
|
this._getCursor = getCursor
|
|
this._setCursor = setCursor
|
|
|
|
// Update cursor when deltas received
|
|
this.onDeltas((deltas) => {
|
|
if (opts.onDeltas) opts.onDeltas(deltas);
|
|
const last = _.last(deltas)
|
|
this._setCursor(last.cursor)
|
|
})
|
|
}
|
|
|
|
_deltaStreamingPath(cursor) {
|
|
return `/delta/streaming?cursor=${cursor}`
|
|
}
|
|
|
|
onError(err) {
|
|
if (err.message.includes('Invalid cursor')) {
|
|
const error = new Error('Delta Connection: Cursor is invalid. Need to blow away local cache.');
|
|
this._setCursor(0)
|
|
DatabaseStore._handleSetupError(error)
|
|
}
|
|
this._onError(err)
|
|
}
|
|
|
|
onDeltas(callback) {
|
|
return this.onResults(callback)
|
|
}
|
|
|
|
start() {
|
|
this._path = this._deltaStreamingPath(this._getCursor() || 0)
|
|
super.start()
|
|
}
|
|
}
|
|
|
|
export default DeltaStreamingConnection
|