Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memoryOnlyKeys setter option to Onyx #270

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -1304,6 +1312,7 @@ const Onyx = {
removeFromEvictionBlockList,
isSafeEvictionKey,
METHOD,
setMemoryOnlyKeys,
};

/**
Expand Down
17 changes: 17 additions & 0 deletions lib/storage/providers/LocalForage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -16,13 +17,22 @@ 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.
* So, we are slowing this process down by waiting until one write is complete before moving on
* 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) => {
Expand Down Expand Up @@ -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;
5 changes: 5 additions & 0 deletions lib/storage/providers/SQLiteStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ const provider = {
* @returns {Promise<void>}
*/
clear: () => db.executeAsync('DELETE FROM keyvaluepairs;', []),

/**
* Noop on mobile for now.
*/
setMemoryOnlyKeys: () => {},
};

export default provider;