Rename Refresh to Sync

This commit is contained in:
Evan Morikawa 2016-06-23 12:20:47 -06:00
parent 8e692982bb
commit 1460a0ae9f
3 changed files with 52 additions and 49 deletions

View file

@ -1,6 +1,6 @@
class RefreshMailboxesOperation {
class FetchCategoryList {
description() {
return `RefreshMailboxesOperation`;
return `FetchCategoryList`;
}
_roleForMailbox(boxName, box) {
@ -86,4 +86,4 @@ class RefreshMailboxesOperation {
}
}
module.exports = RefreshMailboxesOperation;
module.exports = FetchCategoryList;

View file

@ -5,17 +5,17 @@ const {Capabilities} = IMAPConnection;
const MessageFlagAttributes = ['id', 'CategoryUID', 'unread', 'starred']
class SyncMailboxOperation {
class FetchMessagesInCategory {
constructor(category, options) {
this._category = category;
this._options = options;
if (!this._category) {
throw new Error("SyncMailboxOperation requires a category")
throw new Error("FetchMessagesInCategory requires a category")
}
}
description() {
return `SyncMailboxOperation (${this._category.name} - ${this._category.id})\n Options: ${JSON.stringify(this._options)}`;
return `FetchMessagesInCategory (${this._category.name} - ${this._category.id})\n Options: ${JSON.stringify(this._options)}`;
}
_getLowerBoundUID(count) {
@ -177,36 +177,42 @@ class SyncMailboxOperation {
});
}
_shouldRunDeepScan() {
const {timeDeepScan} = this._category.syncState;
return Date.now() - (timeDeepScan || 0) > this._options.deepFolderScan
}
_runDeepScan(range) {
const {Message} = this.db;
return this._imap.fetchUIDAttributes(range).then((remoteUIDAttributes) =>
Message.findAll({
where: {CategoryId: this._category.id},
attributes: MessageFlagAttributes,
}).then((localMessageAttributes) =>
Promise.props({
upserts: this._createAndUpdateMessages(remoteUIDAttributes, localMessageAttributes),
deletes: this._removeDeletedMessages(remoteUIDAttributes, localMessageAttributes),
})
).then(() => {
return this.updateCategorySyncState({
highestmodseq: this._box.highestmodseq,
timeDeepScan: Date.now(),
timeShallowScan: Date.now(),
});
})
);
}
_fetchChangesToMessages() {
const {highestmodseq, timeDeepScan} = this._category.syncState;
const {highestmodseq} = this._category.syncState;
const nextHighestmodseq = this._box.highestmodseq;
const {Message} = this._db;
const {limit} = this._options;
const range = `${this._getLowerBoundUID(limit)}:*`;
const range = `${this._getLowerBoundUID(this._options.limit)}:*`;
console.log(` - fetching changes to messages ${range}`)
const shouldRunDeepScan = Date.now() - (timeDeepScan || 0) > this._options.deepFolderScan
if (shouldRunDeepScan) {
return this._imap.fetchUIDAttributes(range).then((remoteUIDAttributes) =>
Message.findAll({
where: {CategoryId: this._category.id},
attributes: MessageFlagAttributes,
}).then((localMessageAttributes) =>
Promise.props({
upserts: this._createAndUpdateMessages(remoteUIDAttributes, localMessageAttributes),
deletes: this._removeDeletedMessages(remoteUIDAttributes, localMessageAttributes),
})
).then(() => {
return this.updateCategorySyncState({
highestmodseq: nextHighestmodseq,
timeDeepScan: Date.now(),
timeShallowScan: Date.now(),
});
})
);
if (this._shouldRunDeepScan()) {
return this._runDeepScan(range)
}
let shallowFetch = null;
@ -222,7 +228,7 @@ class SyncMailboxOperation {
}
return shallowFetch.then((remoteUIDAttributes) =>
Message.findAll({
this._db.Message.findAll({
where: {CategoryId: this._category.id},
attributes: MessageFlagAttributes,
}).then((localMessageAttributes) =>
@ -257,4 +263,4 @@ class SyncMailboxOperation {
}
}
module.exports = SyncMailboxOperation;
module.exports = FetchMessagesInCategory;

View file

@ -4,8 +4,8 @@ const {
DatabaseConnector,
} = require('nylas-core');
const RefreshMailboxesOperation = require('./imap/refresh-mailboxes-operation')
const SyncMailboxOperation = require('./imap/sync-mailbox-operation')
const FetchCategoryList = require('./imap/fetch-category-list')
const FetchMessagesInCategory = require('./imap/fetch-messages-in-category')
//
// account.syncPolicy = {
// afterSync: 'idle',
@ -111,12 +111,12 @@ class SyncWorker {
});
}
queueOperationsForUpdates() {
fetchCategoryList() {
// todo: syncback operations belong here!
return this._conn.runOperation(new RefreshMailboxesOperation())
return this._conn.runOperation(new FetchCategoryList())
}
queueOperationsForFolderSyncs() {
fetchMessagesInCategory() {
const {Category} = this._db;
const {folderSyncOptions} = this._account.syncPolicy;
@ -131,24 +131,21 @@ class SyncWorker {
// )
return Promise.all(categoriesToSync.map((cat) =>
this._conn.runOperation(new SyncMailboxOperation(cat, folderSyncOptions))
)).then(() => {
this._lastSyncTime = Date.now();
});
this._conn.runOperation(new FetchMessagesInCategory(cat, folderSyncOptions))
))
});
}
syncNow() {
clearTimeout(this._syncTimer);
this.ensureConnection().then(() =>
this.queueOperationsForUpdates().then(() =>
this.queueOperationsForFolderSyncs()
)
).catch((err) => {
// Sync has failed for some reason. What do we do?!
console.error(err);
}).finally(() => {
this.ensureConnection()
.then(this.fetchCategoryList.bind(this))
.then(this.syncbackMessageActions.bind(this))
.then(this.fetchMessagesInCategory.bind(this))
.then(() => { this._lastSyncTime = Date.now() })
.catch(console.error)
.finally(() => {
this.onSyncDidComplete();
this.scheduleNextSync();
});