Skip to content

Commit fd1bf9c

Browse files
committed
clear debounce on unsubscribe
1 parent e8e9af8 commit fd1bf9c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/client/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class Client<
156156
private _instructions?: string;
157157
private _jsonSchemaValidator: jsonSchemaValidator;
158158
private _cachedToolOutputValidators: Map<string, JsonSchemaValidator<unknown>> = new Map();
159-
private _toolListChangedOptions: ToolListChangedOptions | null = null;
159+
private _toolListChangedOptions?: ToolListChangedOptions;
160160
private _toolListChangedDebounceTimer?: ReturnType<typeof setTimeout>;
161161

162162
/**
@@ -499,7 +499,7 @@ export class Client<
499499
const refreshToolList = async () => {
500500
// If autoRefresh is false, call the callback for the notification, but without tools data
501501
if (!toolListChangedOptions.autoRefresh) {
502-
toolListChangedOptions.onToolListChanged?.(null, null);
502+
toolListChangedOptions.onToolListChanged(null, null);
503503
return;
504504
}
505505

@@ -511,7 +511,7 @@ export class Client<
511511
} catch (e) {
512512
error = e instanceof Error ? e : new Error(String(e));
513513
}
514-
toolListChangedOptions.onToolListChanged?.(error, tools);
514+
toolListChangedOptions.onToolListChanged(error, tools);
515515
};
516516

517517
this.setNotificationHandler(ToolListChangedNotificationSchema, () => {
@@ -531,15 +531,19 @@ export class Client<
531531
}
532532
// Reset tool list changed options and remove notification handler
533533
else {
534-
this._toolListChangedOptions = null;
534+
this._toolListChangedOptions = undefined;
535535
this.removeNotificationHandler(ToolListChangedNotificationSchema.shape.method.value);
536+
if (this._toolListChangedDebounceTimer) {
537+
clearTimeout(this._toolListChangedDebounceTimer);
538+
this._toolListChangedDebounceTimer = undefined;
539+
}
536540
}
537541
}
538542

539543
/**
540544
* Gets the current tool list changed options
541545
*/
542-
public getToolListChangedOptions(): ToolListChangedOptions | null {
546+
public getToolListChangedOptions(): ToolListChangedOptions | undefined {
543547
return this._toolListChangedOptions;
544548
}
545549

0 commit comments

Comments
 (0)