| 29 | const idList: Identifier[] = [] |
| 30 | |
| 31 | function createObserver(options: UseIntersectionObserverInit): Observer { |
| 32 | const id = { |
| 33 | root: options.root || null, |
| 34 | margin: options.rootMargin || '', |
| 35 | } |
| 36 | const existing = idList.find( |
| 37 | (obj) => obj.root === id.root && obj.margin === id.margin |
| 38 | ) |
| 39 | let instance: Observer | undefined |
| 40 | |
| 41 | if (existing) { |
| 42 | instance = observers.get(existing) |
| 43 | if (instance) { |
| 44 | return instance |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | const elements = new Map<Element, ObserveCallback>() |
| 49 | const observer = new IntersectionObserver((entries) => { |
| 50 | entries.forEach((entry) => { |
| 51 | const callback = elements.get(entry.target) |
| 52 | const isVisible = entry.isIntersecting || entry.intersectionRatio > 0 |
| 53 | if (callback && isVisible) { |
| 54 | callback(isVisible) |
| 55 | } |
| 56 | }) |
| 57 | }, options) |
| 58 | instance = { |
| 59 | id, |
| 60 | observer, |
| 61 | elements, |
| 62 | } |
| 63 | |
| 64 | idList.push(id) |
| 65 | observers.set(id, instance) |
| 66 | return instance |
| 67 | } |
| 68 | |
| 69 | function observe( |
| 70 | element: Element, |