Mailspring/internal_packages/deltas/lib/account-delta-connection-pool.es6
Evan Morikawa 0be186302a refactor(account): remove unused accont refresh methods
Summary:
This removes `refreshHealthOfAccounts`, which I originally found because I
was trying to refactor it out of the IdentityStore. We need it gone from
the IdentityStore so that store can trigger frequently as things like the
usageLimits constantly update on it.

This also fixes T7520 where we'd get `ECONNREFUSED 127.0.0.1:2578` on
launch unnecessarily.

The concept of refreshing the health of accounts by occassionally polling
the /accounts endpoint is obsolete. This depends on
D3769 which entirely removes the /accounts endpoint
from K2.

Account health is pushed to us by `Actions.updateAccount` which is fired
directly from the main local-sync sync loop.

This also removes `refreshAccounts` and `Actions.refreshAllDeltaConnections`. These were fired by the Identity Store whenever it updated. We no longer need the Identity Store to tell us to reset the delta connections with our local-sync accounts. The delta connections will either be reset by our offline notification button, or when adding or removing an account. The Identity Store was a redundant mechanisms

Test Plan:
Manually verify things work with an existing account. Add a new account
and ensure sync starts. Remove an account and ensure it gets cleaned up.

Reviewers: halla, khamidou, mark, juan

Reviewed By: juan

Maniphest Tasks: T7520

Differential Revision: https://phab.nylas.com/D3770
2017-01-25 10:12:09 -05:00

47 lines
1.8 KiB
JavaScript

import _ from 'underscore';
import {AccountStore} from 'nylas-exports'
import AccountDeltaConnection from './account-delta-connection';
export default class AccountDeltaConnectionPool {
constructor() {
this._accountConnections = [];
AccountStore.listen(this._determineDeltaConnectionPool, this);
this._determineDeltaConnectionPool();
}
_existingConnectionsForAccount(account) {
return _.find(this._accountConnections, c => c.account().id === account.id);
}
_determineDeltaConnectionPool() {
// we need a function lock on this because on bootup, many legitimate
// events coming in may result in this function being called multiple times
// in quick succession, which can cause us to start multiple syncs for the
// same account
if (this._isBuildingDeltaConnections) return;
this._isBuildingDeltaConnections = true;
if (NylasEnv.inSpecMode()) { return; }
const origDeltaConnections = this._accountConnections;
const currentDeltaConnections = []
Promise.each(AccountStore.accounts(), (account) => {
const existingDeltaConnection = this._existingConnectionsForAccount(account)
if (existingDeltaConnection) {
currentDeltaConnections.push(existingDeltaConnection);
return Promise.resolve()
}
const newDeltaConnection = new AccountDeltaConnection(account);
return newDeltaConnection.loadStateFromDatabase().then(() => {
newDeltaConnection.start()
currentDeltaConnections.push(newDeltaConnection);
})
}).then(() => {
const oldDeltaConnections = _.difference(origDeltaConnections, currentDeltaConnections);
for (const deltaConnection of oldDeltaConnections) { deltaConnection.cleanup() }
this._accountConnections = currentDeltaConnections;
}).finally(() => {
this._isBuildingDeltaConnections = false;
})
}
}