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

Function observe

packages/next/src/client/use-intersection.tsx:69–94  ·  view source on GitHub ↗
(
  element: Element,
  callback: ObserveCallback,
  options: UseIntersectionObserverInit
)

Source from the content-addressed store, hash-verified

67}
68
69function observe(
70 element: Element,
71 callback: ObserveCallback,
72 options: UseIntersectionObserverInit
73): () => void {
74 const { id, observer, elements } = createObserver(options)
75 elements.set(element, callback)
76
77 observer.observe(element)
78 return function unobserve(): void {
79 elements.delete(element)
80 observer.unobserve(element)
81
82 // Destroy observer when there's nothing left to watch:
83 if (elements.size === 0) {
84 observer.disconnect()
85 observers.delete(id)
86 const index = idList.findIndex(
87 (obj) => obj.root === id.root && obj.margin === id.margin
88 )
89 if (index > -1) {
90 idList.splice(index, 1)
91 }
92 }
93 }
94}
95
96export function useIntersection<T extends Element>({
97 rootRef,

Callers 2

useIntersectionFunction · 0.85
commitHookFunction · 0.85

Calls 4

createObserverFunction · 0.85
setMethod · 0.65
deleteMethod · 0.45
spliceMethod · 0.45

Tested by

no test coverage detected