Skip to content

Commit 371a9bc

Browse files
optimise event handler control
1 parent c2680f3 commit 371a9bc

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

client/packages/lowcoder/src/comps/controls/eventHandlerControl.tsx

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
QueryConfigItemWrapper,
2121
ValueFromOption,
2222
} from "lowcoder-design";
23-
import { Fragment, ReactNode, useContext, useEffect, useState} from "react";
23+
import { Fragment, ReactNode, useContext, useEffect, useState, useRef, useCallback } from "react";
2424
import { memo } from "util/cacheUtils";
2525
import { EditorContext } from "../editorState";
2626
import { ActionSelectorControl } from "./actionSelector/actionSelectorControl";
@@ -59,7 +59,12 @@ class SingleEventHandlerControl<
5959
return;
6060
}
6161
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+
}
6368
}
6469
};
6570
}
@@ -142,10 +147,9 @@ const EventHandlerControlPropertyView = (props: {
142147
type?: "query";
143148
eventConfigs: EventConfigsType;
144149
}) => {
145-
146-
147150
const editorState = useContext(EditorContext);
148151
const [showNewCreate, setShowNewCreate] = useState(false);
152+
const mountedRef = useRef(true);
149153

150154
const {
151155
dispatch,
@@ -157,14 +161,27 @@ const EventHandlerControlPropertyView = (props: {
157161
type
158162
} = props;
159163

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]);
161178

162179
const queryHandler = {
163180
name: eventConfigs[0].value,
164181
};
165182

166-
const handleAdd = () => {
167-
if (eventConfigs.length === 0) {
183+
const handleAdd = useCallback(() => {
184+
if (eventConfigs.length === 0 || !mountedRef.current) {
168185
return;
169186
}
170187

@@ -190,8 +207,10 @@ const EventHandlerControlPropertyView = (props: {
190207
handler: isInDevIde ? messageHandler : queryExecHandler,
191208
} as const;
192209
dispatch(pushAction(type !== "query" ? newHandler : queryHandler));
193-
setShowNewCreate(true);
194-
};
210+
if (mountedRef.current) {
211+
setShowNewCreate(true);
212+
}
213+
}, [dispatch, eventConfigs, editorState, pushAction, type]);
195214

196215
const renderItems = () =>
197216
items.length > 0 ? (
@@ -251,7 +270,12 @@ class EventHandlerControl<T extends EventConfigsType> extends list(SingleEventHa
251270
super.getView().forEach((child) => {
252271
const ret = child.getView()(eventName);
253272
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+
);
255279
}
256280
});
257281
return Promise.all(list);

0 commit comments

Comments
 (0)