Skip to content

Commit a4d5632

Browse files
committed
Remove initWithStoredValues from Onyx
1 parent a23f03c commit a4d5632

16 files changed

Lines changed: 54 additions & 352 deletions

API-INTERNAL.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,6 @@ When a collection of keys change, search for any callbacks matching the collecti
299299
When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks
300300

301301
**Kind**: global function
302-
**Example**
303-
```js
304-
keyChanged(key, value, subscriber => subscriber.initWithStoredValues === false)
305-
```
306302
<a name="sendDataToConnection"></a>
307303

308304
## sendDataToConnection()

lib/OnyxConnectionManager.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,21 @@ class OnyxConnectionManager {
115115
* according to their purpose and effect they produce in the Onyx connection.
116116
*/
117117
private generateConnectionID<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): string {
118-
const {key, initWithStoredValues, reuseConnection, waitForCollectionCallback} = connectOptions;
118+
const {key, reuseConnection, waitForCollectionCallback} = connectOptions;
119119

120120
// The current session ID is appended to the connection ID so we can have different connections
121121
// after an `Onyx.clear()` operation.
122122
let suffix = `,sessionID=${this.sessionID}`;
123123

124124
// We will generate a unique ID in any of the following situations:
125125
// - `reuseConnection` is `false`. That means the subscriber explicitly wants the connection to not be reused.
126-
// - `initWithStoredValues` is `false`. This flag changes the subscription flow when set to `false`, so the connection can't be reused.
127126
// - `key` is a collection key AND `waitForCollectionCallback` is `undefined/false`. This combination needs a new connection at every subscription
128127
// in order to send all the collection entries, so the connection can't be reused.
129-
if (
130-
reuseConnection === false ||
131-
initWithStoredValues === false ||
132-
(OnyxKeys.isCollectionKey(key) && (waitForCollectionCallback === undefined || waitForCollectionCallback === false))
133-
) {
128+
if (reuseConnection === false || (OnyxKeys.isCollectionKey(key) && (waitForCollectionCallback === undefined || waitForCollectionCallback === false))) {
134129
suffix += `,uniqueID=${Str.guid()}`;
135130
}
136131

137-
return `onyxKey=${key},initWithStoredValues=${initWithStoredValues ?? true},waitForCollectionCallback=${waitForCollectionCallback ?? false}${suffix}`;
132+
return `onyxKey=${key},waitForCollectionCallback=${waitForCollectionCallback ?? false}${suffix}`;
138133
}
139134

140135
/**

lib/OnyxSnapshotCache.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,13 @@ class OnyxSnapshotCache {
5858
* according to their purpose and effect they produce in the useOnyx hook behavior:
5959
*
6060
* - `selector`: Different selectors produce different results, so each selector needs its own cache entry
61-
* - `initWithStoredValues`: This flag changes the initial loading behavior and affects the returned fetch status
6261
*
6362
* Other options like `reuseConnection` don't affect the data transformation
6463
* or timing behavior of getSnapshot, so they're excluded from the cache key for better cache hit rates.
6564
*/
66-
registerConsumer<TKey extends OnyxKey, TReturnValue>(options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector' | 'initWithStoredValues'>): string {
65+
registerConsumer<TKey extends OnyxKey, TReturnValue>(options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector'>): string {
6766
const selectorID = options?.selector ? this.getSelectorID(options.selector) : 'no_selector';
68-
69-
// Create options hash without expensive JSON.stringify
70-
const initWithStoredValues = options?.initWithStoredValues ?? true;
71-
const cacheKey = `${selectorID}_${initWithStoredValues}`;
67+
const cacheKey = `${selectorID}`;
7268

7369
// Increment reference count for this cache key
7470
const currentCount = this.cacheKeyRefCounts.get(cacheKey) || 0;

lib/OnyxUtils.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,6 @@ function keysChanged<TKey extends CollectionKeyBase>(
613613

614614
/**
615615
* When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks
616-
*
617-
* @example
618-
* keyChanged(key, value, subscriber => subscriber.initWithStoredValues === false)
619616
*/
620617
function keyChanged<TKey extends OnyxKey>(
621618
key: TKey,
@@ -820,13 +817,11 @@ function retryOperation<TMethod extends RetriableOnyxOperation>(error: Error, on
820817
* Notifies subscribers and writes current value to cache
821818
*/
822819
function broadcastUpdate<TKey extends OnyxKey>(key: TKey, value: OnyxValue<TKey>, hasChanged?: boolean): void {
823-
// Update subscribers if the cached value has changed, or when the subscriber specifically requires
824-
// all updates regardless of value changes (indicated by initWithStoredValues set to false).
825820
if (hasChanged) {
826821
cache.set(key, value);
827822
}
828823

829-
keyChanged(key, value, (subscriber) => hasChanged || subscriber?.initWithStoredValues === false);
824+
keyChanged(key, value, () => !!hasChanged);
830825
}
831826

832827
function hasPendingMergeForKey(key: OnyxKey): boolean {
@@ -1070,10 +1065,6 @@ function subscribeToKey<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKe
10701065
// We create a mapping from key to lists of subscriptionIDs to access the specific list of subscriptionIDs.
10711066
storeKeyBySubscriptions(mapping.key, callbackToStateMapping[subscriptionID].subscriptionID);
10721067

1073-
if (mapping.initWithStoredValues === false) {
1074-
return subscriptionID;
1075-
}
1076-
10771068
// Commit connection only after init passes
10781069
deferredInitTask.promise
10791070
// This first .then() adds a microtask tick for compatibility reasons and

lib/types.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,6 @@ type Collection<TKey extends CollectionKeyBase, TValue> = Record<`${TKey}${strin
212212
/** Represents the base options used in `Onyx.connect()` method. */
213213
// NOTE: Any changes to this type like adding or removing options must be accounted in OnyxConnectionManager's `generateConnectionID()` method!
214214
type BaseConnectOptions = {
215-
/** If set to `false`, then the initial data will be only sent to the callback function if it changes. */
216-
initWithStoredValues?: boolean;
217-
218215
/**
219216
* If set to `false`, the connection won't be reused between other subscribers that are listening to the same Onyx key
220217
* with the same connect configurations.

lib/useOnyx.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ import useLiveRef from './useLiveRef';
1313
type UseOnyxSelector<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>> = (data: OnyxValue<TKey> | undefined) => TReturnValue;
1414

1515
type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = {
16-
/**
17-
* If set to `false`, then no data will be prefilled into the component.
18-
* @deprecated This param is going to be removed soon. Use RAM-only keys instead.
19-
*/
20-
initWithStoredValues?: boolean;
21-
2216
/**
2317
* If set to `false`, the connection won't be reused between other subscribers that are listening to the same Onyx key
2418
* with the same connect configurations.
@@ -97,11 +91,10 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
9791

9892
// Stores the previously result returned by the hook, containing the data from cache and the fetch status.
9993
// We initialize it to `undefined` and `loading` fetch status to simulate the initial result when the hook is loading from the cache.
100-
// However, if `initWithStoredValues` is `false` we set the fetch status to `loaded` since we want to signal that data is ready.
10194
const resultRef = useRef<UseOnyxResult<TReturnValue>>([
10295
undefined,
10396
{
104-
status: options?.initWithStoredValues === false ? 'loaded' : 'loading',
97+
status: 'loading',
10598
},
10699
]);
107100

@@ -133,9 +126,8 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
133126
() =>
134127
onyxSnapshotCache.registerConsumer({
135128
selector: options?.selector,
136-
initWithStoredValues: options?.initWithStoredValues,
137129
}),
138-
[options?.selector, options?.initWithStoredValues],
130+
[options?.selector],
139131
);
140132

141133
useEffect(() => () => onyxSnapshotCache.deregisterConsumer(key, cacheKey), [key, cacheKey]);
@@ -174,26 +166,16 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
174166

175167
const getSnapshot = useCallback(() => {
176168
// Check if we have any cache for this Onyx key
177-
// Don't use cache for first connection with initWithStoredValues: false
178-
// Also don't use cache during active data updates (when shouldGetCachedValueRef is true)
169+
// Don't use cache during active data updates (when shouldGetCachedValueRef is true)
179170
const isFirstConnection = connectedKeyRef.current !== key;
180-
if (!(isFirstConnection && options?.initWithStoredValues === false) && !shouldGetCachedValueRef.current) {
171+
if (!shouldGetCachedValueRef.current) {
181172
const cachedResult = onyxSnapshotCache.getCachedResult<UseOnyxResult<TReturnValue>>(key, cacheKey);
182173
if (cachedResult !== undefined) {
183174
resultRef.current = cachedResult;
184175
return cachedResult;
185176
}
186177
}
187178

188-
// We return the initial result right away during the first connection if `initWithStoredValues` is set to `false`.
189-
if (isFirstConnection && options?.initWithStoredValues === false) {
190-
const result = resultRef.current;
191-
192-
// Store result in snapshot cache
193-
onyxSnapshotCache.setCachedResult<UseOnyxResult<TReturnValue>>(key, cacheKey, result);
194-
return result;
195-
}
196-
197179
// We get the value from cache while the first connection to Onyx is being made or if the key has changed,
198180
// so we can return any cached value right away. For the case where the key has changed, If we don't return the cached value right away, then the UI will show the incorrect (previous) value for a brief period which looks like a UI glitch to the user. After the connection is made, we only
199181
// update `newValueRef` when `Onyx.connect()` callback is fired.
@@ -255,7 +237,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
255237
}
256238

257239
return resultRef.current;
258-
}, [options?.initWithStoredValues, key, memoizedSelector, cacheKey]);
240+
}, [key, memoizedSelector, cacheKey]);
259241

260242
const subscribe = useCallback(
261243
(onStoreChange: () => void) => {
@@ -266,7 +248,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
266248
previousValueRef.current = null;
267249
newValueRef.current = null;
268250
sourceValueRef.current = undefined;
269-
resultRef.current = [undefined, {status: options?.initWithStoredValues === false ? 'loaded' : 'loading'}];
251+
resultRef.current = [undefined, {status: 'loading'}];
270252
}
271253
// Force a cache re-read on every (re)subscription so any side effects from
272254
// subscribeToKey (e.g. addNullishStorageKey for skippable collection member ids)
@@ -299,7 +281,6 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
299281
// Finally, we signal that the store changed, making `getSnapshot()` be called again.
300282
onStoreChange();
301283
},
302-
initWithStoredValues: options?.initWithStoredValues,
303284
waitForCollectionCallback: OnyxKeys.isCollectionKey(key) as true,
304285
reuseConnection: options?.reuseConnection,
305286
});
@@ -315,7 +296,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
315296
onStoreChangeFnRef.current = null;
316297
};
317298
},
318-
[key, options?.initWithStoredValues, options?.reuseConnection],
299+
[key, options?.reuseConnection],
319300
);
320301

321302
const result = useSyncExternalStore<UseOnyxResult<TReturnValue>>(subscribe, getSnapshot);

tests/perf-test/OnyxSnapshotCache.perf-test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ const complexSelector: UseOnyxSelector<OnyxKey, ComplexSelectorResult> = (data)
3737

3838
const selectorOptions: UseOnyxOptions<string, number | undefined> = {
3939
selector: simpleSelector,
40-
initWithStoredValues: true,
4140
};
4241

4342
const complexSelectorOptions: UseOnyxOptions<string, ComplexSelectorResult> = {
4443
selector: complexSelector,
45-
initWithStoredValues: true,
4644
};
4745

4846
// Mock results

tests/perf-test/OnyxUtils.perf-test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ describe('OnyxUtils', () => {
310310
beforeEach: async () => {
311311
await Onyx.multiSet(mockedReportActionsMap);
312312
for (const key of mockedReportActionsKeys) {
313-
const id = OnyxUtils.subscribeToKey({key, callback: jest.fn(), initWithStoredValues: false});
313+
const id = OnyxUtils.subscribeToKey({key, callback: jest.fn()});
314314
subscriptionMap.set(key, id);
315315
}
316316
},
@@ -340,7 +340,7 @@ describe('OnyxUtils', () => {
340340
beforeEach: async () => {
341341
await Onyx.set(key, previousReportAction);
342342
for (let i = 0; i < 10000; i++) {
343-
const id = OnyxUtils.subscribeToKey({key, callback: jest.fn(), initWithStoredValues: false});
343+
const id = OnyxUtils.subscribeToKey({key, callback: jest.fn()});
344344
subscriptionIDs.add(id);
345345
}
346346
},
@@ -372,7 +372,7 @@ describe('OnyxUtils', () => {
372372
{
373373
beforeEach: async () => {
374374
await Onyx.multiSet(mockedReportActionsMap);
375-
subscriptionID = OnyxUtils.subscribeToKey({key: collectionKey, callback: jest.fn(), initWithStoredValues: false});
375+
subscriptionID = OnyxUtils.subscribeToKey({key: collectionKey, callback: jest.fn()});
376376
},
377377
afterEach: async () => {
378378
if (subscriptionID) {
@@ -402,7 +402,6 @@ describe('OnyxUtils', () => {
402402
subscriptionID = OnyxUtils.subscribeToKey({
403403
key: collectionKey,
404404
callback: jest.fn(),
405-
initWithStoredValues: false,
406405
});
407406

408407
OnyxUtils.getCollectionDataAndSendAsObject(mockedReportActionsKeys, {
@@ -650,7 +649,6 @@ describe('OnyxUtils', () => {
650649
beforeEach: async () => {
651650
subscriptionID = OnyxUtils.subscribeToKey({
652651
key,
653-
initWithStoredValues: false,
654652
});
655653
},
656654
afterEach: clearOnyxAfterEachMeasure,
@@ -692,7 +690,6 @@ describe('OnyxUtils', () => {
692690
beforeEach: async () => {
693691
subscriptionID = OnyxUtils.subscribeToKey({
694692
key,
695-
initWithStoredValues: false,
696693
});
697694
},
698695
afterEach: async () => {
@@ -713,7 +710,6 @@ describe('OnyxUtils', () => {
713710
beforeEach: async () => {
714711
subscriptionID = OnyxUtils.subscribeToKey({
715712
key,
716-
initWithStoredValues: false,
717713
});
718714
OnyxUtils.storeKeyBySubscriptions(key, subscriptionID);
719715
},

tests/perf-test/useOnyx.perf-test.tsx

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -196,31 +196,6 @@ describe('useOnyx', () => {
196196
});
197197
});
198198

199-
describe('initWithStoredValues', () => {
200-
/**
201-
* Expected renders: 1.
202-
*/
203-
test('connecting with initWithStoredValues set to false', async () => {
204-
const key = ONYXKEYS.TEST_KEY;
205-
await measureRenders(
206-
<UseOnyxWrapper
207-
onyxKey={key}
208-
onyxOptions={{initWithStoredValues: false}}
209-
/>,
210-
{
211-
beforeEach: async () => {
212-
await StorageMock.setItem(key, 'test');
213-
},
214-
scenario: async () => {
215-
await screen.findByText(dataMatcher(key, undefined));
216-
await screen.findByText(metadataStatusMatcher(key, 'loaded'));
217-
},
218-
afterEach: clearOnyxAfterEachMeasure,
219-
},
220-
);
221-
});
222-
});
223-
224199
describe('multiple calls', () => {
225200
/**
226201
* Expected renders: 2.

0 commit comments

Comments
 (0)