|
1 | | -import useCustomEffect, { IsEqual } from './useCustomEffect' |
| 1 | +import useCustomEffect from './useCustomEffect' |
2 | 2 | import isEqual from 'lodash/isEqual' |
3 | 3 | import useImmediateUpdateEffect from './useImmediateUpdateEffect' |
4 | | -import useMountEffect from './useMountEffect' |
5 | 4 | import useEventCallback from './useEventCallback' |
6 | | -import { useRef } from 'react' |
7 | 5 |
|
8 | 6 | type Deps = [Element | null | undefined, MutationObserverInit] |
9 | 7 |
|
10 | 8 | function isDepsEqual( |
11 | 9 | [nextElement, nextConfig]: Deps, |
12 | 10 | [prevElement, prevConfig]: Deps, |
13 | 11 | ) { |
14 | | - return ( |
15 | | - nextElement === prevElement && |
16 | | - isEqual(nextConfig, prevConfig) |
17 | | - ); |
| 12 | + return nextElement === prevElement && isEqual(nextConfig, prevConfig) |
18 | 13 | } |
19 | 14 |
|
20 | 15 | /** |
@@ -42,20 +37,18 @@ function useMutationObserver( |
42 | 37 | config: MutationObserverInit, |
43 | 38 | callback: MutationCallback, |
44 | 39 | ): void { |
45 | | - const observerRef = useRef<MutationObserver | null>() |
46 | 40 | const fn = useEventCallback(callback) |
47 | 41 |
|
48 | | - useMountEffect(() => { |
49 | | - if (!element) return |
50 | | - |
51 | | - observerRef.current = new MutationObserver(fn) |
52 | | - }) |
53 | | - |
54 | 42 | useCustomEffect( |
55 | 43 | () => { |
56 | 44 | if (!element) return |
57 | 45 |
|
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 | + |
59 | 52 | observer.observe(element, config) |
60 | 53 |
|
61 | 54 | return () => { |
|
0 commit comments