2016-07-28 05:29:35 +08:00
|
|
|
import _ from 'underscore'
|
2016-07-27 17:56:55 +08:00
|
|
|
import {NylasLongConnection, DatabaseStore} from 'nylas-exports'
|
|
|
|
|
|
|
|
const {Status} = NylasLongConnection
|
|
|
|
|
|
|
|
|
|
|
|
class DeltaStreamingConnection extends NylasLongConnection {
|
|
|
|
|
|
|
|
constructor(api, accountId, opts = {}) {
|
2016-09-23 02:57:00 +08:00
|
|
|
opts.debounceResultsInterval = 1000
|
2016-07-27 17:56:55 +08:00
|
|
|
opts.closeIfDataStopsInterval = 15 * 1000
|
|
|
|
super(api, accountId, opts)
|
|
|
|
|
2016-07-28 05:29:35 +08:00
|
|
|
const {isReady, getCursor, setCursor} = opts
|
|
|
|
this._isReady = isReady
|
2016-07-27 17:56:55 +08:00
|
|
|
this._getCursor = getCursor
|
|
|
|
this._setCursor = setCursor
|
|
|
|
|
2016-07-28 05:29:35 +08:00
|
|
|
// Update cursor when deltas received
|
|
|
|
this.onDeltas((deltas) => {
|
|
|
|
const last = _.last(deltas)
|
|
|
|
this._setCursor(last.cursor)
|
|
|
|
})
|
2016-07-27 17:56:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
deltaStreamingPath(cursor) {
|
|
|
|
return `/delta/streaming?cursor=${cursor}&exclude_folders=false&exclude_metadata=false&exclude_account=false`
|
|
|
|
}
|
|
|
|
|
|
|
|
hasCursor() {
|
|
|
|
return !!this._getCursor()
|
|
|
|
}
|
|
|
|
|
2016-07-28 05:29:35 +08:00
|
|
|
onError(err) {
|
|
|
|
if (err.message.indexOf('Invalid cursor') > 0) {
|
|
|
|
const error = new Error('Delta Connection: Cursor is invalid. Need to blow away local cache.')
|
|
|
|
NylasEnv.config.unset(`nylas.${this._accountId}.cursor`)
|
|
|
|
DatabaseStore._handleSetupError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 17:56:55 +08:00
|
|
|
latestCursor() {
|
|
|
|
const cursor = this._getCursor()
|
|
|
|
if (cursor) { return Promise.resolve(cursor) }
|
|
|
|
return this._api.makeRequest({
|
|
|
|
path: "/delta/latest_cursor",
|
|
|
|
accountId: this._accountId,
|
|
|
|
method: 'POST',
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
console.log(`Obtained stream cursor ${result.cursor}.`)
|
|
|
|
this._setCursor(result.cursor)
|
|
|
|
return Promise.resolve(result.cursor)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
onDeltas(callback) {
|
|
|
|
return this.onResults(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2016-07-28 05:29:35 +08:00
|
|
|
if (!this._isReady()) { return }
|
2016-07-27 17:56:55 +08:00
|
|
|
if (!this.canStart()) { return }
|
|
|
|
if (this._req != null) { return }
|
|
|
|
|
|
|
|
this.latestCursor().then((cursor) => {
|
|
|
|
if (this._status === Status.Ended) { return }
|
|
|
|
this._path = this.deltaStreamingPath(cursor)
|
|
|
|
super.start()
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(`Can't establish DeltaStreamingConnection: Error fetching latest cursor`, error)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DeltaStreamingConnection
|