Skip to content

Commit ae4159b

Browse files
committed
start implementing supporting multiple caches
1 parent 2ed7386 commit ae4159b

16 files changed

Lines changed: 291 additions & 729 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ export const {
5454
},
5555
utils: {applyEntityChanges},
5656
} = createCache({
57-
// This selector should return the cache state based on the path to its reducer.
58-
cacheStateSelector: (state) => state.cache,
59-
// Typenames provide a mapping of all typenames to their entity types, which is needed for normalization.
57+
// Used as prefix for actions and in default cacheStateSelector
58+
name: 'cache',
59+
// Typenames provide a mapping of all typenames to their entity types, which is needed for proper typing and normalization.
6060
// Empty objects with type casting can be used as values.
6161
typenames: {
6262
users: {} as User, // here `users` entities will have type `User`

‎example/src/redux/cache.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const {
1515
selectors: {entitiesSelector, entitiesByTypenameSelector},
1616
hooks: {useClient, useMutation, useQuery, useSelectEntityById},
1717
} = createCache({
18-
cacheStateSelector: (state) => state,
18+
name: 'cache',
1919
typenames: {
2020
users: {} as User,
2121
banks: {} as Bank,

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"redux": "4.2.1",
3939
"redux-logger": "3.0.6",
4040
"ts-jest": "29.1.0",
41-
"typescript": "5.0.4"
41+
"typescript": "5.5.4"
4242
},
4343
"peerDependencies": {
4444
"react": "^16",

‎src/__tests__/createCache.ts‎

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,11 @@
11
import {createCache} from '../createCache'
22

33
test('createCache returns correct result', () => {
4-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5-
const cacheStateSelector = (state: any) => state
6-
const typenames = {
7-
users: {} as {id: number},
8-
}
9-
10-
const getUser = async (id: number) => ({
11-
result: id,
12-
merge: {
13-
[id]: {id},
14-
},
15-
})
16-
17-
const updateUser = async (id: number) => ({
18-
result: id,
19-
})
20-
21-
const {cache, reducer, actions, selectors, hooks} = createCache({
22-
cacheStateSelector,
23-
options: {
24-
logsEnabled: false,
25-
validateFunctionArguments: true,
26-
},
27-
typenames,
28-
queries: {
29-
getUser: {
30-
query: getUser,
31-
},
32-
},
33-
mutations: {
34-
updateUser: {
35-
mutation: updateUser,
36-
},
37-
},
38-
})
4+
const {cache, actions, hooks, reducer, selectors, utils} = createTestingCache()
395

406
expect(cache).toStrictEqual({
417
abortControllers: new WeakMap(),
42-
cacheStateSelector,
8+
cacheStateSelector: cache.cacheStateSelector,
439
options: {
4410
logsEnabled: false,
4511
validateFunctionArguments: true,
@@ -81,4 +47,73 @@ test('createCache returns correct result', () => {
8147
expect(hooks.useMutation).toBeDefined()
8248
expect(hooks.useSelectEntityById).toBeDefined()
8349
expect(hooks.useClient).toBeDefined()
50+
51+
expect(utils.applyEntityChanges).toBeDefined()
52+
})
53+
54+
test('supports multiple cache reducers', () => {
55+
const {
56+
reducer,
57+
actions: {mergeEntityChanges},
58+
} = createTestingCache()
59+
60+
const {
61+
actions: {mergeEntityChanges: mergeEntityChangesInSecondCache},
62+
} = createTestingCache()
63+
64+
const state = reducer(
65+
undefined,
66+
mergeEntityChanges({
67+
entities: {
68+
users: {1: {id: 1}},
69+
},
70+
})
71+
)
72+
// here reducer should not handle action from second cache - should not remove user
73+
const stateAfterSecondCacheAction = reducer(
74+
state,
75+
mergeEntityChangesInSecondCache({
76+
remove: {
77+
users: [1],
78+
},
79+
})
80+
)
81+
82+
expect(stateAfterSecondCacheAction).toBe(state)
83+
})
84+
85+
const typenames = {
86+
users: {} as {id: number},
87+
}
88+
89+
const getUser = async (id: number) => ({
90+
result: id,
91+
merge: {
92+
[id]: {id},
93+
},
8494
})
95+
96+
const updateUser = async (id: number) => ({
97+
result: id,
98+
})
99+
100+
const createTestingCache = () => {
101+
return createCache({
102+
name: 'cache',
103+
options: {
104+
logsEnabled: false,
105+
validateFunctionArguments: true,
106+
},
107+
typenames,
108+
queries: {
109+
getUser: {
110+
query: getUser,
111+
},
112+
},
113+
mutations: {
114+
updateUser: {
115+
mutation: updateUser,
116+
},
117+
},
118+
})
119+
}

‎src/actions.ts‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎src/createActions.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import type {EntityChanges, Key, QueryMutationState, Typenames} from './types'
2+
import {PACKAGE_SHORT_NAME} from './utilsAndConstants'
3+
4+
export type ActionMap<N extends string, T extends Typenames, QR, MR> = ReturnType<
5+
typeof createActions<N, T, QR, MR>
6+
>
7+
8+
export const createActions = <N extends string, T extends Typenames, QR, MR>(name: N) => {
9+
const actionPrefix = `@${PACKAGE_SHORT_NAME}/${name}/` as const
10+
11+
const updateQueryStateAndEntitiesType = `${actionPrefix}UPDATE_QUERY_STATE_AND_ENTITIES` as const
12+
/** Updates query state, and optionally merges entity changes in a single action. */
13+
const updateQueryStateAndEntities = <K extends keyof QR>(
14+
queryKey: K,
15+
queryCacheKey: Key,
16+
state?: Partial<QueryMutationState<QR[K]>>,
17+
entityChagnes?: EntityChanges<T>
18+
) => ({
19+
type: updateQueryStateAndEntitiesType,
20+
queryKey,
21+
queryCacheKey,
22+
state,
23+
entityChagnes,
24+
})
25+
updateQueryStateAndEntities.type = updateQueryStateAndEntitiesType
26+
27+
const updateMutationStateAndEntitiesType =
28+
`${actionPrefix}UPDATE_MUTATION_STATE_AND_ENTITIES` as const
29+
/** Updates mutation state, and optionally merges entity changes in a single action. */
30+
const updateMutationStateAndEntities = <K extends keyof MR>(
31+
mutationKey: K,
32+
state?: Partial<QueryMutationState<MR[K]>>,
33+
entityChagnes?: EntityChanges<T>
34+
) => ({
35+
type: updateMutationStateAndEntitiesType,
36+
mutationKey,
37+
state,
38+
entityChagnes,
39+
})
40+
updateMutationStateAndEntities.type = updateMutationStateAndEntitiesType
41+
42+
const mergeEntityChangesType = `${actionPrefix}MERGE_ENTITY_CHANGES` as const
43+
/** Merge EntityChanges to the state. */
44+
const mergeEntityChanges = (changes: EntityChanges<T>) => ({
45+
type: mergeEntityChangesType,
46+
changes,
47+
})
48+
mergeEntityChanges.type = mergeEntityChangesType
49+
50+
const clearQueryStateType = `${actionPrefix}CLEAR_QUERY_STATE` as const
51+
/** Clear states for provided query keys and cache keys.
52+
* If cache key for query key is not provided, the whole state for query key is cleared. */
53+
const clearQueryState = <K extends keyof QR>(queryKeys: {key: K; cacheKey?: Key}[]) => ({
54+
type: clearQueryStateType,
55+
queryKeys,
56+
})
57+
clearQueryState.type = clearQueryStateType
58+
59+
const clearMutationStateType = `${actionPrefix}CLEAR_MUTATION_STATE` as const
60+
/** Clear states for provided mutation keys. */
61+
const clearMutationState = <K extends keyof MR>(mutationKeys: K[]) => ({
62+
type: clearMutationStateType,
63+
mutationKeys,
64+
})
65+
clearMutationState.type = clearMutationStateType
66+
67+
return {
68+
updateQueryStateAndEntities,
69+
updateMutationStateAndEntities,
70+
mergeEntityChanges,
71+
clearQueryState,
72+
clearMutationState,
73+
}
74+
}

0 commit comments

Comments
 (0)