MCPcopy
hub / github.com/angular/angular / onViewport

Function onViewport

packages/core/primitives/defer/src/triggers.ts:150–211  ·  view source on GitHub ↗
(
  trigger: Element,
  callback: VoidFunction,
  observerFactoryFn: (options?: IntersectionObserverInit) => IntersectionObserver,
  options?: IntersectionObserverInit,
)

Source from the content-addressed store, hash-verified

148 * intersectionObserver to null if there are no more Elements to observe
149 */
150export function onViewport(
151 trigger: Element,
152 callback: VoidFunction,
153 observerFactoryFn: (options?: IntersectionObserverInit) => IntersectionObserver,
154 options?: IntersectionObserverInit,
155): VoidFunction {
156 const key = getIntersectionObserverKey(options);
157 let entry = viewportTriggers.get(trigger)?.get(key);
158
159 if (!intersectionObservers.has(key)) {
160 intersectionObservers.set(key, {observer: observerFactoryFn(options), count: 0});
161 }
162
163 const config = intersectionObservers.get(key)!;
164
165 if (!entry) {
166 entry = new DeferEventEntry();
167 config.observer.observe(trigger);
168
169 let triggerConfig = viewportTriggers.get(trigger);
170
171 if (triggerConfig) {
172 triggerConfig.set(key, entry);
173 } else {
174 triggerConfig = new Map();
175 viewportTriggers.set(trigger, triggerConfig);
176 }
177
178 triggerConfig.set(key, entry);
179 config.count++;
180 }
181
182 entry.callbacks.add(callback);
183
184 return () => {
185 if (!viewportTriggers.get(trigger)?.has(key)) {
186 return;
187 }
188
189 entry!.callbacks.delete(callback);
190
191 if (entry!.callbacks.size === 0) {
192 config.observer.unobserve(trigger);
193 config.count--;
194
195 const triggerConfig = viewportTriggers.get(trigger);
196
197 if (triggerConfig) {
198 triggerConfig.delete(key);
199
200 if (triggerConfig.size === 0) {
201 viewportTriggers.delete(trigger);
202 }
203 }
204 }
205
206 if (config.count === 0) {
207 config.observer.disconnect();

Callers 1

onViewportWrapperFunction · 0.90

Calls 9

getMethod · 0.65
hasMethod · 0.65
setMethod · 0.65
addMethod · 0.65
disconnectMethod · 0.65
observeMethod · 0.45
deleteMethod · 0.45
unobserveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…