From a55b6ac0ef68f50ebaf494bd2a889e2598c7c9bf Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Wed, 16 Oct 2024 12:46:06 -0700 Subject: [PATCH] fix: directly subscribe to configProvider to avoid memory leaks (#1019) --- .../react/src/runtimes/core/BaseThreadRuntimeCore.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/react/src/runtimes/core/BaseThreadRuntimeCore.tsx b/packages/react/src/runtimes/core/BaseThreadRuntimeCore.tsx index f405bc3ff..085066d75 100644 --- a/packages/react/src/runtimes/core/BaseThreadRuntimeCore.tsx +++ b/packages/react/src/runtimes/core/BaseThreadRuntimeCore.tsx @@ -53,11 +53,7 @@ export abstract class BaseThreadRuntimeCore implements ThreadRuntimeCore { public readonly composer = new DefaultThreadComposerRuntimeCore(this); - constructor(private configProvider: ModelConfigProvider) { - this.configProvider.subscribe?.(() => { - this._notifyEventSubscribers("model-config-update"); - }); - } + constructor(private configProvider: ModelConfigProvider) {} public getModelConfig() { return this.configProvider.getModelConfig(); @@ -179,6 +175,10 @@ export abstract class BaseThreadRuntimeCore implements ThreadRuntimeCore { private _eventSubscribers = new Map void>>(); public unstable_on(event: ThreadRuntimeEventType, callback: () => void) { + if (event === "model-config-update") { + return this.configProvider.subscribe?.(callback) ?? (() => {}); + } + const subscribers = this._eventSubscribers.get(event); if (!subscribers) { this._eventSubscribers.set(event, new Set([callback]));