Mailspring/packages/nylas-sync/sync-worker.js

168 lines
4.5 KiB
JavaScript
Raw Normal View History

const {
IMAPConnection,
PubsubConnector,
DatabaseConnector,
} = require('nylas-core');
2016-06-24 02:20:47 +08:00
const FetchCategoryList = require('./imap/fetch-category-list')
const FetchMessagesInCategory = require('./imap/fetch-messages-in-category')
2016-06-21 08:33:23 +08:00
//
// account.syncPolicy = {
// afterSync: 'idle',
// limit: {
// after: Date.now() - 7 * 24 * 60 * 60 * 1000,
// count: 10000,
// },
// interval: 60 * 1000,
// folderSyncOptions: {
// deepFolderScan: 5 * 60 * 1000,
2016-06-21 08:33:23 +08:00
// },
// expiration: Date.now() + 60 * 60 * 1000,
// }
2016-06-19 18:02:32 +08:00
2016-06-20 15:19:16 +08:00
class SyncWorker {
2016-06-21 08:33:23 +08:00
2016-06-20 15:19:16 +08:00
constructor(account, db) {
2016-06-21 08:33:23 +08:00
this._db = db;
this._conn = null;
this._account = account;
this._lastSyncTime = null;
2016-06-19 18:02:32 +08:00
2016-06-21 08:33:23 +08:00
this._syncTimer = null;
this._expirationTimer = null;
this._destroyed = false;
2016-06-19 18:02:32 +08:00
2016-06-21 08:33:23 +08:00
this.syncNow();
this._listener = PubsubConnector.observableForAccountChanges(account.id).subscribe(() => {
this.onAccountChanged();
});
}
cleanup() {
this._destroyed = true;
this._listener.dispose();
this._conn.end();
2016-06-21 08:33:23 +08:00
}
2016-06-19 18:02:32 +08:00
2016-06-21 08:33:23 +08:00
onAccountChanged() {
console.log("SyncWorker: Detected change to account. Reloading and syncing now.")
DatabaseConnector.forShared().then(({Account}) => {
Account.find({where: {id: this._account.id}}).then((account) => {
this._account = account;
this.syncNow();
})
});
2016-06-21 08:33:23 +08:00
}
onSyncDidComplete() {
const {afterSync} = this._account.syncPolicy;
if (afterSync === 'idle') {
this.getInboxCategory().then((inboxCategory) => {
this._conn.openBox(inboxCategory.name, true).then(() => {
console.log("SyncWorker: - Idling on inbox category");
2016-06-21 08:33:23 +08:00
});
2016-06-19 18:02:32 +08:00
});
2016-06-21 08:33:23 +08:00
} else if (afterSync === 'close') {
console.log("SyncWorker: - Closing connection");
2016-06-21 08:33:23 +08:00
this._conn.end();
this._conn = null;
} else {
throw new Error(`onSyncDidComplete: Unknown afterSync behavior: ${afterSync}`)
}
}
onConnectionIdleUpdate() {
this.syncNow();
2016-06-21 08:33:23 +08:00
}
2016-06-19 18:02:32 +08:00
2016-06-21 08:33:23 +08:00
getInboxCategory() {
return this._db.Category.find({where: {role: 'inbox'}})
}
ensureConnection() {
if (this._conn) {
return this._conn.connect();
}
return new Promise((resolve) => {
const settings = this._account.connectionSettings;
const credentials = this._account.decryptedCredentials();
if (!settings || !settings.imap_host) {
throw new Error("ensureConnection: There are no IMAP connection settings for this account.")
}
if (!credentials || !credentials.imap_username) {
throw new Error("ensureConnection: There are no IMAP connection credentials for this account.")
}
const conn = new IMAPConnection(this._db, Object.assign({}, settings, credentials));
2016-06-21 08:33:23 +08:00
conn.on('mail', () => {
this.onConnectionIdleUpdate();
})
conn.on('update', () => {
this.onConnectionIdleUpdate();
})
conn.on('queue-empty', () => {
});
this._conn = conn;
resolve(this._conn.connect());
});
2016-06-21 08:33:23 +08:00
}
2016-06-24 02:20:47 +08:00
fetchCategoryList() {
2016-06-21 08:33:23 +08:00
// todo: syncback operations belong here!
2016-06-24 02:20:47 +08:00
return this._conn.runOperation(new FetchCategoryList())
2016-06-21 08:33:23 +08:00
}
2016-06-24 02:20:47 +08:00
fetchMessagesInCategory() {
2016-06-20 15:19:16 +08:00
const {Category} = this._db;
const {folderSyncOptions} = this._account.syncPolicy;
2016-06-21 08:33:23 +08:00
return Category.findAll().then((categories) => {
const priority = ['inbox', 'drafts', 'sent'].reverse();
const categoriesToSync = categories.sort((a, b) =>
(priority.indexOf(a.role) - priority.indexOf(b.role)) * -1
2016-06-21 08:33:23 +08:00
)
// const filtered = sorted.filter(cat =>
// ['[Gmail]/All Mail', '[Gmail]/Trash', '[Gmail]/Spam'].includes(cat.name)
// )
return Promise.all(categoriesToSync.map((cat) =>
2016-06-24 02:20:47 +08:00
this._conn.runOperation(new FetchMessagesInCategory(cat, folderSyncOptions))
))
2016-06-20 15:19:16 +08:00
});
2016-06-19 18:02:32 +08:00
}
2016-06-21 08:33:23 +08:00
syncNow() {
clearTimeout(this._syncTimer);
2016-06-24 02:20:47 +08:00
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(() => {
2016-06-21 08:33:23 +08:00
this.onSyncDidComplete();
this.scheduleNextSync();
});
}
scheduleNextSync() {
const {interval} = this._account.syncPolicy;
if (interval) {
const target = this._lastSyncTime + interval;
console.log(`SyncWorker: Next sync scheduled for ${new Date(target).toLocaleString()}`);
this._syncTimer = setTimeout(() => {
this.syncNow();
}, target - Date.now());
2016-06-21 08:33:23 +08:00
}
}
2016-06-19 18:02:32 +08:00
}
module.exports = SyncWorker;