MCPcopy
hub / github.com/vercel/next.js / createObserver

Function createObserver

packages/next/src/client/use-intersection.tsx:31–67  ·  view source on GitHub ↗
(options: UseIntersectionObserverInit)

Source from the content-addressed store, hash-verified

29const idList: Identifier[] = []
30
31function 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
69function observe(
70 element: Element,

Callers 1

observeFunction · 0.85

Calls 6

callbackFunction · 0.70
getMethod · 0.65
pushMethod · 0.65
setMethod · 0.65
findMethod · 0.45
forEachMethod · 0.45

Tested by

no test coverage detected