Skip to content

Commit 577832e

Browse files
committed
`Merge branch 'main' into feature/remove-cache-eviction-system
2 parents 9133123 + d2000b5 commit 577832e

18 files changed

Lines changed: 962 additions & 516 deletions

API-INTERNAL.md

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,6 @@ If the requested key is a collection, it will return an object with all the coll
7575
<dt><a href="#getCollectionDataAndSendAsObject">getCollectionDataAndSendAsObject()</a></dt>
7676
<dd><p>Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber.</p>
7777
</dd>
78-
<dt><a href="#prepareSubscriberUpdate">prepareSubscriberUpdate(callback)</a></dt>
79-
<dd><p>Delays promise resolution until the next macrotask to prevent race condition if the key subscription is in progress.</p>
80-
</dd>
81-
<dt><a href="#scheduleSubscriberUpdate">scheduleSubscriberUpdate()</a></dt>
82-
<dd><p>Schedules an update that will be appended to the macro task queue (so it doesn&#39;t update the subscribers immediately).</p>
83-
</dd>
84-
<dt><a href="#scheduleNotifyCollectionSubscribers">scheduleNotifyCollectionSubscribers()</a></dt>
85-
<dd><p>This method is similar to scheduleSubscriberUpdate but it is built for working specifically with collections
86-
so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
87-
subscriber callbacks receive the data in a different format than they normally expect and it breaks code.</p>
88-
</dd>
8978
<dt><a href="#remove">remove()</a></dt>
9079
<dd><p>Remove a key from Onyx and update the subscribers</p>
9180
</dd>
@@ -339,35 +328,6 @@ Sends the data obtained from the keys to the connection.
339328
## getCollectionDataAndSendAsObject()
340329
Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber.
341330

342-
**Kind**: global function
343-
<a name="prepareSubscriberUpdate"></a>
344-
345-
## prepareSubscriberUpdate(callback)
346-
Delays promise resolution until the next macrotask to prevent race condition if the key subscription is in progress.
347-
348-
**Kind**: global function
349-
350-
| Param | Description |
351-
| --- | --- |
352-
| callback | The keyChanged/keysChanged callback |
353-
354-
<a name="scheduleSubscriberUpdate"></a>
355-
356-
## scheduleSubscriberUpdate()
357-
Schedules an update that will be appended to the macro task queue (so it doesn't update the subscribers immediately).
358-
359-
**Kind**: global function
360-
**Example**
361-
```js
362-
scheduleSubscriberUpdate(key, value, subscriber => subscriber.initWithStoredValues === false)
363-
```
364-
<a name="scheduleNotifyCollectionSubscribers"></a>
365-
366-
## scheduleNotifyCollectionSubscribers()
367-
This method is similar to scheduleSubscriberUpdate but it is built for working specifically with collections
368-
so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
369-
subscriber callbacks receive the data in a different format than they normally expect and it breaks code.
370-
371331
**Kind**: global function
372332
<a name="remove"></a>
373333

lib/Onyx.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,8 @@ function merge<TKey extends OnyxKey>(key: TKey, changes: OnyxMergeInput<TKey>):
257257
return Promise.resolve();
258258
}
259259

260-
return OnyxMerge.applyMerge(key, existingValue, validChanges).then(({mergedValue, updatePromise}) => {
260+
return OnyxMerge.applyMerge(key, existingValue, validChanges).then(({mergedValue}) => {
261261
OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MERGE, key, changes, mergedValue);
262-
return updatePromise;
263262
});
264263
} catch (error) {
265264
Logger.logAlert(`An error occurred while applying merge for key: ${key}, Error: ${error}`);
@@ -371,16 +370,6 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
371370
keysToBeClearedFromStorage.push(key);
372371
}
373372

374-
const updatePromises: Array<Promise<void>> = [];
375-
376-
// Notify the subscribers for each key/value group so they can receive the new values
377-
for (const [key, value] of Object.entries(keyValuesToResetIndividually)) {
378-
updatePromises.push(OnyxUtils.scheduleSubscriberUpdate(key, value));
379-
}
380-
for (const [key, value] of Object.entries(keyValuesToResetAsCollection)) {
381-
updatePromises.push(OnyxUtils.scheduleNotifyCollectionSubscribers(key, value.newValues, value.oldValues));
382-
}
383-
384373
// Exclude RAM-only keys to prevent them from being saved to storage
385374
const defaultKeyValuePairs = Object.entries(
386375
Object.keys(defaultKeyStates)
@@ -399,7 +388,14 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
399388
.then(() => Storage.multiSet(defaultKeyValuePairs))
400389
.then(() => {
401390
DevTools.clearState(keysToPreserve);
402-
return Promise.all(updatePromises);
391+
392+
// Notify the subscribers for each key/value group so they can receive the new values
393+
for (const [key, value] of Object.entries(keyValuesToResetIndividually)) {
394+
OnyxUtils.keyChanged(key, value);
395+
}
396+
for (const [key, value] of Object.entries(keyValuesToResetAsCollection)) {
397+
OnyxUtils.keysChanged(key, value.newValues, value.oldValues);
398+
}
403399
});
404400
})
405401
.then(() => undefined);

lib/OnyxMerge/index.native.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@ const applyMerge: ApplyMerge = <TKey extends OnyxKey, TValue extends OnyxInput<T
2727
OnyxUtils.logKeyChanged(OnyxUtils.METHOD.MERGE, key, mergedValue, hasChanged);
2828

2929
// This approach prioritizes fast UI changes without waiting for data to be stored in device storage.
30-
const updatePromise = OnyxUtils.broadcastUpdate(key, mergedValue as OnyxValue<TKey>, hasChanged);
30+
OnyxUtils.broadcastUpdate(key, mergedValue as OnyxValue<TKey>, hasChanged);
3131

3232
const shouldSkipStorageOperations = !hasChanged || OnyxKeys.isRamOnlyKey(key);
3333

3434
// If the value has not changed, calling Storage.setItem() would be redundant and a waste of performance, so return early instead.
3535
// If the key is marked as RAM-only, it should not be saved nor updated in the storage.
3636
if (shouldSkipStorageOperations) {
37-
return Promise.resolve({mergedValue, updatePromise});
37+
return Promise.resolve({mergedValue});
3838
}
3939

4040
// For native platforms we use `mergeItem` that will take advantage of JSON_PATCH and JSON_REPLACE SQL operations to
4141
// merge the object in a performant way.
4242
return Storage.mergeItem(key, batchedChanges as OnyxValue<TKey>, replaceNullPatches).then(() => ({
4343
mergedValue,
44-
updatePromise,
4544
}));
4645
};
4746

lib/OnyxMerge/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,19 @@ const applyMerge: ApplyMerge = <TKey extends OnyxKey, TValue extends OnyxInput<T
1919
OnyxUtils.logKeyChanged(OnyxUtils.METHOD.MERGE, key, mergedValue, hasChanged);
2020

2121
// This approach prioritizes fast UI changes without waiting for data to be stored in device storage.
22-
const updatePromise = OnyxUtils.broadcastUpdate(key, mergedValue as OnyxValue<TKey>, hasChanged);
22+
OnyxUtils.broadcastUpdate(key, mergedValue as OnyxValue<TKey>, hasChanged);
2323

2424
const shouldSkipStorageOperations = !hasChanged || OnyxKeys.isRamOnlyKey(key);
2525

2626
// If the value has not changed, calling Storage.setItem() would be redundant and a waste of performance, so return early instead.
2727
// If the key is marked as RAM-only, it should not be saved nor updated in the storage.
2828
if (shouldSkipStorageOperations) {
29-
return Promise.resolve({mergedValue, updatePromise});
29+
return Promise.resolve({mergedValue});
3030
}
3131

3232
// For web platforms we use `setItem` since the object was already merged with its changes before.
3333
return Storage.setItem(key, mergedValue as OnyxValue<TKey>).then(() => ({
3434
mergedValue,
35-
updatePromise,
3635
}));
3736
};
3837

lib/OnyxMerge/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type {OnyxInput, OnyxKey} from '../types';
22

33
type ApplyMergeResult<TValue> = {
44
mergedValue: TValue;
5-
updatePromise: Promise<void>;
65
};
76

87
type ApplyMerge = <TKey extends OnyxKey, TValue extends OnyxInput<OnyxKey> | undefined, TChange extends OnyxInput<OnyxKey> | null>(

lib/OnyxSnapshotCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class OnyxSnapshotCache {
3333

3434
constructor() {
3535
this.snapshotCache = new Map();
36-
this.selectorIDMap = new Map();
36+
this.selectorIDMap = new WeakMap();
3737
this.selectorIDCounter = 0;
3838
this.cacheKeyRefCounts = new Map();
3939
}

0 commit comments

Comments
 (0)