How to use markSchedulerSuspended method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Scheduler.js

Source: Scheduler.js Github

copy
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9/* eslint-disable no-var */
10
11import {
12  enableSchedulerDebugging,
13  enableProfiling,
14} from './SchedulerFeatureFlags';
15import {
16  requestHostCallback,
17  requestHostTimeout,
18  cancelHostTimeout,
19  shouldYieldToHost,
20  getCurrentTime,
21  forceFrameRate,
22  requestPaint,
23} from './SchedulerHostConfig';
24import {push, pop, peek} from './SchedulerMinHeap';
25
26// TODO: Use symbols?
27import {
28  ImmediatePriority,
29  UserBlockingPriority,
30  NormalPriority,
31  LowPriority,
32  IdlePriority,
33} from './SchedulerPriorities';
34import {
35  sharedProfilingBuffer,
36  markTaskRun,
37  markTaskYield,
38  markTaskCompleted,
39  markTaskCanceled,
40  markTaskErrored,
41  markSchedulerSuspended,
42  markSchedulerUnsuspended,
43  markTaskStart,
44  stopLoggingProfilingEvents,
45  startLoggingProfilingEvents,
46} from './SchedulerProfiling';
47
48// Max 31 bit integer. The max integer size in V8 for 32-bit systems.
49// Math.pow(2, 30) - 1
50// 0b111111111111111111111111111111
51var maxSigned31BitInt = 1073741823;
52
53// Times out immediately
54var IMMEDIATE_PRIORITY_TIMEOUT = -1;
55// Eventually times out
56var USER_BLOCKING_PRIORITY = 250;
57var NORMAL_PRIORITY_TIMEOUT = 5000;
58var LOW_PRIORITY_TIMEOUT = 10000;
59// Never times out
60var IDLE_PRIORITY = maxSigned31BitInt;
61
62// Tasks are stored on a min heap
63var taskQueue = [];
64var timerQueue = [];
65
66// Incrementing id counter. Used to maintain insertion order.
67var taskIdCounter = 1;
68
69// Pausing the scheduler is useful for debugging.
70var isSchedulerPaused = false;
71
72var currentTask = null;
73var currentPriorityLevel = NormalPriority;
74
75// This is set while performing work, to prevent re-entrancy.
76var isPerformingWork = false;
77
78var isHostCallbackScheduled = false;
79var isHostTimeoutScheduled = false;
80
81function advanceTimers(currentTime) {
82  // Check for tasks that are no longer delayed and add them to the queue.
83  let timer = peek(timerQueue);
84  while (timer !== null) {
85    if (timer.callback === null) {
86      // Timer was cancelled.
87      pop(timerQueue);
88    } else if (timer.startTime <= currentTime) {
89      // Timer fired. Transfer to the task queue.
90      pop(timerQueue);
91      timer.sortIndex = timer.expirationTime;
92      push(taskQueue, timer);
93      if (enableProfiling) {
94        markTaskStart(timer, currentTime);
95        timer.isQueued = true;
96      }
97    } else {
98      // Remaining timers are pending.
99      return;
100    }
101    timer = peek(timerQueue);
102  }
103}
104
105function handleTimeout(currentTime) {
106  isHostTimeoutScheduled = false;
107  advanceTimers(currentTime);
108
109  if (!isHostCallbackScheduled) {
110    if (peek(taskQueue) !== null) {
111      isHostCallbackScheduled = true;
112      requestHostCallback(flushWork);
113    } else {
114      const firstTimer = peek(timerQueue);
115      if (firstTimer !== null) {
116        requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
117      }
118    }
119  }
120}
121
122function flushWork(hasTimeRemaining, initialTime) {
123  if (enableProfiling) {
124    markSchedulerUnsuspended(initialTime);
125  }
126
127  // We'll need a host callback the next time work is scheduled.
128  isHostCallbackScheduled = false;
129  if (isHostTimeoutScheduled) {
130    // We scheduled a timeout but it's no longer needed. Cancel it.
131    isHostTimeoutScheduled = false;
132    cancelHostTimeout();
133  }
134
135  isPerformingWork = true;
136  const previousPriorityLevel = currentPriorityLevel;
137  try {
138    if (enableProfiling) {
139      try {
140        return workLoop(hasTimeRemaining, initialTime);
141      } catch (error) {
142        if (currentTask !== null) {
143          const currentTime = getCurrentTime();
144          markTaskErrored(currentTask, currentTime);
145          currentTask.isQueued = false;
146        }
147        throw error;
148      }
149    } else {
150      // No catch in prod codepath.
151      return workLoop(hasTimeRemaining, initialTime);
152    }
153  } finally {
154    currentTask = null;
155    currentPriorityLevel = previousPriorityLevel;
156    isPerformingWork = false;
157    if (enableProfiling) {
158      const currentTime = getCurrentTime();
159      markSchedulerSuspended(currentTime);
160    }
161  }
162}
163
164function workLoop(hasTimeRemaining, initialTime) {
165  let currentTime = initialTime;
166  advanceTimers(currentTime);
167  currentTask = peek(taskQueue);
168  while (
169    currentTask !== null &&
170    !(enableSchedulerDebugging && isSchedulerPaused)
171  ) {
172    if (
173      currentTask.expirationTime > currentTime &&
174      (!hasTimeRemaining || shouldYieldToHost())
175    ) {
176      // This currentTask hasn't expired, and we've reached the deadline.
177      break;
178    }
179    const callback = currentTask.callback;
180    if (callback !== null) {
181      currentTask.callback = null;
182      currentPriorityLevel = currentTask.priorityLevel;
183      const didUserCallbackTimeout = currentTask.expirationTime <= currentTime;
184      markTaskRun(currentTask, currentTime);
185      const continuationCallback = callback(didUserCallbackTimeout);
186      currentTime = getCurrentTime();
187      if (typeof continuationCallback === 'function') {
188        currentTask.callback = continuationCallback;
189        markTaskYield(currentTask, currentTime);
190      } else {
191        if (enableProfiling) {
192          markTaskCompleted(currentTask, currentTime);
193          currentTask.isQueued = false;
194        }
195        if (currentTask === peek(taskQueue)) {
196          pop(taskQueue);
197        }
198      }
199      advanceTimers(currentTime);
200    } else {
201      pop(taskQueue);
202    }
203    currentTask = peek(taskQueue);
204  }
205  // Return whether there's additional work
206  if (currentTask !== null) {
207    return true;
208  } else {
209    let firstTimer = peek(timerQueue);
210    if (firstTimer !== null) {
211      requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
212    }
213    return false;
214  }
215}
216
217function unstable_runWithPriority(priorityLevel, eventHandler) {
218  switch (priorityLevel) {
219    case ImmediatePriority:
220    case UserBlockingPriority:
221    case NormalPriority:
222    case LowPriority:
223    case IdlePriority:
224      break;
225    default:
226      priorityLevel = NormalPriority;
227  }
228
229  var previousPriorityLevel = currentPriorityLevel;
230  currentPriorityLevel = priorityLevel;
231
232  try {
233    return eventHandler();
234  } finally {
235    currentPriorityLevel = previousPriorityLevel;
236  }
237}
238
239function unstable_next(eventHandler) {
240  var priorityLevel;
241  switch (currentPriorityLevel) {
242    case ImmediatePriority:
243    case UserBlockingPriority:
244    case NormalPriority:
245      // Shift down to normal priority
246      priorityLevel = NormalPriority;
247      break;
248    default:
249      // Anything lower than normal priority should remain at the current level.
250      priorityLevel = currentPriorityLevel;
251      break;
252  }
253
254  var previousPriorityLevel = currentPriorityLevel;
255  currentPriorityLevel = priorityLevel;
256
257  try {
258    return eventHandler();
259  } finally {
260    currentPriorityLevel = previousPriorityLevel;
261  }
262}
263
264function unstable_wrapCallback(callback) {
265  var parentPriorityLevel = currentPriorityLevel;
266  return function() {
267    // This is a fork of runWithPriority, inlined for performance.
268    var previousPriorityLevel = currentPriorityLevel;
269    currentPriorityLevel = parentPriorityLevel;
270
271    try {
272      return callback.apply(this, arguments);
273    } finally {
274      currentPriorityLevel = previousPriorityLevel;
275    }
276  };
277}
278
279function timeoutForPriorityLevel(priorityLevel) {
280  switch (priorityLevel) {
281    case ImmediatePriority:
282      return IMMEDIATE_PRIORITY_TIMEOUT;
283    case UserBlockingPriority:
284      return USER_BLOCKING_PRIORITY;
285    case IdlePriority:
286      return IDLE_PRIORITY;
287    case LowPriority:
288      return LOW_PRIORITY_TIMEOUT;
289    case NormalPriority:
290    default:
291      return NORMAL_PRIORITY_TIMEOUT;
292  }
293}
294
295function unstable_scheduleCallback(priorityLevel, callback, options) {
296  var currentTime = getCurrentTime();
297
298  var startTime;
299  var timeout;
300  if (typeof options === 'object' && options !== null) {
301    var delay = options.delay;
302    if (typeof delay === 'number' && delay > 0) {
303      startTime = currentTime + delay;
304    } else {
305      startTime = currentTime;
306    }
307    timeout =
308      typeof options.timeout === 'number'
309        ? options.timeout
310        : timeoutForPriorityLevel(priorityLevel);
311  } else {
312    timeout = timeoutForPriorityLevel(priorityLevel);
313    startTime = currentTime;
314  }
315
316  var expirationTime = startTime + timeout;
317
318  var newTask = {
319    id: taskIdCounter++,
320    callback,
321    priorityLevel,
322    startTime,
323    expirationTime,
324    sortIndex: -1,
325  };
326  if (enableProfiling) {
327    newTask.isQueued = false;
328  }
329
330  if (startTime > currentTime) {
331    // This is a delayed task.
332    newTask.sortIndex = startTime;
333    push(timerQueue, newTask);
334    if (peek(taskQueue) === null && newTask === peek(timerQueue)) {
335      // All tasks are delayed, and this is the task with the earliest delay.
336      if (isHostTimeoutScheduled) {
337        // Cancel an existing timeout.
338        cancelHostTimeout();
339      } else {
340        isHostTimeoutScheduled = true;
341      }
342      // Schedule a timeout.
343      requestHostTimeout(handleTimeout, startTime - currentTime);
344    }
345  } else {
346    newTask.sortIndex = expirationTime;
347    push(taskQueue, newTask);
348    if (enableProfiling) {
349      markTaskStart(newTask, currentTime);
350      newTask.isQueued = true;
351    }
352    // Schedule a host callback, if needed. If we're already performing work,
353    // wait until the next time we yield.
354    if (!isHostCallbackScheduled && !isPerformingWork) {
355      isHostCallbackScheduled = true;
356      requestHostCallback(flushWork);
357    }
358  }
359
360  return newTask;
361}
362
363function unstable_pauseExecution() {
364  isSchedulerPaused = true;
365}
366
367function unstable_continueExecution() {
368  isSchedulerPaused = false;
369  if (!isHostCallbackScheduled && !isPerformingWork) {
370    isHostCallbackScheduled = true;
371    requestHostCallback(flushWork);
372  }
373}
374
375function unstable_getFirstCallbackNode() {
376  return peek(taskQueue);
377}
378
379function unstable_cancelCallback(task) {
380  if (enableProfiling) {
381    if (task.isQueued) {
382      const currentTime = getCurrentTime();
383      markTaskCanceled(task, currentTime);
384      task.isQueued = false;
385    }
386  }
387
388  // Null out the callback to indicate the task has been canceled. (Can't
389  // remove from the queue because you can't remove arbitrary nodes from an
390  // array based heap, only the first one.)
391  task.callback = null;
392}
393
394function unstable_getCurrentPriorityLevel() {
395  return currentPriorityLevel;
396}
397
398function unstable_shouldYield() {
399  const currentTime = getCurrentTime();
400  advanceTimers(currentTime);
401  const firstTask = peek(taskQueue);
402  return (
403    (firstTask !== currentTask &&
404      currentTask !== null &&
405      firstTask !== null &&
406      firstTask.callback !== null &&
407      firstTask.startTime <= currentTime &&
408      firstTask.expirationTime < currentTask.expirationTime) ||
409    shouldYieldToHost()
410  );
411}
412
413const unstable_requestPaint = requestPaint;
414
415export {
416  ImmediatePriority as unstable_ImmediatePriority,
417  UserBlockingPriority as unstable_UserBlockingPriority,
418  NormalPriority as unstable_NormalPriority,
419  IdlePriority as unstable_IdlePriority,
420  LowPriority as unstable_LowPriority,
421  unstable_runWithPriority,
422  unstable_next,
423  unstable_scheduleCallback,
424  unstable_cancelCallback,
425  unstable_wrapCallback,
426  unstable_getCurrentPriorityLevel,
427  unstable_shouldYield,
428  unstable_requestPaint,
429  unstable_continueExecution,
430  unstable_pauseExecution,
431  unstable_getFirstCallbackNode,
432  getCurrentTime as unstable_now,
433  forceFrameRate as unstable_forceFrameRate,
434};
435
436export const unstable_Profiling = enableProfiling
437  ? {
438      startLoggingProfilingEvents,
439      stopLoggingProfilingEvents,
440      sharedProfilingBuffer,
441    }
442  : null;
443
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)