Skip to content

Commit

Permalink
Performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioh8010 committed Dec 4, 2024
1 parent 28bd489 commit 0912da3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 49 deletions.
104 changes: 58 additions & 46 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function init({
}: InitOptions): void {
Storage.init();

OnyxUtils.setSkippableCollectionMemberIDs(skippableCollectionMemberIDs);
OnyxUtils.setSkippableCollectionMemberIDs(new Set(skippableCollectionMemberIDs));

if (shouldSyncMultipleInstances) {
Storage.keepInstancesSync?.((key, value) => {
Expand Down Expand Up @@ -123,13 +123,16 @@ function disconnect(connection: Connection): void {
* @param value value to store
*/
function set<TKey extends OnyxKey>(key: TKey, value: OnyxSetInput<TKey>): Promise<void> {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key);
if (OnyxUtils.getSkippableCollectionMemberIDs().includes(collectionMemberID)) {
return Promise.resolve();
const skippableCollectionMemberIDs = OnyxUtils.getSkippableCollectionMemberIDs();
if (skippableCollectionMemberIDs.size) {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key);
if (skippableCollectionMemberIDs.has(collectionMemberID)) {
return Promise.resolve();
}
} catch (e) {
// Key is not a collection one or something went wrong during split, so we proceed with the function's logic.
}
} catch (e) {
// Key is not a collection one or something went wrong during split, so we proceed with the function's logic.
}

// When we use Onyx.set to set a key we want to clear the current delta changes from Onyx.merge that were queued
Expand Down Expand Up @@ -203,21 +206,23 @@ function multiSet(data: OnyxMultiSetInput): Promise<void> {
let newData = data;

const skippableCollectionMemberIDs = OnyxUtils.getSkippableCollectionMemberIDs();
newData = Object.keys(newData).reduce((result: OnyxMultiSetInput, key) => {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key);
if (!skippableCollectionMemberIDs.includes(collectionMemberID)) {
if (skippableCollectionMemberIDs.size) {
newData = Object.keys(newData).reduce((result: OnyxMultiSetInput, key) => {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key);
if (!skippableCollectionMemberIDs.has(collectionMemberID)) {
// eslint-disable-next-line no-param-reassign
result[key] = newData[key];
}
} catch {
// Key is not a collection one or something went wrong during split, so we assign the data to result anyway.
// eslint-disable-next-line no-param-reassign
result[key] = newData[key];
}
} catch {
// Key is not a collection one or something went wrong during split, so we assign the data to result anyway.
// eslint-disable-next-line no-param-reassign
result[key] = newData[key];
}

return result;
}, {});
return result;
}, {});
}

const keyValuePairsToSet = OnyxUtils.prepareKeyValuePairsForStorage(newData, true);

Expand Down Expand Up @@ -255,13 +260,16 @@ function multiSet(data: OnyxMultiSetInput): Promise<void> {
* Onyx.merge(ONYXKEYS.POLICY, {name: 'My Workspace'}); // -> {id: 1, name: 'My Workspace'}
*/
function merge<TKey extends OnyxKey>(key: TKey, changes: OnyxMergeInput<TKey>): Promise<void> {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key);
if (OnyxUtils.getSkippableCollectionMemberIDs().includes(collectionMemberID)) {
return Promise.resolve();
const skippableCollectionMemberIDs = OnyxUtils.getSkippableCollectionMemberIDs();
if (skippableCollectionMemberIDs.size) {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key);
if (skippableCollectionMemberIDs.has(collectionMemberID)) {
return Promise.resolve();
}
} catch (e) {
// Key is not a collection one or something went wrong during split, so we proceed with the function's logic.
}
} catch (e) {
// Key is not a collection one or something went wrong during split, so we proceed with the function's logic.
}

const mergeQueue = OnyxUtils.getMergeQueue();
Expand Down Expand Up @@ -387,21 +395,23 @@ function mergeCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TK
}

const skippableCollectionMemberIDs = OnyxUtils.getSkippableCollectionMemberIDs();
resultCollection = Object.keys(resultCollection).reduce((result: OnyxInputKeyValueMapping, key) => {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key, collectionKey);
if (!skippableCollectionMemberIDs.includes(collectionMemberID)) {
if (skippableCollectionMemberIDs.size) {
resultCollection = Object.keys(resultCollection).reduce((result: OnyxInputKeyValueMapping, key) => {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key, collectionKey);
if (!skippableCollectionMemberIDs.has(collectionMemberID)) {
// eslint-disable-next-line no-param-reassign
result[key] = resultCollection[key];
}
} catch {
// Something went wrong during split, so we assign the data to result anyway.
// eslint-disable-next-line no-param-reassign
result[key] = resultCollection[key];
}
} catch {
// Something went wrong during split, so we assign the data to result anyway.
// eslint-disable-next-line no-param-reassign
result[key] = resultCollection[key];
}

return result;
}, {});
return result;
}, {});
}

return OnyxUtils.getAllKeys()
.then((persistedKeys) => {
Expand Down Expand Up @@ -803,21 +813,23 @@ function setCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey
}

const skippableCollectionMemberIDs = OnyxUtils.getSkippableCollectionMemberIDs();
resultCollection = Object.keys(resultCollection).reduce((result: OnyxInputKeyValueMapping, key) => {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key, collectionKey);
if (!skippableCollectionMemberIDs.includes(collectionMemberID)) {
if (skippableCollectionMemberIDs.size) {
resultCollection = Object.keys(resultCollection).reduce((result: OnyxInputKeyValueMapping, key) => {
try {
const [, collectionMemberID] = OnyxUtils.splitCollectionMemberKey(key, collectionKey);
if (!skippableCollectionMemberIDs.has(collectionMemberID)) {
// eslint-disable-next-line no-param-reassign
result[key] = resultCollection[key];
}
} catch {
// Something went wrong during split, so we assign the data to result anyway.
// eslint-disable-next-line no-param-reassign
result[key] = resultCollection[key];
}
} catch {
// Something went wrong during split, so we assign the data to result anyway.
// eslint-disable-next-line no-param-reassign
result[key] = resultCollection[key];
}

return result;
}, {});
return result;
}, {});
}

return OnyxUtils.getAllKeys().then((persistedKeys) => {
const mutableCollection: OnyxInputKeyValueMapping = {...resultCollection};
Expand Down
6 changes: 3 additions & 3 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ let lastSubscriptionID = 0;
// Connections can be made before `Onyx.init`. They would wait for this task before resolving
const deferredInitTask = createDeferredTask();

let skippableCollectionMemberIDs: string[] = [];
let skippableCollectionMemberIDs = new Set<string>();

function getSnapshotKey(): OnyxKey | null {
return snapshotKey;
Expand Down Expand Up @@ -130,14 +130,14 @@ function getEvictionBlocklist(): Record<OnyxKey, string[] | undefined> {
/**
* Getter - TODO
*/
function getSkippableCollectionMemberIDs(): string[] {
function getSkippableCollectionMemberIDs(): Set<string> {
return skippableCollectionMemberIDs;
}

/**
* Setter - TODO
*/
function setSkippableCollectionMemberIDs(ids: string[]): void {
function setSkippableCollectionMemberIDs(ids: Set<string>): void {
skippableCollectionMemberIDs = ids;
}

Expand Down

0 comments on commit 0912da3

Please sign in to comment.