@@ -260,11 +260,15 @@ export const applyEvent = async (event, socket) => {
260260 try {
261261 const eval_result = event . payload . function ( ) ;
262262 if ( event . payload . callback ) {
263- if ( ! ! eval_result && typeof eval_result . then === "function" ) {
264- event . payload . callback ( await eval_result ) ;
265- } else {
266- event . payload . callback ( eval_result ) ;
267- }
263+ const final_result =
264+ ! ! eval_result && typeof eval_result . then === "function"
265+ ? await eval_result
266+ : eval_result ;
267+ const callback =
268+ typeof event . payload . callback === "string"
269+ ? eval ( event . payload . callback )
270+ : event . payload . callback ;
271+ callback ( final_result ) ;
268272 }
269273 } catch ( e ) {
270274 console . log ( "_call_function" , e ) ;
@@ -283,11 +287,15 @@ export const applyEvent = async (event, socket) => {
283287 : eval ( event . payload . function ) ( ) ;
284288
285289 if ( event . payload . callback ) {
286- if ( ! ! eval_result && typeof eval_result . then === "function" ) {
287- eval ( event . payload . callback ) ( await eval_result ) ;
288- } else {
289- eval ( event . payload . callback ) ( eval_result ) ;
290- }
290+ const final_result =
291+ ! ! eval_result && typeof eval_result . then === "function"
292+ ? await eval_result
293+ : eval_result ;
294+ const callback =
295+ typeof event . payload . callback === "string"
296+ ? eval ( event . payload . callback )
297+ : event . payload . callback ;
298+ callback ( final_result ) ;
291299 }
292300 } catch ( e ) {
293301 console . log ( "_call_script" , e ) ;
@@ -364,7 +372,7 @@ export const queueEvents = async (events, socket, prepend) => {
364372 ) ,
365373 ] ;
366374 }
367- event_queue . push ( ...events ) ;
375+ event_queue . push ( ...events . filter ( ( e ) => e !== undefined && e !== null ) ) ;
368376 await processEvent ( socket . current ) ;
369377} ;
370378
@@ -750,11 +758,13 @@ export const useEventLoop = (
750758
751759 // Function to add new events to the event queue.
752760 const addEvents = ( events , args , event_actions ) => {
761+ const _events = events . filter ( ( e ) => e !== undefined && e !== null ) ;
762+
753763 if ( ! ( args instanceof Array ) ) {
754764 args = [ args ] ;
755765 }
756766
757- event_actions = events . reduce (
767+ event_actions = _events . reduce (
758768 ( acc , e ) => ( { ...acc , ...e . event_actions } ) ,
759769 event_actions ?? { } ,
760770 ) ;
@@ -767,7 +777,7 @@ export const useEventLoop = (
767777 if ( event_actions ?. stopPropagation && _e ?. stopPropagation ) {
768778 _e . stopPropagation ( ) ;
769779 }
770- const combined_name = events . map ( ( e ) => e . name ) . join ( "+++" ) ;
780+ const combined_name = _events . map ( ( e ) => e . name ) . join ( "+++" ) ;
771781 if ( event_actions ?. temporal ) {
772782 if ( ! socket . current || ! socket . current . connected ) {
773783 return ; // don't queue when the backend is not connected
@@ -783,11 +793,11 @@ export const useEventLoop = (
783793 // If debounce is used, queue the events after some delay
784794 debounce (
785795 combined_name ,
786- ( ) => queueEvents ( events , socket ) ,
796+ ( ) => queueEvents ( _events , socket ) ,
787797 event_actions . debounce ,
788798 ) ;
789799 } else {
790- queueEvents ( events , socket ) ;
800+ queueEvents ( _events , socket ) ;
791801 }
792802 } ;
793803
0 commit comments