Skip to content

Commit 913351b

Browse files
author
Daniel Schmidt
committed
Actually broke the unobserve - change method, so it works in IE11 and other browsers.
Add a basic test to ensure calling observe/unobserve doesn't trigger an error
1 parent d2b0636 commit 913351b

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/intersection.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ export function unobserve(element) {
4545
observerInstance.unobserve(element)
4646
}
4747

48-
const itemsLeft = INSTANCE_MAP.values().some(
49-
item => item.threshold === instance.threshold,
50-
)
48+
// Check if we are stilling observing any elements with the same threshold.
49+
let itemsLeft = false
50+
INSTANCE_MAP.forEach(item => {
51+
if (item.threshold === instance.threshold) {
52+
itemsLeft = true
53+
}
54+
})
5155

5256
if (observerInstance && !itemsLeft) {
53-
// No more elements to observe, disconnect
57+
// No more elements to observe for threshold, disconnect observer
5458
observerInstance.disconnect()
5559
OBSERVER_MAP.delete(instance.threshold)
5660
}

tests/intersection.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { observe, unobserve } from '../src/intersection'
2+
3+
global.IntersectionObserver = jest.fn(() => ({
4+
observe: jest.fn(),
5+
unobserve: jest.fn(),
6+
disconnect: jest.fn(),
7+
}))
8+
9+
const el = { el: 'htmlElement' }
10+
11+
it('should observe', () => {
12+
const cb = jest.fn()
13+
observe(el, cb)
14+
})
15+
16+
it('should unobserve', () => {
17+
observe(el, jest.fn())
18+
unobserve(el)
19+
})

0 commit comments

Comments
 (0)