Mailspring/internal_packages/deltas/lib/delta-streaming-in-memory-connection.es6
Evan Morikawa f2517ea7d7 refactor(name): rename worker-sync package to deltas
Summary:
This diff is a rename. No logical changes.

We used to have a set of files called `nylas-sync-worker`. In Old N1 these
used to have a lot of logic to slowly sync mail from our API. Since we
exclusively use local-sync via a soon-to-be-obsolesced delta stream, these
files really just manage the delta streaming connection.

It's incredibly confusing to have a set of files called worker-sync when
there's a sync-worker already in the codebase. This renames everything to
refer to them as account sync workers.

The reason I wanted this rename is because the IdentityStore on initial
launch triggers and fires a fairly scary-sounding
`Actions.refreshAllSyncWorkers()`. In reality all it does is
`Actions.refreshAllDeltaConnections()`. I'm also tired of staring at files
for a full minute before realizing that this is not the sync worker I was
  looking for.

Test Plan:
After rename, booted the app and ensured that deltas were coming through
for new mail and no errors were being thrown

Reviewers: halla, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3767
2017-01-25 10:08:23 -05:00

52 lines
1.3 KiB
JavaScript

/**
* This implements the same interface as the DeltaStreamingConnection
*/
class DeltaStreamingInMemoryConnection {
constructor(accountId, opts) {
this._accountId = accountId
this._getCursor = opts.getCursor
this._setCursor = opts.setCursor
this._onDeltas = opts.onDeltas
this._onStatusChanged = opts.onStatusChanged
this._status = "none"
}
onDeltas = (allDeltas = []) => {
const deltas = allDeltas.filter((d) => d.accountId === this._accountId);
this._onDeltas(deltas);
const last = deltas[deltas.length - 1]
if (last) this._setCursor(last.cursor);
}
get accountId() {
return this._accountId;
}
get status() {
return this._status;
}
setStatus(status) {
this._status = status
this._onStatusChanged(status)
}
start() {
this._disp = NylasEnv.localSyncEmitter.on("localSyncDeltas", this.onDeltas);
NylasEnv.localSyncEmitter.emit("startDeltasFor", {
cursor: this._getCursor() || 0,
accountId: this._accountId,
})
this.setStatus("connected")
}
end() {
if (this._disp && this._disp.dispose) this._disp.dispose()
NylasEnv.localSyncEmitter.emit("endDeltasFor", {
accountId: this._accountId,
})
this.setStatus("ended")
}
}
export default DeltaStreamingInMemoryConnection