From 1946db376632c68967d99dbb4031023d73fad015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BD=81=EF=BD=99=EF=BD=95=EF=BD=8D=EF=BD=89=C2=A0=20?= =?UTF-8?q?=EF=BD=99=EF=BD=95?= Date: Sun, 22 Jan 2017 18:32:40 -0800 Subject: [PATCH] Sync: Apply records asynchronously Fix https://github.com/brave/sync/issues/44 Auditors: @diracdeltas Test Plan: 1. Start Pyramid 1 which has a bunch of syncable data. Sync should enabled. 2. Setup Pyramid 2 (add `"start2": "node ./tools/start.js --user-data-dir=brave-development-2 --debug=5859 --enable-logging --v=0 --enable-extension-activity-logging --enable-sandbox-logging --enable-dcheck",` to package.json) and specify the Pyramid 1 Sync credentials. 3. When Pyramid 2 restarts and begins syncing data, Brave should be somewhat usable (not completely frozen for 60s). --- app/sync.js | 2 +- js/state/syncUtil.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/sync.js b/app/sync.js index 3529c9c0948..38b576ba449 100644 --- a/app/sync.js +++ b/app/sync.js @@ -201,7 +201,7 @@ module.exports.onSyncReady = (isFirstRun, e) => { return } log(`applying resolved ${records.length} ${categoryName}.`) - for (let record of records) { syncUtil.applySyncRecord(record) } + syncUtil.applySyncRecords(records) }) // Periodically poll for new records let startAt = appState.getIn(['sync', 'lastFetchTimestamp']) || 0 diff --git a/js/state/syncUtil.js b/js/state/syncUtil.js index f39b037157c..03715be31a9 100644 --- a/js/state/syncUtil.js +++ b/js/state/syncUtil.js @@ -155,6 +155,19 @@ module.exports.applySyncRecord = (record) => { } } +/** + * Apply several SyncRecords in a less blocking manner. + * @param {Array} records + */ +module.exports.applySyncRecords = (records) => { + if (!records || records.length === 0) { return } + setImmediate(() => { + const record = records.shift() + this.applySyncRecord(record) + this.applySyncRecords(records) + }) +} + /** * Given a category and SyncRecord, get an existing browser object. * Used to respond to IPC GET_EXISTING_OBJECTS.