Skip to content

Commit 56f1691

Browse files
committed
feat: remove allowStaleData flag
1 parent 313200b commit 56f1691

5 files changed

Lines changed: 6 additions & 70 deletions

File tree

lib/OnyxSnapshotCache.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,16 @@ class OnyxSnapshotCache {
5959
*
6060
* - `selector`: Different selectors produce different results, so each selector needs its own cache entry
6161
* - `initWithStoredValues`: This flag changes the initial loading behavior and affects the returned fetch status
62-
* - `allowStaleData`: Controls whether stale data can be returned during pending merges, affecting result timing
6362
*
6463
* Other options like `canEvict`, `reuseConnection`, and `allowDynamicKey` don't affect the data transformation
6564
* or timing behavior of getSnapshot, so they're excluded from the cache key for better cache hit rates.
6665
*/
67-
registerConsumer<TKey extends OnyxKey, TReturnValue>(options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector' | 'initWithStoredValues' | 'allowStaleData'>): string {
66+
registerConsumer<TKey extends OnyxKey, TReturnValue>(options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector' | 'initWithStoredValues'>): string {
6867
const selectorID = options?.selector ? this.getSelectorID(options.selector) : 'no_selector';
6968

7069
// Create options hash without expensive JSON.stringify
7170
const initWithStoredValues = options?.initWithStoredValues ?? true;
72-
const allowStaleData = options?.allowStaleData ?? false;
73-
const cacheKey = `${selectorID}_${initWithStoredValues}_${allowStaleData}`;
71+
const cacheKey = `${selectorID}_${initWithStoredValues}`;
7472

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

lib/useOnyx.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = {
2626
*/
2727
initWithStoredValues?: boolean;
2828

29-
/**
30-
* If set to `true`, data will be retrieved from cache during the first render even if there is a pending merge for the key.
31-
*/
32-
allowStaleData?: boolean;
33-
3429
/**
3530
* If set to `false`, the connection won't be reused between other subscribers that are listening to the same Onyx key
3631
* with the same connect configurations.
@@ -146,9 +141,8 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
146141
onyxSnapshotCache.registerConsumer({
147142
selector: options?.selector,
148143
initWithStoredValues: options?.initWithStoredValues,
149-
allowStaleData: options?.allowStaleData,
150144
}),
151-
[options?.selector, options?.initWithStoredValues, options?.allowStaleData],
145+
[options?.selector, options?.initWithStoredValues],
152146
);
153147

154148
useEffect(() => () => onyxSnapshotCache.deregisterConsumer(key, cacheKey), [key, cacheKey]);
@@ -262,8 +256,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
262256

263257
// If we have pending merge operations for the key during the first connection, we set the new value to `undefined`
264258
// and fetch status to `loading` to simulate that it is still being loaded until we have the most updated data.
265-
// If `allowStaleData` is `true` this logic will be ignored and cached value will be used, even if it's stale data.
266-
if (isFirstConnectionRef.current && OnyxUtils.hasPendingMergeForKey(key) && !options?.allowStaleData) {
259+
if (isFirstConnectionRef.current && OnyxUtils.hasPendingMergeForKey(key)) {
267260
newValueRef.current = undefined;
268261
newFetchStatus = 'loading';
269262
}
@@ -308,7 +301,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
308301
}
309302

310303
return resultRef.current;
311-
}, [options?.initWithStoredValues, options?.allowStaleData, key, memoizedSelector, cacheKey, previousKey]);
304+
}, [options?.initWithStoredValues, key, memoizedSelector, cacheKey, previousKey]);
312305

313306
const subscribe = useCallback(
314307
(onStoreChange: () => void) => {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ const complexSelector: UseOnyxSelector<OnyxKey, ComplexSelectorResult> = (data)
3838
const selectorOptions: UseOnyxOptions<string, number | undefined> = {
3939
selector: simpleSelector,
4040
initWithStoredValues: true,
41-
allowStaleData: false,
4241
};
4342

4443
const complexSelectorOptions: UseOnyxOptions<string, ComplexSelectorResult> = {
4544
selector: complexSelector,
4645
initWithStoredValues: true,
47-
allowStaleData: false,
4846
};
4947

5048
// Mock results

tests/unit/OnyxSnapshotCacheTest.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ describe('OnyxSnapshotCache', () => {
3939
const optionsWithSelector: UseOnyxOptions<OnyxKey, string> = {
4040
selector,
4141
initWithStoredValues: true,
42-
allowStaleData: false,
4342
};
4443
const optionsWithoutSelector: UseOnyxOptions<OnyxKey, string> = {
4544
initWithStoredValues: false,
46-
allowStaleData: true,
4745
};
4846
const keyWithSelector = cache.registerConsumer(optionsWithSelector);
4947
const keyWithoutSelector = cache.registerConsumer(optionsWithoutSelector);

tests/unit/useOnyxTest.ts

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ describe('useOnyx', () => {
717717
});
718718
});
719719

720-
describe('allowStaleData', () => {
720+
describe('pending merges', () => {
721721
it('should return undefined and loading state while we have pending merges for the key, and then return updated value and loaded state', async () => {
722722
Onyx.set(ONYXKEYS.TEST_KEY, 'test1');
723723

@@ -757,24 +757,6 @@ describe('useOnyx', () => {
757757
expect(result.current[0]).toEqual('test4_changed');
758758
expect(result.current[1].status).toEqual('loaded');
759759
});
760-
761-
it('should return stale value and loaded state if allowStaleData is true, and then return updated value and loaded state', async () => {
762-
Onyx.set(ONYXKEYS.TEST_KEY, 'test1');
763-
764-
Onyx.merge(ONYXKEYS.TEST_KEY, 'test2');
765-
Onyx.merge(ONYXKEYS.TEST_KEY, 'test3');
766-
Onyx.merge(ONYXKEYS.TEST_KEY, 'test4');
767-
768-
const {result} = renderHook(() => useOnyx(ONYXKEYS.TEST_KEY, {allowStaleData: true}));
769-
770-
expect(result.current[0]).toEqual('test1');
771-
expect(result.current[1].status).toEqual('loaded');
772-
773-
await act(async () => waitForPromisesToResolve());
774-
775-
expect(result.current[0]).toEqual('test4');
776-
expect(result.current[1].status).toEqual('loaded');
777-
});
778760
});
779761

780762
describe('initWithStoredValues', () => {
@@ -857,39 +839,6 @@ describe('useOnyx', () => {
857839
expect(result2.current[1].status).toEqual('loaded');
858840
});
859841

860-
it('"allowStaleData" should work correctly for the same key if more than one hook is using it', async () => {
861-
Onyx.set(ONYXKEYS.TEST_KEY, 'test1');
862-
863-
Onyx.merge(ONYXKEYS.TEST_KEY, 'test2');
864-
Onyx.merge(ONYXKEYS.TEST_KEY, 'test3');
865-
Onyx.merge(ONYXKEYS.TEST_KEY, 'test4');
866-
867-
const {result: result1} = renderHook(() => useOnyx(ONYXKEYS.TEST_KEY, {allowStaleData: true}));
868-
869-
expect(result1.current[0]).toEqual('test1');
870-
expect(result1.current[1].status).toEqual('loaded');
871-
872-
await act(async () => waitForPromisesToResolve());
873-
874-
expect(result1.current[0]).toEqual('test4');
875-
expect(result1.current[1].status).toEqual('loaded');
876-
877-
// Second hook
878-
Onyx.merge(ONYXKEYS.TEST_KEY, 'test5');
879-
Onyx.merge(ONYXKEYS.TEST_KEY, 'test6');
880-
Onyx.merge(ONYXKEYS.TEST_KEY, 'test7');
881-
882-
const {result: result2} = renderHook(() => useOnyx(ONYXKEYS.TEST_KEY, {allowStaleData: true}));
883-
884-
expect(result2.current[0]).toEqual('test4');
885-
expect(result2.current[1].status).toEqual('loaded');
886-
887-
await act(async () => waitForPromisesToResolve());
888-
889-
expect(result2.current[0]).toEqual('test7');
890-
expect(result2.current[1].status).toEqual('loaded');
891-
});
892-
893842
it('"initWithStoredValues" should work correctly for the same key if more than one hook is using it', async () => {
894843
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test1');
895844

0 commit comments

Comments
 (0)