Files
react-native-devtools-frontend/test/shared/async-scope.ts
T
Takuto Ikuta a36c4e87eb extract AsyncScope to separate file
This is extracted from https://crrev.com/c/3330076/36

This is to fix interactions test failure like
```
    1) shows cookies even when navigating to an unreachable page (crbug.com/1047348)
(node:3756728) Warning: Accessing non-existent property 'AsyncScope' of module exports inside circular dependency
    at emitCircularRequireWarning (node:internal/modules/cjs/loader:707:11)
    at Object.get (node:internal/modules/cjs/loader:721:5)
    at waitFor (/usr/local/google/home/tikuta/devtools/devtools-frontend/test/shared/helper.ts:228:67)
    at getElementPosition (/usr/local/google/home/tikuta/devtools/devtools-frontend/test/shared/helper.ts:57:21)
    at click (/usr/local/google/home/tikuta/devtools/devtools-frontend/test/shared/helper.ts:104:13)
    at navigateToApplicationTab (/usr/local/google/home/tikuta/devtools/devtools-frontend/test/e2e/helpers/application-helpers.ts:11:9)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at Context.<anonymous> (/usr/local/google/home/tikuta/devtools/devtools-frontend/test/e2e/application/cookies_test.ts:29:5)
```
when we use esbuild.

Bug: 1278663
Change-Id: I9e3ab2d19c4daa56c87e3f77ac917a13d32a7c24
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3394445
Auto-Submit: Takuto Ikuta <tikuta@chromium.org>
Reviewed-by: Tim Van der Lippe <tvanderlippe@chromium.org>
Commit-Queue: Tim Van der Lippe <tvanderlippe@chromium.org>
2022-01-17 12:40:45 +00:00

68 lines
1.5 KiB
TypeScript

// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
export class AsyncScope {
static scopes: Set<AsyncScope> = new Set();
private asyncStack: string[][] = [];
private canceled: boolean = false;
setCanceled(): void {
this.canceled = true;
}
isCanceled(): boolean {
return this.canceled;
}
get stack() {
if (this.asyncStack.length === 0) {
return null;
}
return this.asyncStack[this.asyncStack.length - 1];
}
push() {
const stack = new Error().stack;
if (!stack) {
throw new Error('Could not get stack trace');
}
if (this.asyncStack.length === 0) {
AsyncScope.scopes.add(this);
}
const filteredStack = stack.split('\n').filter(
value =>
!(value === 'Error' || value.includes('AsyncScope') || value.includes('runMicrotasks') ||
value.includes('processTicksAndRejections')));
this.asyncStack.push(filteredStack);
}
pop() {
this.asyncStack.pop();
if (this.asyncStack.length === 0) {
AsyncScope.scopes.delete(this);
}
}
async exec<T>(callable: () => Promise<T>) {
this.push();
try {
const result = await callable();
return result;
} finally {
this.pop();
}
}
execSync<T>(callable: () => T) {
this.push();
try {
const result = callable();
return result;
} finally {
this.pop();
}
}
}