Mailspring/internal_packages/worker-sync/lib/nylas-sync-worker-pool.es6
Evan Morikawa 5582782ddd refactor(delta): add a second delta stream, convert to es6, and cleanup
This reverts commit ee5609bdb0.

Updates to nylas sync worker to support multiple cursors

Convert NylasSyncWorker to es6

Convert NylasSyncWorkerPool to es6

Extract into deltaProcessor

Update names to NylasSyncWorker state

Working on spec fixes

More spec fixes

Delta stream refactor fixes
2016-11-30 13:42:10 -05:00

43 lines
1.3 KiB
JavaScript

import _ from 'underscore';
import {AccountStore} from 'nylas-exports'
import DeltaProcessor from './delta-processor'
import NylasSyncWorker from './nylas-sync-worker';
export default class NylasSyncWorkerPool {
constructor() {
this._workers = [];
AccountStore.listen(this._onAccountsChanged, this);
this._onAccountsChanged();
}
_onAccountsChanged() {
if (NylasEnv.inSpecMode()) { return; }
const accounts = AccountStore.accounts();
const workers = _.map(accounts, this._workerForAccount);
// Stop the workers that are not in the new workers list.
// These accounts are no longer in our database, so we shouldn't
// be listening.
const old = _.without(this._workers, ...workers);
for (const worker of old) { worker.cleanup(); }
this._workers = workers;
}
_workerForAccount = (account) => {
const worker = _.find(this._workers, c =>
c.account().id === account.id);
if (worker) { return worker; }
const newWorker = new NylasSyncWorker(account);
const streams = newWorker.deltaStreams();
for (const name of Object.keys(streams)) {
const stream = streams[name];
stream.onDeltas(DeltaProcessor.process);
}
this._workers.push(newWorker);
newWorker.start();
return newWorker
}
}