From caf5380659e41269dcb8e7338c457cf4d371d967 Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Tue, 28 Mar 2017 09:07:43 -0700 Subject: [PATCH] [client-app] Fix importing SyncActivity In some places we were not accessing `.default` when using `require`, and when using `import` we couldn't really destructure unless we accessed `.default` too, or if the functions were regular exports instead of properties on a default exported object. This was causing our sync loop to error. To remain consistent, we always just `require` or `import` the SyncActivity singleton and access that --- .../sync-process-manager-spec.es6 | 2 +- .../src/local-api/routes/health.es6 | 6 +++--- .../sync-tasks/fetch-folder-list.imap.es6 | 10 +++++----- .../fetch-messages-in-folder.imap.es6 | 17 ++++++++-------- .../src/local-sync-worker/sync-worker.es6 | 20 +++++++++---------- .../src/message-processor/index.js | 4 ++-- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/packages/client-sync/spec/local-sync-worker/sync-process-manager-spec.es6 b/packages/client-sync/spec/local-sync-worker/sync-process-manager-spec.es6 index 8e9875944..0369bba4a 100644 --- a/packages/client-sync/spec/local-sync-worker/sync-process-manager-spec.es6 +++ b/packages/client-sync/spec/local-sync-worker/sync-process-manager-spec.es6 @@ -4,7 +4,7 @@ import LocalDatabaseConnector from '../../src/shared/local-database-connector' import SyncProcessManager from '../../src/local-sync-worker/sync-process-manager' import SyncActivity from '../../src/shared/sync-activity' -describe("SynccProcessManager", () => { +describe("SyncProcessManager", () => { beforeEach(async () => { global.Logger = createLogger() spyOn(IdentityStore, 'identity').andReturn(true) diff --git a/packages/client-sync/src/local-api/routes/health.es6 b/packages/client-sync/src/local-api/routes/health.es6 index 317a97b2a..2c223af3f 100644 --- a/packages/client-sync/src/local-api/routes/health.es6 +++ b/packages/client-sync/src/local-api/routes/health.es6 @@ -1,5 +1,5 @@ const Joi = require('joi'); -const {getLastSyncActivityForAccount, getLastSyncActivity} = require('../../shared/sync-activity').default +const SyncActivity = require('../../shared/sync-activity').default module.exports = (server) => { server.route({ @@ -12,7 +12,7 @@ module.exports = (server) => { handler: (request, reply) => { let response; try { - response = getLastSyncActivity() + response = SyncActivity.getLastSyncActivity() response = JSON.stringify(response) reply(response) } catch (err) { @@ -39,7 +39,7 @@ module.exports = (server) => { let response; try { const {accountId} = request.params - response = getLastSyncActivityForAccount(accountId) + response = SyncActivity.getLastSyncActivityForAccount(accountId) response = JSON.stringify(response) reply(response) } catch (err) { diff --git a/packages/client-sync/src/local-sync-worker/sync-tasks/fetch-folder-list.imap.es6 b/packages/client-sync/src/local-sync-worker/sync-tasks/fetch-folder-list.imap.es6 index 8edc1796d..abbd6a6a8 100644 --- a/packages/client-sync/src/local-sync-worker/sync-tasks/fetch-folder-list.imap.es6 +++ b/packages/client-sync/src/local-sync-worker/sync-tasks/fetch-folder-list.imap.es6 @@ -1,7 +1,7 @@ const {Provider} = require('isomorphic-core'); const SyncTask = require('./sync-task') const {localizedCategoryNames} = require('../sync-utils') -const {reportSyncActivity} = require('../../shared/sync-activity') +const SyncActivity = require('../../shared/sync-activity').default const GMAIL_ROLES_WITH_FOLDERS = ['all', 'trash', 'spam']; @@ -133,23 +133,23 @@ class FetchFolderListIMAP extends SyncTask { // `yield` async * runTask(db, imap) { const accountId = this._account.id - reportSyncActivity(accountId, `Fetching folder list`) + SyncActivity.reportSyncActivity(accountId, `Fetching folder list`) this._logger.log(`🔜 Fetching folder list`) this._db = db; const boxes = yield imap.getBoxes(); const {Folder, Label} = this._db; - reportSyncActivity(accountId, `Finding categories`) + SyncActivity.reportSyncActivity(accountId, `Finding categories`) const folders = yield Folder.findAll() const labels = yield Label.findAll() const all = [].concat(folders, labels); - reportSyncActivity(accountId, `Updating categories`) + SyncActivity.reportSyncActivity(accountId, `Updating categories`) await this._updateCategoriesWithBoxes(all, boxes); this._logger.log(`🔚 Fetching folder list done`) - reportSyncActivity(accountId, `Done fetching folder list`) + SyncActivity.reportSyncActivity(accountId, `Done fetching folder list`) } } diff --git a/packages/client-sync/src/local-sync-worker/sync-tasks/fetch-messages-in-folder.imap.es6 b/packages/client-sync/src/local-sync-worker/sync-tasks/fetch-messages-in-folder.imap.es6 index 796b92a76..0c50539c7 100644 --- a/packages/client-sync/src/local-sync-worker/sync-tasks/fetch-messages-in-folder.imap.es6 +++ b/packages/client-sync/src/local-sync-worker/sync-tasks/fetch-messages-in-folder.imap.es6 @@ -3,11 +3,10 @@ const {IMAPConnection} = require('isomorphic-core'); const {Capabilities} = IMAPConnection; const SyncTask = require('./sync-task') const MessageProcessor = require('../../message-processor') -const {reportSyncActivity} = require('../../shared/sync-activity').default +const SyncActivity = require('../../shared/sync-activity').default const MessageFlagAttributes = ['id', 'threadId', 'folderImapUID', 'unread', 'starred', 'folderImapXGMLabels'] const FETCH_ATTRIBUTES_BATCH_SIZE = 1000; -const FETCH_MESSAGE_BATCH_SIZE = 30; const MIN_MESSAGE_BATCH_SIZE = 30; const MAX_MESSAGE_BATCH_SIZE = 300; const BATCH_SIZE_PER_SELECT_SEC = 60; @@ -689,7 +688,7 @@ class FetchMessagesInFolderIMAP extends SyncTask { async * runTask(db, imap, syncWorker) { const accountId = this._db.accountId const folderName = this._folder.name - reportSyncActivity(accountId, `Starting folder: ${folderName}`) + SyncActivity.reportSyncActivity(accountId, `Starting folder: ${folderName}`) this._logger.log(`🔜 📂 ${this._folder.name}`) this._db = db; this._imap = imap; @@ -713,17 +712,17 @@ class FetchMessagesInFolderIMAP extends SyncTask { }) } - reportSyncActivity(accountId, `Checking if folder needs sync: ${folderName}`) + SyncActivity.reportSyncActivity(accountId, `Checking if folder needs sync: ${folderName}`) if (!this._shouldSyncFolder(latestBoxStatus)) { // Don't even attempt to issue an IMAP SELECT if there are absolutely no // updates this._logger.log(`🔚 📂 ${this._folder.name} has no updates at all - skipping sync`) - reportSyncActivity(accountId, `Done with folder: ${folderName}`) + SyncActivity.reportSyncActivity(accountId, `Done with folder: ${folderName}`) return; } - reportSyncActivity(accountId, `Checking what to fetch for folder: ${folderName}`) + SyncActivity.reportSyncActivity(accountId, `Checking what to fetch for folder: ${folderName}`) this._box = yield this._openMailboxAndEnsureValidity(); const shouldFetchMessages = this._shouldFetchMessages(this._box) @@ -731,19 +730,19 @@ class FetchMessagesInFolderIMAP extends SyncTask { // Do as little work as possible if (shouldFetchMessages) { - reportSyncActivity(accountId, `Fetching messages: ${folderName}`) + SyncActivity.reportSyncActivity(accountId, `Fetching messages: ${folderName}`) yield this._fetchNextMessageBatch() } else { this._logger.log(`🔚 📂 ${this._folder.name} has no new messages - skipping fetch messages`) } if (shouldFetchAttributes) { - reportSyncActivity(accountId, `Fetching attributes: ${folderName}`) + SyncActivity.reportSyncActivity(accountId, `Fetching attributes: ${folderName}`) yield this._fetchMessageAttributeChanges(); } else { this._logger.log(`🔚 📂 ${this._folder.name} has no attribute changes - skipping fetch attributes`) } this._logger.log(`🔚 📂 ${this._folder.name} done`) - reportSyncActivity(accountId, `Done with folder: ${folderName}`) + SyncActivity.reportSyncActivity(accountId, `Done with folder: ${folderName}`) } } diff --git a/packages/client-sync/src/local-sync-worker/sync-worker.es6 b/packages/client-sync/src/local-sync-worker/sync-worker.es6 index ac356372d..7cda3968b 100644 --- a/packages/client-sync/src/local-sync-worker/sync-worker.es6 +++ b/packages/client-sync/src/local-sync-worker/sync-worker.es6 @@ -19,7 +19,7 @@ import { import Interruptible from '../shared/interruptible' import SyncTaskFactory from './sync-task-factory'; import SyncbackTaskRunner from './syncback-task-runner' -import {reportSyncActivity} from '../shared/sync-activity' +import SyncActivity from '../shared/sync-activity' const {SYNC_STATE_RUNNING, SYNC_STATE_AUTH_FAILED, SYNC_STATE_ERROR} = Account @@ -451,7 +451,7 @@ class SyncWorker { // This function is interruptible. See Interruptible async * _performSync() { const accountId = this._account.id - reportSyncActivity(accountId, "Starting worker sync") + SyncActivity.reportSyncActivity(accountId, "Starting worker sync") yield this._account.update({syncError: null}); const syncbackTaskRunner = new SyncbackTaskRunner({ @@ -463,7 +463,7 @@ class SyncWorker { syncWorker: this, }) - reportSyncActivity(accountId, "Updating lingering tasks in progress") + SyncActivity.reportSyncActivity(accountId, "Updating lingering tasks in progress") // Step 1: Mark all "INPROGRESS-NOTRETRYABLE" tasks as failed, and all // "INPROGRESS-RETRYABLE tasks as new await syncbackTaskRunner.updateLingeringTasksInProgress() @@ -476,11 +476,11 @@ class SyncWorker { // (e.g. marking as unread or starred). We need to listen to that event for // when updates are performed from another mail client, but ignore // them when they are caused from within N1 to prevent unecessary interrupts - reportSyncActivity(accountId, "Getting new syncback tasks") + SyncActivity.reportSyncActivity(accountId, "Getting new syncback tasks") const tasks = yield syncbackTaskRunner.getNewSyncbackTasks() this._shouldIgnoreInboxFlagUpdates = true for (const task of tasks) { - reportSyncActivity(accountId, `Running syncback task: ${task.description()}`) + SyncActivity.reportSyncActivity(accountId, `Running syncback task: ${task.description()}`) await syncbackTaskRunner.runSyncbackTask(task) yield // Yield to allow interruption } @@ -488,14 +488,14 @@ class SyncWorker { // Step 3: Fetch the folder list. We need to run this before syncing folders // because we need folders to sync! - reportSyncActivity(accountId, "Running FetchFolderList task") + SyncActivity.reportSyncActivity(accountId, "Running FetchFolderList task") await this._runTask(SyncTaskFactory.create('FetchFolderList', {account: this._account})) yield // Yield to allow interruption // Step 4: Listen to new mail. We need to do this after we've fetched the // folder list so we can correctly find the inbox folder on the very first // sync loop - reportSyncActivity(accountId, "Lisening for new mail") + SyncActivity.reportSyncActivity(accountId, "Lisening for new mail") await this._listenForNewMail() yield // Yield to allow interruption @@ -507,14 +507,14 @@ class SyncWorker { // syncing slowly. This should only be done during initial sync. // TODO Also consider using multiple imap connections, 1 for inbox, one for the // rest - reportSyncActivity(accountId, "Getting folders to sync") + SyncActivity.reportSyncActivity(accountId, "Getting folders to sync") const sortedFolders = yield this._getFoldersToSync() for (const folder of sortedFolders) { - reportSyncActivity(accountId, `Running FetchMessagesInFolder task: ${folder.name}`) + SyncActivity.reportSyncActivity(accountId, `Running FetchMessagesInFolder task: ${folder.name}`) await this._runTask(SyncTaskFactory.create('FetchMessagesInFolder', {account: this._account, folder})) yield // Yield to allow interruption } - reportSyncActivity(accountId, "Done with worker sync") + SyncActivity.reportSyncActivity(accountId, "Done with worker sync") } // Public API: diff --git a/packages/client-sync/src/message-processor/index.js b/packages/client-sync/src/message-processor/index.js index a71a4cf87..5791aa6cd 100644 --- a/packages/client-sync/src/message-processor/index.js +++ b/packages/client-sync/src/message-processor/index.js @@ -9,7 +9,7 @@ const extractContacts = require('./extract-contacts'); const {MessageFactory} = require('isomorphic-core'); const LocalDatabaseConnector = require('../shared/local-database-connector'); const {BatteryStatusManager} = require('nylas-exports'); -const {reportSyncActivity} = require('../shared/sync-activity'); +const SyncActivity = require('../shared/sync-activity').default; const MAX_QUEUE_LENGTH = 500 const MAX_CPU_USE_ON_AC = 1.0; @@ -156,7 +156,7 @@ class MessageProcessor { const activity = `🔃 ✉️ (${folder.name}) "${messageValues.subject}" - ${messageValues.date}` logger.log(activity) - reportSyncActivity(accountId, activity) + SyncActivity.reportSyncActivity(accountId, activity) return processedMessage } catch (err) { await this._onError({imapMessage, desiredParts, folder, err, logger});