MCPcopy Create free account
hub / github.com/ampproject/amphtml / observe

Method observe

ads/inabox/position-observer.js:47–67  ·  view source on GitHub ↗

* Start to observe the target element's position change and trigger callback. * TODO: maybe take DOM mutation into consideration * @param {!Element} element * @param {function(!PositionEntryDef)} callback * @return {!UnlistenDef}

(element, callback)

Source from the content-addressed store, hash-verified

45 * @return {!UnlistenDef}
46 */
47 observe(element, callback) {
48 if (!this.positionObservable_) {
49 this.positionObservable_ = new Observable();
50 const listener = throttle(
51 this.win_,
52 () => {
53 this.update_();
54 this.positionObservable_.fire();
55 },
56 MIN_EVENT_INTERVAL_IN_MS
57 );
58 this.update_();
59 this.win_.addEventListener('scroll', listener, true);
60 this.win_.addEventListener('resize', listener, true);
61 }
62 // Send the 1st ping immediately
63 callback(this.getPositionEntry_(element));
64 return this.positionObservable_.add(() => {
65 callback(this.getPositionEntry_(element));
66 });
67 }
68
69 /**
70 * Updates viewport rect.

Callers 2

handleSendPositions_Method · 0.45
observeAmplifiedFunction · 0.45

Calls 7

update_Method · 0.95
getPositionEntry_Method · 0.95
throttleFunction · 0.90
callbackFunction · 0.50
fireMethod · 0.45
addEventListenerMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected