MCPcopy Index your code
hub / github.com/ampproject/amphtml / instrumentEntryPoints

Function instrumentEntryPoints

3p/environment.js:167–210  ·  view source on GitHub ↗

* Replace timers with variants that can be throttled. * @param {!Window} win

(win)

Source from the content-addressed store, hash-verified

165 * @param {!Window} win
166 */
167function instrumentEntryPoints(win) {
168 // Change setTimeout to respect a minimum timeout.
169 const {setTimeout} = win;
170 win.setTimeout = function (fn, time) {
171 time = minTime(time);
172 arguments[1] = time;
173 return setTimeout.apply(this, arguments);
174 };
175 // Implement setInterval in terms of setTimeout to make
176 // it respect the same rules
177 win.setInterval = function (fn) {
178 const id = intervalId++;
179 const args = Array.prototype.slice.call(arguments);
180 /**
181 * @return {*}
182 * @suppress {uselessCode}
183 */
184 function wrapper() {
185 next();
186 if (typeof fn == 'string') {
187 // Handle rare and dangerous string arg case.
188 return (0, win.eval) /*NOT OK but whatcha gonna do.*/
189 .call(win, fn); // lgtm [js/useless-expression]
190 } else {
191 return fn.apply(this, arguments);
192 }
193 }
194 args[0] = wrapper;
195 /**
196 *
197 */
198 function next() {
199 intervals[id] = win.setTimeout.apply(win, args);
200 }
201 next();
202 return id;
203 };
204 const {clearInterval} = win;
205 win.clearInterval = function (id) {
206 clearInterval(id);
207 win.clearTimeout(intervals[id]);
208 delete intervals[id];
209 };
210}
211
212/**
213 * Blackhole the legacy popups since they should never be used for anything.

Callers 1

manageWin_Function · 0.85

Calls 4

minTimeFunction · 0.85
applyMethod · 0.80
nextFunction · 0.70
clearIntervalFunction · 0.50

Tested by

no test coverage detected