Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor[ReactDevToolsBindingsModel]: dont send messages when execution context is destroyed #114

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions front_end/models/react_native/ReactDevToolsBindingsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,27 @@ export class ReactDevToolsBindingsModel extends SDK.SDKModel.SDKModel {
}

async sendMessage(domainName: DomainName, message: JSONValue): Promise<void> {
// If Execution Context is destroyed, do not attempt to send a message (evaluate anything)
// This could happen when we destroy Bridge from ReactDevToolsModel, which attempts to send `shutdown` event
// We still need to call `bridge.shutdown()` in order to unsubscribe all listeners on the Frontend (this) side
if (!this.fuseboxDispatcherIsInitialized) {
return;
}

const runtimeModel = this.target().model(SDK.RuntimeModel.RuntimeModel);
if (!runtimeModel) {
throw new Error(`Failed to send message from ReactDevToolsBindingsModel for domain ${domainName}: runtime model is not available`);
}

const serializedMessage = JSON.stringify(message);

await runtimeModel.agent.invoke_evaluate({expression: `${RUNTIME_GLOBAL}.sendMessage('${domainName}', '${serializedMessage}')`});
const sendMessageExpression = `${RUNTIME_GLOBAL}.sendMessage('${domainName}', '${serializedMessage}')`;

// As of today, all expressions which were scheduled via Runtime.evaluate method are not going through RuntimeScheduler
// This means that the task is not being placed on the Event Loop, and any transitively called Microtasks are not executed properly
// We are wrapping the expression in setTimeout to make sure that this code (and everything what is called by it) goes through the Event Loop implementation
// See T200616136 for more context
// TODO(hoxyq): remove setTimeout once Runtime.evaluate expressions are passed through React Native Runtime Scheduler
await runtimeModel.agent.invoke_evaluate({expression: `void setTimeout(() => ${sendMessageExpression}, 0)`});
}

async enable(): Promise<void> {
Expand Down
Loading