From 87fe7fefa492934f0e081552d9f2c7a6a4113e63 Mon Sep 17 00:00:00 2001 From: Md Neyaz Ahmad Date: Tue, 12 Apr 2022 15:13:25 +0530 Subject: [PATCH 1/3] feat: add config option to disable sync events for some keys --- lib/Onyx.js | 6 ++++-- lib/storage/WebStorage.js | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/Onyx.js b/lib/Onyx.js index 5d7312134..3cde9822d 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..406d99f20 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,8 @@ const webStorage = { } const onyxKey = event.newValue; + if(_.contains(keysToDisableSyncEvents, onyxKey)) return; + Storage.getItem(onyxKey) .then(value => onStorageKeyChanged(onyxKey, value)); }); From ce5bd2c203fc054d53841f049e8276fbecaf7c0a Mon Sep 17 00:00:00 2001 From: mdneyazahmad <77761491+mdneyazahmad@users.noreply.github.com> Date: Sun, 17 Apr 2022 14:47:52 +0530 Subject: [PATCH 2/3] style: fix code style Co-authored-by: Marc Glasser --- lib/storage/WebStorage.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/storage/WebStorage.js b/lib/storage/WebStorage.js index 406d99f20..cf1514878 100644 --- a/lib/storage/WebStorage.js +++ b/lib/storage/WebStorage.js @@ -43,7 +43,9 @@ const webStorage = { } const onyxKey = event.newValue; - if(_.contains(keysToDisableSyncEvents, onyxKey)) return; + if (_.contains(keysToDisableSyncEvents, onyxKey)) { + return; + } Storage.getItem(onyxKey) .then(value => onStorageKeyChanged(onyxKey, value)); From 7df200bf0c263911bc4cdda19176926da0679920 Mon Sep 17 00:00:00 2001 From: Md Neyaz Ahmad Date: Sat, 23 Apr 2022 15:48:30 +0530 Subject: [PATCH 3/3] fix: lint error --- lib/Onyx.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Onyx.js b/lib/Onyx.js index 3cde9822d..a37d0ff9c 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -796,8 +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. - * + * @param {String[]} [option.keysToDisableSyncEvents=[]] Contains keys for which + * we want to disable sync event across tabs. * @example * Onyx.init({ * keys: ONYXKEYS,