File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -19,6 +19,32 @@ test("should be able to use observe", () => {
1919 ) ;
2020} ) ;
2121
22+ test ( "should only clean up each observer callback once" , ( ) => {
23+ const element = document . createElement ( "div" ) ;
24+ const firstCallback = vi . fn ( ) ;
25+ const secondCallback = vi . fn ( ) ;
26+ const firstCleanup = observe ( element , firstCallback , { threshold : 0.1 } ) ;
27+ const secondCleanup = observe ( element , secondCallback , { threshold : 0.1 } ) ;
28+ const observer = intersectionMockInstance ( element ) ;
29+
30+ firstCleanup ( ) ;
31+ firstCleanup ( ) ;
32+ mockIsIntersecting ( element , true ) ;
33+
34+ expect ( firstCallback ) . not . toHaveBeenCalled ( ) ;
35+ expect ( secondCallback ) . toHaveBeenCalledTimes ( 1 ) ;
36+ expect ( observer . unobserve ) . not . toHaveBeenCalled ( ) ;
37+
38+ secondCleanup ( ) ;
39+ expect ( observer . unobserve ) . toHaveBeenCalledTimes ( 1 ) ;
40+ expect ( observer . unobserve ) . toHaveBeenCalledWith ( element ) ;
41+ expect ( observer . disconnect ) . toHaveBeenCalledTimes ( 1 ) ;
42+
43+ secondCleanup ( ) ;
44+ expect ( observer . unobserve ) . toHaveBeenCalledTimes ( 1 ) ;
45+ expect ( observer . disconnect ) . toHaveBeenCalledTimes ( 1 ) ;
46+ } ) ;
47+
2248test ( "should convert options to id" , ( ) => {
2349 expect (
2450 optionsToId ( {
Original file line number Diff line number Diff line change @@ -156,7 +156,11 @@ export function observe(
156156 callbacks . push ( callback ) ;
157157 observer . observe ( element ) ;
158158
159+ let unobserved = false ;
159160 return function unobserve ( ) {
161+ if ( unobserved ) return ;
162+ unobserved = true ;
163+
160164 // Remove the callback from the callback list
161165 callbacks . splice ( callbacks . indexOf ( callback ) , 1 ) ;
162166
You can’t perform that action at this time.
0 commit comments