Skip to content

Commit 6e0a332

Browse files
Merge pull request #760 from callstack-internal/VickyStash/refactor/82871-remove-micro-macro-tasks
Reapply "Remove macro/micro tasks during subscriber update"
2 parents 42cfc01 + e1642fb commit 6e0a332

9 files changed

Lines changed: 281 additions & 255 deletions

File tree

API-INTERNAL.md

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,6 @@ run out of storage the least recently accessed key can be removed.</p>
7979
<dt><a href="#getCollectionDataAndSendAsObject">getCollectionDataAndSendAsObject()</a></dt>
8080
<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>
8181
</dd>
82-
<dt><a href="#prepareSubscriberUpdate">prepareSubscriberUpdate(callback)</a></dt>
83-
<dd><p>Delays promise resolution until the next macrotask to prevent race condition if the key subscription is in progress.</p>
84-
</dd>
85-
<dt><a href="#scheduleSubscriberUpdate">scheduleSubscriberUpdate()</a></dt>
86-
<dd><p>Schedules an update that will be appended to the macro task queue (so it doesn&#39;t update the subscribers immediately).</p>
87-
</dd>
88-
<dt><a href="#scheduleNotifyCollectionSubscribers">scheduleNotifyCollectionSubscribers()</a></dt>
89-
<dd><p>This method is similar to scheduleSubscriberUpdate but it is built for working specifically with collections
90-
so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
91-
subscriber callbacks receive the data in a different format than they normally expect and it breaks code.</p>
92-
</dd>
9382
<dt><a href="#remove">remove()</a></dt>
9483
<dd><p>Remove a key from Onyx and update the subscribers</p>
9584
</dd>
@@ -350,35 +339,6 @@ run out of storage the least recently accessed key can be removed.
350339
## getCollectionDataAndSendAsObject()
351340
Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber.
352341

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

lib/Onyx.ts

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

265-
return OnyxMerge.applyMerge(key, existingValue, validChanges).then(({mergedValue, updatePromise}) => {
265+
return OnyxMerge.applyMerge(key, existingValue, validChanges).then(({mergedValue}) => {
266266
OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MERGE, key, changes, mergedValue);
267-
return updatePromise;
268267
});
269268
} catch (error) {
270269
Logger.logAlert(`An error occurred while applying merge for key: ${key}, Error: ${error}`);
@@ -376,16 +375,6 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
376375
keysToBeClearedFromStorage.push(key);
377376
}
378377

379-
const updatePromises: Array<Promise<void>> = [];
380-
381-
// Notify the subscribers for each key/value group so they can receive the new values
382-
for (const [key, value] of Object.entries(keyValuesToResetIndividually)) {
383-
updatePromises.push(OnyxUtils.scheduleSubscriberUpdate(key, value));
384-
}
385-
for (const [key, value] of Object.entries(keyValuesToResetAsCollection)) {
386-
updatePromises.push(OnyxUtils.scheduleNotifyCollectionSubscribers(key, value.newValues, value.oldValues));
387-
}
388-
389378
// Exclude RAM-only keys to prevent them from being saved to storage
390379
const defaultKeyValuePairs = Object.entries(
391380
Object.keys(defaultKeyStates)
@@ -404,7 +393,14 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
404393
.then(() => Storage.multiSet(defaultKeyValuePairs))
405394
.then(() => {
406395
DevTools.clearState(keysToPreserve);
407-
return Promise.all(updatePromises);
396+
397+
// Notify the subscribers for each key/value group so they can receive the new values
398+
for (const [key, value] of Object.entries(keyValuesToResetIndividually)) {
399+
OnyxUtils.keyChanged(key, value);
400+
}
401+
for (const [key, value] of Object.entries(keyValuesToResetAsCollection)) {
402+
OnyxUtils.keysChanged(key, value.newValues, value.oldValues);
403+
}
408404
});
409405
})
410406
.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>(

0 commit comments

Comments
 (0)