@@ -20,7 +20,7 @@ import {
20
20
QueryConfigItemWrapper ,
21
21
ValueFromOption ,
22
22
} from "lowcoder-design" ;
23
- import { Fragment , ReactNode , useContext , useEffect , useState } from "react" ;
23
+ import { Fragment , ReactNode , useContext , useEffect , useState , useRef , useCallback } from "react" ;
24
24
import { memo } from "util/cacheUtils" ;
25
25
import { EditorContext } from "../editorState" ;
26
26
import { ActionSelectorControl } from "./actionSelector/actionSelectorControl" ;
@@ -59,7 +59,12 @@ class SingleEventHandlerControl<
59
59
return ;
60
60
}
61
61
if ( handler ) {
62
- return handler ( ) ;
62
+ try {
63
+ return handler ( ) ;
64
+ } catch ( error ) {
65
+ console . error ( 'Error in event handler:' , error ) ;
66
+ return Promise . reject ( error ) ;
67
+ }
63
68
}
64
69
} ;
65
70
}
@@ -142,10 +147,9 @@ const EventHandlerControlPropertyView = (props: {
142
147
type ?: "query" ;
143
148
eventConfigs : EventConfigsType ;
144
149
} ) => {
145
-
146
-
147
150
const editorState = useContext ( EditorContext ) ;
148
151
const [ showNewCreate , setShowNewCreate ] = useState ( false ) ;
152
+ const mountedRef = useRef ( true ) ;
149
153
150
154
const {
151
155
dispatch,
@@ -157,14 +161,27 @@ const EventHandlerControlPropertyView = (props: {
157
161
type
158
162
} = props ;
159
163
160
- useEffect ( ( ) => setShowNewCreate ( false ) , [ dispatch ] ) ;
164
+ // Reset state on unmount
165
+ useEffect ( ( ) => {
166
+ return ( ) => {
167
+ mountedRef . current = false ;
168
+ setShowNewCreate ( false ) ;
169
+ } ;
170
+ } , [ ] ) ;
171
+
172
+ // Reset showNewCreate when dispatch changes
173
+ useEffect ( ( ) => {
174
+ if ( mountedRef . current ) {
175
+ setShowNewCreate ( false ) ;
176
+ }
177
+ } , [ dispatch ] ) ;
161
178
162
179
const queryHandler = {
163
180
name : eventConfigs [ 0 ] . value ,
164
181
} ;
165
182
166
- const handleAdd = ( ) => {
167
- if ( eventConfigs . length === 0 ) {
183
+ const handleAdd = useCallback ( ( ) => {
184
+ if ( eventConfigs . length === 0 || ! mountedRef . current ) {
168
185
return ;
169
186
}
170
187
@@ -190,8 +207,10 @@ const EventHandlerControlPropertyView = (props: {
190
207
handler : isInDevIde ? messageHandler : queryExecHandler ,
191
208
} as const ;
192
209
dispatch ( pushAction ( type !== "query" ? newHandler : queryHandler ) ) ;
193
- setShowNewCreate ( true ) ;
194
- } ;
210
+ if ( mountedRef . current ) {
211
+ setShowNewCreate ( true ) ;
212
+ }
213
+ } , [ dispatch , eventConfigs , editorState , pushAction , type ] ) ;
195
214
196
215
const renderItems = ( ) =>
197
216
items . length > 0 ? (
@@ -251,7 +270,12 @@ class EventHandlerControl<T extends EventConfigsType> extends list(SingleEventHa
251
270
super . getView ( ) . forEach ( ( child ) => {
252
271
const ret = child . getView ( ) ( eventName ) ;
253
272
if ( ret ) {
254
- list . push ( ret ) ;
273
+ list . push (
274
+ Promise . resolve ( ret ) . catch ( error => {
275
+ console . error ( 'Error in event handler:' , error ) ;
276
+ return Promise . reject ( error ) ;
277
+ } )
278
+ ) ;
255
279
}
256
280
} ) ;
257
281
return Promise . all ( list ) ;
0 commit comments