syncbackMessageActions refactor

This commit is contained in:
Evan Morikawa 2016-06-28 17:12:45 -07:00
parent b867bcb31e
commit cea7783a5f
5 changed files with 24 additions and 24 deletions

View file

@ -1,7 +1,11 @@
module.exports = (sequelize, Sequelize) => {
const SyncbackRequest = sequelize.define('SyncbackRequest', {
type: Sequelize.STRING,
status: Sequelize.STRING,
status: {
type: Sequelize.ENUM("NEW", "SUCCEEDED", "FAILED"),
defaultValue: "NEW",
allowNull: false,
},
error: {
type: Sequelize.STRING,
get: function get() {

View file

@ -55,7 +55,7 @@ class PubsubConnector {
}
notify({accountId, type, data}) {
this.broadcastClient().publish(`channel-${accountId}`, {type, data});
this.broadcastClient().publish(`channel-${accountId}`, JSON.stringify({type, data}));
}
observe(accountId) {

View file

@ -40,14 +40,15 @@ class SyncWorker {
this._conn = null
}
_onMessage(msg = {}) {
switch(msg.type) {
_onMessage(msg) {
const {type, data} = JSON.parse(msg)
switch(type) {
case MessageTypes.ACCOUNT_UPDATED:
this._onAccountUpdated(); break;
case MessageTypes.SYNCBACK_REQUESTED:
this.syncNow(); break;
default:
throw new Error(`Invalid message: ${JSON.stringify(msg)}`)
throw new Error(`Invalid message: ${msg}`)
}
}
@ -121,20 +122,11 @@ class SyncWorker {
return this._conn.connect();
}
fetchCategoryList() {
return this._conn.runOperation(new FetchCategoryList())
}
syncbackMessageActions() {
return this._getAccount((account) =>
Promise.each(this._db.SyncbackRequest.findAll().then((reqs = []) =>
reqs.map((request) => {
const task = SyncbackTaskFactory.create(account, request);
if (!task) return Promise.reject("No Task")
return this._conn.runOperation(task)
})
))
)
const where = {where: {status: "NEW"}, limit: 100};
return this._db.SyncbackRequest.findAll(where)
.map((req) => SyncbackTaskFactory.create(this._account, req))
.each(this._conn.runOperation)
}
syncAllCategories() {
@ -163,7 +155,7 @@ class SyncWorker {
}
performSync() {
return this.fetchCategoryList()
return this._conn.runOperation(new FetchCategoryList())
.then(() => this.syncbackMessageActions())
.then(() => this.syncAllCategories())
}

View file

@ -4,12 +4,14 @@
*/
class SyncbackTaskFactory {
static create(account, syncbackRequest) {
if (syncbackRequest.type === "MoveToFolder") {
// TODO: Know it's IMAP from the account object.
const Task = require('./syncback_tasks/move-to-folder.imap');
return new Task(account, syncbackRequest)
let Task = null;
switch(syncbackRequest.type) {
case "MoveToFolder":
Task = require('./syncback_tasks/move-to-folder.imap'); break;
default:
throw new Error(`Invalid Task Type: ${syncbackRequest.type}`)
}
return null
return new Task(account, syncbackRequest)
}
}

View file

@ -9,6 +9,8 @@ class MoveToFolderIMAP {
}
run(db, imap) {
console.log("RUNNING MOVE TO FOLDER IMAP");
return Promise.resolve();
}
}
module.exports = MoveToFolderIMAP