diff --git a/packages/local-sync/src/local-sync-worker/sync-worker.es6 b/packages/local-sync/src/local-sync-worker/sync-worker.es6 index b47bd0a89..739ff23ad 100644 --- a/packages/local-sync/src/local-sync-worker/sync-worker.es6 +++ b/packages/local-sync/src/local-sync-worker/sync-worker.es6 @@ -10,6 +10,7 @@ const { NylasAPI, N1CloudAPI, NylasAPIRequest, + BatteryStatusManager, Account: {SYNC_STATE_RUNNING, SYNC_STATE_AUTH_FAILED, SYNC_STATE_ERROR}, } = require('nylas-exports') const Interruptible = require('../shared/interruptible') @@ -18,9 +19,9 @@ const SyncTaskFactory = require('./sync-task-factory'); const SyncbackTaskRunner = require('./syncback-task-runner').default; const LocalSyncDeltaEmitter = require('./local-sync-delta-emitter').default - -const SYNC_LOOP_INTERVAL_MS = 10 * 1000 -const MAX_SOCKET_TIMEOUT_MS = 10 * 60 * 1000 // 10 min +const AC_SYNC_LOOP_INTERVAL_MS = 10 * 1000 // 10 sec +const BATTERY_SYNC_LOOP_INTERVAL_MS = 5 * 60 * 1000 // 5 min +const MAX_SOCKET_TIMEOUT_MS = 10 * 60 * 1000 // 10 min class SyncWorker { constructor(account, db, parentManager) { @@ -422,7 +423,7 @@ class SyncWorker { // Encountered a permanent error // In this case we could do something fancier, but for now, just retry // with the normal sync loop interval - interval = SYNC_LOOP_INTERVAL_MS + interval = AC_SYNC_LOOP_INTERVAL_MS; } } else { // Continue syncing immediately if initial sync isn't done, or if the loop was @@ -432,7 +433,13 @@ class SyncWorker { this._interrupted || this._requireTokenRefresh ) - interval = shouldSyncImmediately ? 1 : SYNC_LOOP_INTERVAL_MS; + if (shouldSyncImmediately) { + interval = 1; + } else if (BatteryStatusManager.isBatteryCharging()) { + interval = AC_SYNC_LOOP_INTERVAL_MS; + } else { + interval = BATTERY_SYNC_LOOP_INTERVAL_MS; + } } diff --git a/packages/local-sync/src/message-processor/index.js b/packages/local-sync/src/message-processor/index.js index d5e66927a..c8a771560 100644 --- a/packages/local-sync/src/message-processor/index.js +++ b/packages/local-sync/src/message-processor/index.js @@ -8,7 +8,7 @@ const extractFiles = require('./extract-files'); const extractContacts = require('./extract-contacts'); const MessageFactory = require('../shared/message-factory') const LocalDatabaseConnector = require('../shared/local-database-connector'); - +const {BatteryStatusManager} = require('nylas-exports'); const MAX_QUEUE_LENGTH = 500 const MAX_CPU_USE_ON_AC = 1.0; @@ -23,14 +23,6 @@ class MessageProcessor { this._queueLength = 0 this._currentChunkSize = 0 this._currentChunkStart = Date.now(); - this._isBatteryCharging = true; - navigator.getBattery().then((battery) => { - this._isBatteryCharging = battery.charging; - battery.addEventListener('chargingchange', () => { - console.info('charge change', battery.charging); - this._isBatteryCharging = battery.charging - }); - }); } queueLength() { @@ -42,7 +34,7 @@ class MessageProcessor { } _maxCPUForProcessing() { - if (this._isBatteryCharging) { + if (BatteryStatusManager.isBatteryCharging()) { return MAX_CPU_USE_ON_AC; } return MAX_CPU_USE_ON_BATTERY;