Mailspring/internal_packages/worker-sync/lib/nylas-sync-worker-pool.es6

39 lines
1.2 KiB
Plaintext
Raw Normal View History

import _ from 'underscore';
import {AccountStore} from 'nylas-exports'
import NylasSyncWorker from './nylas-sync-worker';
export default class NylasSyncWorkerPool {
constructor() {
this._workers = [];
2016-12-01 06:55:09 +08:00
AccountStore.listen(this._determineWorkerPool, this);
this._determineWorkerPool();
}
2016-12-01 06:55:09 +08:00
_existingWorkerForAccount(account) {
return _.find(this._workers, c => c.account().id === account.id);
}
2016-12-01 06:55:09 +08:00
_determineWorkerPool() {
if (NylasEnv.inSpecMode()) { return; }
const origWorkers = this._workers;
const currentWorkers = []
Promise.each(AccountStore.accounts(), (account) => {
const existingWorker = this._existingWorkerForAccount(account)
if (existingWorker) {
currentWorkers.push(existingWorker);
return Promise.resolve()
}
const newWorker = new NylasSyncWorker(account);
return newWorker.loadStateFromDatabase().then(() => {
newWorker.start()
currentWorkers.push(newWorker);
})
}).then(() => {
const oldWorkers = _.difference(origWorkers, currentWorkers);
for (const worker of oldWorkers) { worker.cleanup() }
this._workers = currentWorkers;
})
}
}