From f9138af09612766cf3dcc9b8a18acaa75daefb18 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 11 Jul 2023 16:36:09 -1000 Subject: [PATCH 1/3] Add memoryOnlyKeys setter option to Onyx --- lib/Onyx.js | 9 +++++++++ lib/storage/providers/LocalForage.js | 17 +++++++++++++++++ lib/storage/providers/SQLiteStorage.js | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/lib/Onyx.js b/lib/Onyx.js index eb3124fb..a2f14c29 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -1216,6 +1216,14 @@ function update(data) { return clearPromise.then(() => Promise.all(_.map(promises, p => p()))); } +/** + * When set these keys will not be persisted to storage + * @param {string[]} keyList + */ +function setMemoryOnlyKeys(keyList) { + Storage.setMemoryOnlyKeys(keyList); +} + /** * Initialize the store with actions and listening for storage events * @@ -1304,6 +1312,7 @@ const Onyx = { removeFromEvictionBlockList, isSafeEvictionKey, METHOD, + setMemoryOnlyKeys, }; /** diff --git a/lib/storage/providers/LocalForage.js b/lib/storage/providers/LocalForage.js index 338d1abe..677208df 100644 --- a/lib/storage/providers/LocalForage.js +++ b/lib/storage/providers/LocalForage.js @@ -8,6 +8,7 @@ import localforage from 'localforage'; import _ from 'underscore'; import {extendPrototype} from 'localforage-removeitems'; import SyncQueue from '../../SyncQueue'; +import * as Str from '../../Str'; import fastMerge from '../../fastMerge'; extendPrototype(localforage); @@ -16,6 +17,11 @@ localforage.config({ name: 'OnyxDB', }); +/** + * Keys that will not ever be persisted to disk. + */ +let memoryOnlyKeys = []; + const provider = { /** * Writing very quickly to IndexedDB causes performance issues and can lock up the page and lead to jank. @@ -23,6 +29,10 @@ const provider = { * to the next. */ setItemQueue: new SyncQueue(({key, value, shouldMerge}) => { + if (_.find(memoryOnlyKeys, noCacheKey => Str.startsWith(key, noCacheKey))) { + return Promise.resolve(); + } + if (shouldMerge) { return localforage.getItem(key) .then((existingValue) => { @@ -125,6 +135,13 @@ const provider = { setItem(key, value) { return this.setItemQueue.push({key, value}); }, + + /** + * @param {string[]} keyList + */ + setMemoryOnlyKeys(keyList) { + memoryOnlyKeys = keyList; + }, }; export default provider; diff --git a/lib/storage/providers/SQLiteStorage.js b/lib/storage/providers/SQLiteStorage.js index 2fd865fa..c1901636 100644 --- a/lib/storage/providers/SQLiteStorage.js +++ b/lib/storage/providers/SQLiteStorage.js @@ -128,6 +128,11 @@ const provider = { * @returns {Promise} */ clear: () => db.executeAsync('DELETE FROM keyvaluepairs;', []), + + /** + * Noop on mobile for now. + */ + setMemoryOnlyKeys: () => {}, }; export default provider; From 5241544a682ab7ac43e2f6c8f26cf1158bbc72c1 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 11 Jul 2023 16:45:59 -1000 Subject: [PATCH 2/3] Expose some way to tell if we have memory only keys enabled --- lib/Onyx.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/Onyx.js b/lib/Onyx.js index a2f14c29..f30890f9 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -1216,12 +1216,22 @@ function update(data) { return clearPromise.then(() => Promise.all(_.map(promises, p => p()))); } +let memoryOnlyKeysEnabled = false; + /** * When set these keys will not be persisted to storage * @param {string[]} keyList */ function setMemoryOnlyKeys(keyList) { Storage.setMemoryOnlyKeys(keyList); + memoryOnlyKeysEnabled = true; +} + +/** + * @returns {boolean} + */ +function hasMemoryOnlyKeys() { + return memoryOnlyKeysEnabled; } /** @@ -1313,6 +1323,7 @@ const Onyx = { isSafeEvictionKey, METHOD, setMemoryOnlyKeys, + hasMemoryOnlyKeys, }; /** From 37863c5c1619f27de81ac096f01988d3a4ed9855 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 11 Jul 2023 17:18:34 -1000 Subject: [PATCH 3/3] No need this part --- lib/Onyx.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/lib/Onyx.js b/lib/Onyx.js index f30890f9..a2f14c29 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -1216,22 +1216,12 @@ function update(data) { return clearPromise.then(() => Promise.all(_.map(promises, p => p()))); } -let memoryOnlyKeysEnabled = false; - /** * When set these keys will not be persisted to storage * @param {string[]} keyList */ function setMemoryOnlyKeys(keyList) { Storage.setMemoryOnlyKeys(keyList); - memoryOnlyKeysEnabled = true; -} - -/** - * @returns {boolean} - */ -function hasMemoryOnlyKeys() { - return memoryOnlyKeysEnabled; } /** @@ -1323,7 +1313,6 @@ const Onyx = { isSafeEvictionKey, METHOD, setMemoryOnlyKeys, - hasMemoryOnlyKeys, }; /**