@@ -45,46 +45,39 @@ export const ChatKit = React.forwardRef<OpenAIChatKit, ChatKitProps>(
4545 return (
4646 < openai-chatkit
4747 ref = { ( chatKit ) => {
48- ref . current = chatKit ;
4948 control . setInstance ( chatKit ) ;
49+
5050 if ( typeof forwardedRef === 'function' ) {
5151 forwardedRef ( chatKit ) ;
5252 } else if ( forwardedRef ) {
5353 forwardedRef . current = chatKit ;
5454 }
5555
56- if ( ref . current ) {
57- const abortController = new AbortController ( ) ;
58- const events = {
59- 'chatkit.error' : 'onError' ,
60- 'chatkit.response.end' : 'onResponseEnd' ,
61- 'chatkit.response.start' : 'onResponseStart' ,
62- 'chatkit.log' : 'onLog' ,
63- 'chatkit.thread.change' : 'onThreadChange' ,
64- 'chatkit.thread.load.start' : 'onThreadLoadStart' ,
65- 'chatkit.thread.load.end' : 'onThreadLoadEnd' ,
66- } satisfies {
67- [ K in keyof ChatKitEvents ] : ToEventHandlerKey < K > ;
68- } ;
56+ if ( ! ref . current ) {
57+ return ;
58+ }
59+
60+ const events : {
61+ [ K in keyof ChatKitEvents ] : ToEventHandlerKey < K > ;
62+ } = {
63+ 'chatkit.error' : 'onError' ,
64+ 'chatkit.response.end' : 'onResponseEnd' ,
65+ 'chatkit.response.start' : 'onResponseStart' ,
66+ 'chatkit.log' : 'onLog' ,
67+ 'chatkit.thread.change' : 'onThreadChange' ,
68+ 'chatkit.thread.load.start' : 'onThreadLoadStart' ,
69+ 'chatkit.thread.load.end' : 'onThreadLoadEnd' ,
70+ } ;
6971
70- for ( const eventName of Object . keys (
71- events ,
72- ) as ( keyof ChatKitEvents ) [ ] ) {
73- ref . current . addEventListener (
74- eventName ,
75- ( e ) => {
76- const handler = control . handlers [ events [ eventName ] ] ;
77- if ( typeof handler === 'function' ) {
78- handler ( e . detail as any ) ;
79- }
80- } ,
81- { signal : abortController . signal } ,
82- ) ;
83- }
72+ const eventNames = Object . keys ( events ) as ( keyof ChatKitEvents ) [ ] ;
8473
85- return ( ) => {
86- abortController . abort ( ) ;
87- } ;
74+ for ( const event of eventNames ) {
75+ ref . current . addEventListener ( event , ( e ) => {
76+ const handler = control . handlers [ events [ event ] ] ;
77+ if ( typeof handler === 'function' ) {
78+ handler ( e . detail as any ) ;
79+ }
80+ } ) ;
8881 }
8982 } }
9083 { ...htmlProps }
0 commit comments