Skip to content

Commit e995550

Browse files
authored
feat(use-dataloader): add cache key prefix on Provider (#202)
1 parent 07a86e7 commit e995550

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

packages/use-dataloader/src/DataLoaderProvider.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import React, {
99

1010
export const DataLoaderContext = createContext()
1111

12-
const DataLoaderProvider = ({ children }) => {
12+
const DataLoaderProvider = ({ children, cacheKeyPrefix }) => {
1313
const cachedData = useRef({})
1414
const reloads = useRef({})
1515

@@ -34,11 +34,11 @@ const DataLoaderProvider = ({ children }) => {
3434
if (key && typeof key === 'string' && newData) {
3535
setCachedData(actualCachedData => ({
3636
...actualCachedData,
37-
[key]: newData,
37+
[`${cacheKeyPrefix ? `${cacheKeyPrefix}-` : ''}${key}`]: newData,
3838
}))
3939
}
4040
},
41-
[setCachedData],
41+
[setCachedData, cacheKeyPrefix],
4242
)
4343

4444
const addReload = useCallback(
@@ -76,13 +76,13 @@ const DataLoaderProvider = ({ children }) => {
7676
if (key && typeof key === 'string') {
7777
setCachedData(actualCachedData => {
7878
const tmp = actualCachedData
79-
delete tmp[key]
79+
delete tmp[`${cacheKeyPrefix ? `${cacheKeyPrefix}-` : ''}${key}`]
8080

8181
return tmp
8282
})
8383
}
8484
},
85-
[setCachedData],
85+
[setCachedData, cacheKeyPrefix],
8686
)
8787
const clearAllCachedData = useCallback(() => {
8888
setCachedData({})
@@ -100,13 +100,20 @@ const DataLoaderProvider = ({ children }) => {
100100
)
101101
}, [])
102102

103-
const getCachedData = useCallback(key => {
104-
if (key) {
105-
return cachedData.current[key] || undefined
106-
}
103+
const getCachedData = useCallback(
104+
key => {
105+
if (key) {
106+
return (
107+
cachedData.current[
108+
`${cacheKeyPrefix ? `${cacheKeyPrefix}-` : ''}${key}`
109+
] || undefined
110+
)
111+
}
107112

108-
return cachedData.current
109-
}, [])
113+
return cachedData.current
114+
},
115+
[cacheKeyPrefix],
116+
)
110117

111118
const getReloads = useCallback(key => {
112119
if (key) {
@@ -151,9 +158,14 @@ const DataLoaderProvider = ({ children }) => {
151158
}
152159

153160
DataLoaderProvider.propTypes = {
161+
cacheKeyPrefix: PropTypes.string,
154162
children: PropTypes.node.isRequired,
155163
}
156164

165+
DataLoaderProvider.defaultProps = {
166+
cacheKeyPrefix: undefined,
167+
}
168+
157169
export const useDataLoaderContext = () => useContext(DataLoaderContext)
158170

159171
export default DataLoaderProvider

packages/use-dataloader/src/__tests__/DataLoaderProvider.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import DataLoaderProvider, { useDataLoaderContext } from '../DataLoaderProvider'
66
const wrapper = ({ children }) => (
77
<DataLoaderProvider>{children}</DataLoaderProvider>
88
)
9+
10+
const wrapperWithCacheKey = ({ children }) => (
11+
<DataLoaderProvider cacheKeyPrefix="sample">{children}</DataLoaderProvider>
12+
)
13+
914
describe('DataLoaderProvider', () => {
1015
test('should render correctly', async () => {
1116
render(<DataLoaderProvider>Test</DataLoaderProvider>)
@@ -23,6 +28,22 @@ describe('DataLoaderProvider', () => {
2328
expect(Object.values(result.current.getCachedData()).length).toBe(1)
2429
expect(result.current.getCachedData().test).toBe('test')
2530
})
31+
32+
test('should add cached data with cacheKeyPrefix', async () => {
33+
const { result } = renderHook(useDataLoaderContext, {
34+
wrapper: wrapperWithCacheKey,
35+
})
36+
expect(result.current.getCachedData()).toStrictEqual({})
37+
38+
act(() => {
39+
result.current.addCachedData('test', 'test')
40+
result.current.addCachedData(3, 'testWrong')
41+
})
42+
43+
expect(Object.values(result.current.getCachedData()).length).toBe(1)
44+
expect(result.current.getCachedData()['sample-test']).toBe('test')
45+
})
46+
2647
test('should delete cached data', async () => {
2748
const { result } = renderHook(useDataLoaderContext, { wrapper })
2849

@@ -49,6 +70,34 @@ describe('DataLoaderProvider', () => {
4970
expect(result.current.getCachedData()).toStrictEqual({})
5071
})
5172

73+
test('should delete cached data with cacheKeyPrefix', async () => {
74+
const { result } = renderHook(useDataLoaderContext, {
75+
wrapper: wrapperWithCacheKey,
76+
})
77+
78+
act(() => {
79+
result.current.addCachedData('testA', 'testA')
80+
result.current.addCachedData('testB', 'testB')
81+
result.current.addCachedData('testC', 'testC')
82+
result.current.addCachedData('testD', 'testD')
83+
result.current.addCachedData('testE', 'testE')
84+
})
85+
expect(result.current.getCachedData('testA')).toBe('testA')
86+
87+
act(() => {
88+
result.current.clearCachedData()
89+
result.current.clearCachedData('testA')
90+
})
91+
expect(Object.values(result.current.getCachedData()).length).toBe(4)
92+
expect(result.current.getCachedData().testA).toBe(undefined)
93+
94+
act(() => {
95+
result.current.clearAllCachedData()
96+
})
97+
expect(Object.values(result.current.getCachedData()).length).toBe(0)
98+
expect(result.current.getCachedData()).toStrictEqual({})
99+
})
100+
52101
test('should get cached data', async () => {
53102
const { result } = renderHook(useDataLoaderContext, { wrapper })
54103
expect(result.current.getCachedData()).toStrictEqual({})

0 commit comments

Comments
 (0)