Skip to content

Commit

Permalink
Fix debugging performance (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
RedMickey committed Oct 13, 2021
1 parent 3985608 commit b8887dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
32 changes: 23 additions & 9 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,43 @@ export function waitUntil<T>(
interval: number = 1000,
timeout?: number,
): Promise<T | null> {
return new Promise(resolve => {
return new Promise(async resolve => {
let rejectTimeout: NodeJS.Timeout | undefined;
let сheckInterval: NodeJS.Timeout | undefined;

if (timeout) {
rejectTimeout = setTimeout(() => {
cleanup();
resolve(null);
}, timeout);
}

const сheckInterval = setInterval(async () => {
const cleanup = () => {
if (rejectTimeout) {
clearTimeout(rejectTimeout);
}
if (сheckInterval) {
clearInterval(сheckInterval);
}
};

const tryToResolve = async (): Promise<boolean> => {
const result = await condition();
if (result) {
cleanup();
resolve(result);
}
}, interval);

const cleanup = () => {
if (rejectTimeout) {
clearTimeout(rejectTimeout);
}
clearInterval(сheckInterval);
return !!result;
};

const resolved = await tryToResolve();
if (resolved) {
return;
}

сheckInterval = setInterval(async () => {
await tryToResolve();
}, interval);
});
}

Expand Down
17 changes: 12 additions & 5 deletions src/debugger/forkedAppWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { IDebuggeeWorker, RNAppMessage } from "./appWorker";
import { InternalErrorCode } from "../common/error/internalErrorCode";
import { getLoggingDirectory } from "../extension/log/LogHelper";
import { generateRandomPortNumber } from "../common/extensionHelper";
import { waitUntil } from "../common/utils";

function printDebuggingError(error: Error, reason: any) {
const nestedError = ErrorHelper.getNestedError(
Expand Down Expand Up @@ -139,10 +138,18 @@ export class ForkedAppWorker implements IDebuggeeWorker {

public async postMessage(rnMessage: RNAppMessage): Promise<RNAppMessage> {
// Before sending messages, make sure that the worker is loaded
const condition = async () => {
return !!this.workerLoaded;
};
await waitUntil(condition);
await new Promise(resolve => {
if (this.workerLoaded) {
resolve();
} else {
const checkWorkerLoaded = setInterval(() => {
if (this.workerLoaded) {
clearInterval(checkWorkerLoaded);
resolve();
}
}, 1000);
}
});

const promise = (async () => {
await this.workerLoaded;
Expand Down

0 comments on commit b8887dd

Please sign in to comment.