diff --git a/lib/Onyx.js b/lib/Onyx.js index 5d7312134..a37d0ff9c 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -796,7 +796,8 @@ function mergeCollection(collectionKey, collection) { * @param {Boolean} [options.captureMetrics] Enables Onyx benchmarking and exposes the get/print/reset functions * @param {Boolean} [options.shouldSyncMultipleInstances] Auto synchronize storage events between multiple instances * of Onyx running in different tabs/windows. Defaults to true for platforms that support local storage (web/desktop) - * + * @param {String[]} [option.keysToDisableSyncEvents=[]] Contains keys for which + * we want to disable sync event across tabs. * @example * Onyx.init({ * keys: ONYXKEYS, @@ -812,6 +813,7 @@ function init({ maxCachedKeysCount = 1000, captureMetrics = false, shouldSyncMultipleInstances = Boolean(global.localStorage), + keysToDisableSyncEvents = [], } = {}) { if (captureMetrics) { // The code here is only bundled and applied when the captureMetrics is set @@ -840,7 +842,7 @@ function init({ .then(deferredInitTask.resolve); if (shouldSyncMultipleInstances && _.isFunction(Storage.keepInstancesSync)) { - Storage.keepInstancesSync((key, value) => { + Storage.keepInstancesSync(keysToDisableSyncEvents, (key, value) => { cache.set(key, value); keyChanged(key, value); }); diff --git a/lib/storage/WebStorage.js b/lib/storage/WebStorage.js index 0d2d9d128..cf1514878 100644 --- a/lib/storage/WebStorage.js +++ b/lib/storage/WebStorage.js @@ -16,10 +16,12 @@ const webStorage = { ...Storage, /** + * Contains keys for which we want to disable sync event across tabs. + * @param {String[]} keysToDisableSyncEvents * Storage synchronization mechanism keeping all opened tabs in sync * @param {function(key: String, data: *)} onStorageKeyChanged */ - keepInstancesSync(onStorageKeyChanged) { + keepInstancesSync(keysToDisableSyncEvents, onStorageKeyChanged) { // Override set, remove and clear to raise storage events that we intercept in other tabs this.setItem = (key, value) => Storage.setItem(key, value) .then(() => raiseStorageSyncEvent(key)); @@ -41,6 +43,10 @@ const webStorage = { } const onyxKey = event.newValue; + if (_.contains(keysToDisableSyncEvents, onyxKey)) { + return; + } + Storage.getItem(onyxKey) .then(value => onStorageKeyChanged(onyxKey, value)); });