Skip to content

Commit 4696cdb

Browse files
authored
Merge pull request #750 from callstack-internal/JKobrynski/feat/remove-allow-dynamic-key-poc
Remove allowDynamicKey
2 parents 1c2beeb + 576b426 commit 4696cdb

3 files changed

Lines changed: 1 addition & 135 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
@@ -32,11 +32,6 @@ type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = {
3232
*/
3333
reuseConnection?: boolean;
3434

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

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

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

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)