Skip to content

Commit 6287ea3

Browse files
committed
fix: rerender remounted components
1 parent 05944e0 commit 6287ea3

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/shared/event.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,26 @@ export function removeListener(
1616
cache: reactCache,
1717
id: string,
1818
keys?: (keyof reactCache)[],
19-
): void {
20-
(keys ?? Object.keys(cache)).forEach(key => {
21-
delProperty(cache, [key, 'listeners', id])
19+
): (keyof reactCache)[] {
20+
const removedKeys: (keyof reactCache)[] = []
21+
const traverseKeys = (keys ?? Object.keys(cache))
22+
23+
traverseKeys.forEach(key => {
24+
if (cache[key]?.listeners?.[id]) {
25+
delProperty(cache, [key, 'listeners', id])
26+
removedKeys.push(key)
27+
}
2228
})
29+
30+
return removedKeys
2331
}
2432

2533
export function dispatch(
2634
cache: reactCache,
2735
keys: (keyof reactCache)[],
2836
): void {
2937
const listeners: reactCacheEntry['listeners'] = {}
38+
3039
keys.forEach(key => {
3140
Object.entries(cache[key]?.listeners ?? {}).forEach(([id, listener]) => {
3241
listeners[id] = listener

src/useCached.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { useContext, useEffect, useMemo, useRef, useState } from 'react'
1+
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
22
import { createStore } from 'idb-keyval'
3-
import { reactCache, removeListener } from './shared'
3+
import { addListener, reactCache, removeListener } from './shared'
44
import { createApi } from './methods'
55
import { CacheContext } from './context'
66

@@ -24,16 +24,19 @@ export function useCached({dbName = 'Cached', storeName = 'keyval', context = tr
2424
}
2525
const cache = context ? contextCache[dbName][storeName] : componentCache
2626

27+
const subscribedKeys = useRef<string[]>([])
2728
const id = useRef(Math.random().toString(36)).current
28-
const [, setState] = useState({})
29+
const [, _setState] = useState({})
30+
const rerender = useCallback(() => _setState({}), [_setState])
2931

3032
useEffect(() => {
33+
addListener(cache, subscribedKeys.current, id, rerender)
3134
return () => {
32-
removeListener(cache, id)
35+
subscribedKeys.current = removeListener(cache, id)
3336
}
3437
})
3538

36-
const api = useMemo(() => createApi(cache, store, id, () => setState({})), [cache, store, id, setState])
39+
const api = useMemo(() => createApi(cache, store, id, rerender), [cache, store, id, rerender])
3740

3841
return api
3942
}

test/shared/event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ it('remove listeners', () => {
4040
},
4141
}
4242

43-
removeListener(cache, 'listenerTestA', ['foo', 'baz'])
44-
removeListener(cache, 'listenerTestB')
43+
expect(removeListener(cache, 'listenerTestA', ['foo', 'baz'])).toEqual(['foo'])
44+
expect(removeListener(cache, 'listenerTestB')).toEqual(['bar'])
4545

4646
expect(cache).toEqual({
4747
bar: {

0 commit comments

Comments
 (0)