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

Unsubscribe React DevTools events #129

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions front_end/models/react_native/ReactDevToolsBindingsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export class ReactDevToolsBindingsModel extends SDK.SDKModel.SDKModel {
private fuseboxDispatcherIsInitialized = false;
private readonly domainToMessageQueue: Map<DomainName, Array<JSONValue>> = new Map();

override dispose(): void {
this.domainToListeners.clear();
this.domainToMessageQueue.clear();

const runtimeModel = this.target().model(SDK.RuntimeModel.RuntimeModel);
runtimeModel?.removeEventListener(SDK.RuntimeModel.Events.BindingCalled, this.bindingCalled, this);
runtimeModel?.removeEventListener(
SDK.RuntimeModel.Events.ExecutionContextCreated, this.onExecutionContextCreated, this);
runtimeModel?.removeEventListener(
SDK.RuntimeModel.Events.ExecutionContextDestroyed, this.onExecutionContextDestroyed, this);
}

private bindingCalled(event: BindingCalledEventTargetEvent): void {
// If binding name is not initialized, then we failed to get its name
if (this.messagingBindingName === null || event.data.name !== this.messagingBindingName) {
Expand Down
42 changes: 40 additions & 2 deletions front_end/panels/react_devtools/ReactDevToolsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,34 @@ export class ReactDevToolsModel extends SDK.SDKModel.SDKModel<EventTypes> {
);

// Notify backend if Chrome DevTools was closed, marking frontend as disconnected
window.addEventListener('beforeunload', () => this.#bridge?.shutdown());
window.addEventListener('beforeunload', this.#handleBeforeUnload);
}

override dispose(): void {
this.#bridge?.removeListener('reloadAppForProfiling', this.#handleReloadAppForProfiling);
this.#bridge?.shutdown();

this.#bindingsModel.removeEventListener(
ReactNativeModels.ReactDevToolsBindingsModel.Events.BackendExecutionContextCreated,
this.#handleBackendExecutionContextCreated,
this,
);
this.#bindingsModel.removeEventListener(
ReactNativeModels.ReactDevToolsBindingsModel.Events.BackendExecutionContextUnavailable,
this.#handleBackendExecutionContextUnavailable,
this,
);
this.#bindingsModel.removeEventListener(
ReactNativeModels.ReactDevToolsBindingsModel.Events.BackendExecutionContextDestroyed,
this.#handleBackendExecutionContextDestroyed,
this,
);

window.removeEventListener('beforeunload', this.#handleBeforeUnload);

this.#bridge = null;
this.#store = null;
this.#listeners.clear();
}

ensureInitialized(): void {
Expand Down Expand Up @@ -147,6 +174,10 @@ export class ReactDevToolsModel extends SDK.SDKModel.SDKModel<EventTypes> {
return rdtBindingsModel.sendMessage(ReactDevToolsModel.FUSEBOX_BINDING_NAMESPACE, message);
}

#handleBeforeUnload = (): void => {
this.#bridge?.shutdown();
};

#handleBackendExecutionContextCreated(): void {
const rdtBindingsModel = this.#bindingsModel;
if (!rdtBindingsModel) {
Expand All @@ -163,10 +194,17 @@ export class ReactDevToolsModel extends SDK.SDKModel.SDKModel<EventTypes> {

#finishInitializationAndNotify(): void {
this.#bridge = ReactDevTools.createBridge(this.#wall);
this.#store = ReactDevTools.createStore(this.#bridge);
this.#store = ReactDevTools.createStore(this.#bridge, {
supportsReloadAndProfile: true,
});
this.#bridge.addListener('reloadAppForProfiling', this.#handleReloadAppForProfiling);
this.dispatchEventToListeners(Events.InitializationCompleted);
}

#handleReloadAppForProfiling(): void {
SDK.ResourceTreeModel.ResourceTreeModel.reloadAllPages(false);
}

#handleBackendExecutionContextUnavailable({data: errorMessage}: ReactDevToolsBindingsBackendExecutionContextUnavailableEvent): void {
this.dispatchEventToListeners(Events.InitializationFailed, errorMessage);
}
Expand Down
4 changes: 3 additions & 1 deletion front_end/third_party/react-devtools/package/frontend.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export type Wall = {
};

export type Bridge = {
shutdown: () => void,
addListener(event: string, listener: (params: unknown) => any): void;
removeListener(event: string, listener: Function): void;
shutdown: () => void;
};
export type Store = Object;
export type BrowserTheme = 'dark' | 'light';
Expand Down
Loading