Skip to content

Commit c2b92d1

Browse files
committed
simplify again
1 parent 06c9e9f commit c2b92d1

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/useCustomEffect.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import useMounted from './useMounted'
1010

1111
export type EffectHook = (effect: EffectCallback, deps?: DependencyList) => void
1212

13-
export type IsEqual<TDeps extends DependencyList> = (nextDeps: TDeps, prevDeps: TDeps) => boolean
13+
export type IsEqual<TDeps extends DependencyList> = (
14+
nextDeps: TDeps,
15+
prevDeps: TDeps,
16+
) => boolean
1417

15-
export type CustomEffectOptions<TDeps> = {
18+
export type CustomEffectOptions<TDeps extends DependencyList> = {
1619
isEqual: IsEqual<TDeps>
1720
effectHook?: EffectHook
1821
}
@@ -79,10 +82,9 @@ function useCustomEffect<TDeps extends DependencyList = DependencyList>(
7982
cleanupRef.current = null
8083
if (cleanup) cleanup()
8184
}
82-
cleanupRef.current.cleanup = cleanup
8385
}
8486

85-
return cleanupRef.current ?? undefined
87+
return cleanupRef.current
8688
})
8789

8890
useDebugValue(effect)

src/useMutationObserver.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import useCustomEffect, { IsEqual } from './useCustomEffect'
1+
import useCustomEffect from './useCustomEffect'
22
import isEqual from 'lodash/isEqual'
33
import useImmediateUpdateEffect from './useImmediateUpdateEffect'
4-
import useMountEffect from './useMountEffect'
54
import useEventCallback from './useEventCallback'
6-
import { useRef } from 'react'
75

86
type Deps = [Element | null | undefined, MutationObserverInit]
97

108
function isDepsEqual(
119
[nextElement, nextConfig]: Deps,
1210
[prevElement, prevConfig]: Deps,
1311
) {
14-
return (
15-
nextElement === prevElement &&
16-
isEqual(nextConfig, prevConfig)
17-
);
12+
return nextElement === prevElement && isEqual(nextConfig, prevConfig)
1813
}
1914

2015
/**
@@ -42,20 +37,18 @@ function useMutationObserver(
4237
config: MutationObserverInit,
4338
callback: MutationCallback,
4439
): void {
45-
const observerRef = useRef<MutationObserver | null>()
4640
const fn = useEventCallback(callback)
4741

48-
useMountEffect(() => {
49-
if (!element) return
50-
51-
observerRef.current = new MutationObserver(fn)
52-
})
53-
5442
useCustomEffect(
5543
() => {
5644
if (!element) return
5745

58-
const observer = observerRef.current || new MutationObserver(fn)
46+
// The behavior around reusing mutation observers is confusing
47+
// observing again _should_ disable the last listener but doesn't
48+
// seem to always be the case, maybe just in JSDOM? In any case the cost
49+
// to redeclaring it is gonna be fairly low anyway, so make it simple
50+
const observer = new MutationObserver(fn)
51+
5952
observer.observe(element, config)
6053

6154
return () => {

test/useMutationObserver.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('useMutationObserver', () => {
7070
wrapper.setProps({ attributeFilter: undefined, role: 'button' })
7171

7272
await Promise.resolve()
73-
73+
// console.log(spy.mock.calls[1][0][0].target)
7474
expect(spy).toHaveBeenCalledTimes(1)
7575

7676
expect(spy).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)