Update folderSyncProgressStore to work with C++ worker

This commit is contained in:
Ben Gotow 2017-07-04 22:38:53 -07:00
parent 95d2f3ae94
commit f9a3899663
5 changed files with 38 additions and 61 deletions

View file

@ -2,9 +2,6 @@ import _ from 'underscore';
import _str from 'underscore.string';
import {Utils, AccountStore, FolderSyncProgressStore, React} from 'nylas-exports';
const MONTH_SHORT_FORMATS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
export default class InitialSyncActivity extends React.Component {
static displayName = 'InitialSyncActivity';
@ -36,32 +33,16 @@ export default class InitialSyncActivity extends React.Component {
this.setState({syncState});
}
renderFolderProgress(name, progress, oldestProcessedDate) {
renderFolderProgress(name, progress) {
let status = 'busy';
let progressLabel = 'In Progress'
let syncedThrough = 'Syncing this past month';
let progressLabel = `In Progress (${Math.round(progress * 100)}%)`;
if (progress === 1) {
status = 'complete';
progressLabel = '';
syncedThrough = 'Up to date'
} else {
let month = oldestProcessedDate.getMonth();
let year = oldestProcessedDate.getFullYear();
const currentDate = new Date();
if (month !== currentDate.getMonth() || year !== currentDate.getFullYear()) {
// We're currently syncing in `month`, which mean's we've synced through all
// of the month *after* it.
month++;
if (month === 12) {
month = 0;
year++;
}
syncedThrough = `Synced through ${MONTH_SHORT_FORMATS[month]} ${year}`;
}
}
return (
<div className={`model-progress ${status}`} key={name} title={syncedThrough}>
<div className={`model-progress ${status}`} key={name}>
{_str.titleize(name)} <span className="progress-label">{progressLabel}</span>
</div>
)
@ -80,8 +61,8 @@ export default class InitialSyncActivity extends React.Component {
}
const {folderSyncProgress} = accountSyncState
let folderStates = _.map(folderSyncProgress, ({progress, oldestProcessedDate}, name) => {
return this.renderFolderProgress(name, progress, oldestProcessedDate)
let folderStates = _.map(folderSyncProgress, ({progress}, name) => {
return this.renderFolderProgress(name, progress)
})
if (folderStates.length === 0) {

View file

@ -25,6 +25,8 @@ class ActionBridgeCPP {
AccountStore = require('./stores/account-store').default;
AccountStore.listen(this.ensureClients, this);
this.ensureClients();
NylasEnv.onBeforeUnload(this.onBeforeUnload);
}
ensureClients() {
@ -109,6 +111,14 @@ class ActionBridgeCPP {
}
}
onBeforeUnload = () => {
for (const client of Object.values(this.clients)) {
client.kill();
}
this.clients = [];
return true;
}
sendMessageToAccount(accountId, json) {
if (!this.clients[accountId]) {
throw new Error(`No mailsync worker is running for account id ${accountId}.`);

View file

@ -87,9 +87,8 @@ export default class Category extends Model {
queryable: true,
modelKey: 'path',
}),
syncProgress: Attributes.Object({
modelKey: 'syncProgress',
jsonKey: 'sync_progress',
localStatus: Attributes.Object({
modelKey: 'localStatus',
}),
_refcount: Attributes.Number({
modelKey: '_refcount',

View file

@ -2,7 +2,7 @@ import _ from 'underscore'
import NylasStore from 'nylas-store'
import AccountStore from './account-store'
import CategoryStore from './category-store'
import Folder from '../models/folder';
/**
* FolderSyncProgressStore keeps track of the sync state per account, and will
@ -34,49 +34,31 @@ class FolderSyncProgressStore extends NylasStore {
}
activate() {
this.listenTo(AccountStore, () => this._onAccountsChanged())
this.listenTo(CategoryStore, () => this._onCategoriesChanged())
this._onCategoriesChanged()
this.listenTo(AccountStore, () => this._onRefresh())
this.listenTo(CategoryStore, () => this._onRefresh())
this._onRefresh()
}
_onAccountsChanged() {
const currentIds = Object.keys(this._statesByAccount)
const nextIds = AccountStore.accountIds()
const removedIds = _.difference(currentIds, nextIds)
_onRefresh() {
const accountIds = AccountStore.accountIds();
removedIds.forEach((accountId) => {
if (this._statesByAccount[accountId]) {
delete this._statesByAccount[accountId]
this._triggerDebounced()
}
})
}
this._statesByAccount = {};
_onCategoriesChanged() {
const accountIds = AccountStore.accountIds()
for (const accountId of accountIds) {
const folders = CategoryStore.categories(accountId)
.filter(cat => cat.object === 'folder')
const folders = CategoryStore.categories(accountId).filter(cat => cat instanceof Folder)
const folderSyncProgress = {};
const updates = {}
for (const folder of folders) {
const name = folder.name || folder.displayName
const {approxPercentComplete, approxTotal, oldestProcessedDate} = folder.syncProgress || {};
updates[name] = {
progress: approxPercentComplete || 0,
total: approxTotal || 0,
oldestProcessedDate: oldestProcessedDate ? new Date(oldestProcessedDate) : new Date(),
const name = folder.name || folder.displayName;
const {uidnext, syncedMinUID} = folder.localStatus || {};
folderSyncProgress[name] = {
progress: 1.0 - (syncedMinUID - 1) / uidnext,
total: uidnext,
}
}
this._updateState(accountId, {folderSyncProgress: updates})
}
}
_updateState(accountId, nextState) {
const currentState = this._statesByAccount[accountId] || {}
if (_.isEqual(currentState, nextState)) { return }
this._statesByAccount[accountId] = nextState
this._statesByAccount[accountId] = {folderSyncProgress};
}
this._triggerDebounced()
}

View file

@ -57,6 +57,7 @@ export default class MailsyncProcess extends EventEmitter {
reject(err, buffer);
});
this._proc.on('close', (code) => {
console.log(`SyncWorker exited mode ${mode} with code ${code}`);
try {
const lastLine = buffer.toString('UTF-8').split('\n').pop();
const response = JSON.parse(lastLine);
@ -72,6 +73,10 @@ export default class MailsyncProcess extends EventEmitter {
});
}
kill() {
this._proc.kill();
}
sync() {
this._spawnProcess('sync');
let buffer = "";