Skip to content

Commit ea305ab

Browse files
committed
Merge branch 'main' into feature/structural-sharing-cache
2 parents b622701 + 434871e commit ea305ab

5 files changed

Lines changed: 4 additions & 138 deletions

File tree

lib/OnyxSnapshotCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class OnyxSnapshotCache {
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
6262
*
63-
* Other options like `canEvict`, `reuseConnection`, and `allowDynamicKey` don't affect the data transformation
63+
* Other options like `canEvict` and `reuseConnection` don't affect the data transformation
6464
* or timing behavior of getSnapshot, so they're excluded from the cache key for better cache hit rates.
6565
*/
6666
registerConsumer<TKey extends OnyxKey, TReturnValue>(options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector' | 'initWithStoredValues'>): string {

lib/useOnyx.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = {
3333
*/
3434
reuseConnection?: boolean;
3535

36-
/**
37-
* If set to `true`, the key can be changed dynamically during the component lifecycle.
38-
*/
39-
allowDynamicKey?: boolean;
40-
4136
/**
4237
* This will be used to subscribe to a subset of an Onyx key's data.
4338
* Using this setting on `useOnyx` can have very positive performance benefits because the component will only re-render
@@ -148,30 +143,6 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
148143

149144
useEffect(() => () => onyxSnapshotCache.deregisterConsumer(key, cacheKey), [key, cacheKey]);
150145

151-
useEffect(() => {
152-
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
153-
if (options?.allowDynamicKey || previousKey === key) {
154-
return;
155-
}
156-
157-
try {
158-
const previousCollectionKey = OnyxKeys.splitCollectionMemberKey(previousKey)[0];
159-
const collectionKey = OnyxKeys.splitCollectionMemberKey(key)[0];
160-
161-
if (OnyxKeys.isCollectionMemberKey(previousCollectionKey, previousKey) && OnyxKeys.isCollectionMemberKey(collectionKey, key) && previousCollectionKey === collectionKey) {
162-
return;
163-
}
164-
} catch (e) {
165-
throw new Error(
166-
`'${previousKey}' key can't be changed to '${key}'. useOnyx() only supports dynamic keys if they are both collection member keys from the same collection e.g. from 'collection_id1' to 'collection_id2'.`,
167-
);
168-
}
169-
170-
throw new Error(
171-
`'${previousKey}' key can't be changed to '${key}'. useOnyx() only supports dynamic keys if they are both collection member keys from the same collection e.g. from 'collection_id1' to 'collection_id2'.`,
172-
);
173-
}, [previousKey, key, options?.allowDynamicKey]);
174-
175146
// Track previous dependencies to prevent infinite loops
176147
const previousDependenciesRef = useRef<DependencyList>([]);
177148

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-onyx",
3-
"version": "3.0.47",
3+
"version": "3.0.48",
44
"author": "Expensify, Inc.",
55
"homepage": "https://expensify.com",
66
"description": "State management for React Native",

tests/unit/useOnyxTest.ts

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -35,111 +35,6 @@ beforeEach(async () => {
3535
});
3636

3737
describe('useOnyx', () => {
38-
describe('dynamic key', () => {
39-
const error = (key1: string, key2: string) =>
40-
`'${key1}' key can't be changed to '${key2}'. useOnyx() only supports dynamic keys if they are both collection member keys from the same collection e.g. from 'collection_id1' to 'collection_id2'.`;
41-
42-
beforeEach(() => {
43-
jest.spyOn(console, 'error').mockImplementation(jest.fn);
44-
});
45-
46-
afterEach(() => {
47-
(console.error as unknown as jest.SpyInstance<void, Parameters<typeof console.error>>).mockRestore();
48-
});
49-
50-
it('should throw an error when changing from a non-collection key to another one', async () => {
51-
const {rerender} = renderHook((key: string) => useOnyx(key), {initialProps: ONYXKEYS.TEST_KEY});
52-
53-
try {
54-
await act(async () => {
55-
rerender(ONYXKEYS.TEST_KEY_2);
56-
});
57-
58-
fail('Expected to throw an error.');
59-
} catch (e) {
60-
expect((e as Error).message).toBe(error(ONYXKEYS.TEST_KEY, ONYXKEYS.TEST_KEY_2));
61-
}
62-
});
63-
64-
it('should throw an error when changing from a collection key to another one', async () => {
65-
const {rerender} = renderHook((key: string) => useOnyx(key), {initialProps: ONYXKEYS.COLLECTION.TEST_KEY});
66-
67-
try {
68-
await act(async () => {
69-
rerender(ONYXKEYS.COLLECTION.TEST_KEY_2);
70-
});
71-
72-
fail('Expected to throw an error.');
73-
} catch (e) {
74-
expect((e as Error).message).toBe(error(ONYXKEYS.COLLECTION.TEST_KEY, ONYXKEYS.COLLECTION.TEST_KEY_2));
75-
}
76-
});
77-
78-
it('should throw an error when changing from a collection key to a collectiom member key', async () => {
79-
const {rerender} = renderHook((key: string) => useOnyx(key), {initialProps: ONYXKEYS.COLLECTION.TEST_KEY});
80-
81-
try {
82-
await act(async () => {
83-
rerender(`${ONYXKEYS.COLLECTION.TEST_KEY}1`);
84-
});
85-
86-
fail('Expected to throw an error.');
87-
} catch (e) {
88-
expect((e as Error).message).toBe(error(ONYXKEYS.COLLECTION.TEST_KEY, `${ONYXKEYS.COLLECTION.TEST_KEY}1`));
89-
}
90-
});
91-
92-
it('should not throw any errors when changing from a collection member key to another one', async () => {
93-
const {rerender} = renderHook((key: string) => useOnyx(key), {initialProps: `${ONYXKEYS.COLLECTION.TEST_KEY}1` as string});
94-
95-
try {
96-
await act(async () => {
97-
rerender(`${ONYXKEYS.COLLECTION.TEST_KEY}2`);
98-
});
99-
} catch (e) {
100-
fail("Expected to don't throw any errors.");
101-
}
102-
});
103-
104-
it('should not throw an error when changing from a non-collection key to another one if allowDynamicKey is true', async () => {
105-
const {rerender} = renderHook((key: string) => useOnyx(key, {allowDynamicKey: true}), {initialProps: ONYXKEYS.TEST_KEY});
106-
107-
try {
108-
await act(async () => {
109-
rerender(ONYXKEYS.TEST_KEY_2);
110-
});
111-
} catch (e) {
112-
fail("Expected to don't throw any errors.");
113-
}
114-
});
115-
116-
it('should throw an error when changing from a non-collection key to another one if allowDynamicKey is false', async () => {
117-
const {rerender} = renderHook((key: string) => useOnyx(key, {allowDynamicKey: false}), {initialProps: ONYXKEYS.TEST_KEY});
118-
119-
try {
120-
await act(async () => {
121-
rerender(ONYXKEYS.TEST_KEY_2);
122-
});
123-
124-
fail('Expected to throw an error.');
125-
} catch (e) {
126-
expect((e as Error).message).toBe(error(ONYXKEYS.TEST_KEY, ONYXKEYS.TEST_KEY_2));
127-
}
128-
});
129-
130-
it('should not throw an error when changing from a collection member key to another one if allowDynamicKey is true', async () => {
131-
const {rerender} = renderHook((key: string) => useOnyx(key, {allowDynamicKey: true}), {initialProps: `${ONYXKEYS.COLLECTION.TEST_KEY}` as string});
132-
133-
try {
134-
await act(async () => {
135-
rerender(`${ONYXKEYS.COLLECTION.TEST_KEY_2}`);
136-
});
137-
} catch (e) {
138-
fail("Expected to don't throw any errors.");
139-
}
140-
});
141-
});
142-
14338
describe('misc', () => {
14439
it('should initially return loading state while loading non-existent key, and then return `undefined` and loaded state', async () => {
14540
const {result} = renderHook(() => useOnyx(ONYXKEYS.TEST_KEY));

0 commit comments

Comments
 (0)