1- import React , { startTransition } from "react"
1+ import React from "react"
22import type { Flow } from "@code0-tech/sagittarius-graphql-types"
33import { useService , useStore } from "@code0-tech/pictor" ;
44import { FunctionService } from "@edition/function/services/Function.service" ;
@@ -7,11 +7,7 @@ import {DatatypeService} from "@edition/datatype/services/Datatype.service";
77import { ValidationResult } from "@core/util/inspection" ;
88import { useFlowValidationAction } from "@edition/flow/components/FlowWorkerProvider" ;
99
10- declare global {
11- interface Window {
12- lastValidatedFlows ?: Record < string , { lastValidated : string , validation : ValidationResult [ ] } > ;
13- }
14- }
10+ const globalPendingValidations = new Map < string , Promise < ValidationResult [ ] > > ( )
1511
1612export const useFlowValidation = (
1713 flowId : Flow [ 'id' ]
@@ -31,40 +27,37 @@ export const useFlowValidation = (
3127 ( ) => flowService . getById ( flowId ) ,
3228 [ flowStore , flowId , flowService ]
3329 )
34-
3530 const functions = React . useMemo ( ( ) => functionService . values ( ) , [ functionStore ] ) ;
3631 const dataTypes = React . useMemo ( ( ) => dataTypeService . values ( ) , [ dataTypeStore ] ) ;
3732
3833 React . useEffect ( ( ) => {
3934 if ( ! flow ) return ;
4035
41- // Skip if already validated for this version
42- if ( flow . editedAt && window . lastValidatedFlows ?. [ flowId as string ] ?. lastValidated === flow . editedAt ) {
43- setValidationResult ( window . lastValidatedFlows ?. [ flowId as string ] . validation ! )
44- return ;
45- }
46-
4736 const timeout = setTimeout ( ( ) => {
48- execute ( {
37+ const key = flowId as string ;
38+
39+ if ( globalPendingValidations . has ( key ) ) {
40+ globalPendingValidations . get ( key ) ! . then ( value => {
41+ setValidationResult ( value as ValidationResult [ ] )
42+ } ) ;
43+ return ;
44+ }
45+
46+ const promise = execute ( {
4947 flow,
5048 functions,
5149 dataTypes
5250 } ) . then ( value => {
53- startTransition ( ( ) => {
54- setValidationResult ( value as ValidationResult [ ] )
55- window . lastValidatedFlows = {
56- ...window . lastValidatedFlows ,
57- [ flowId as string ] : {
58- lastValidated : flow . editedAt ! ,
59- validation : value as ValidationResult [ ]
60- }
61- }
62- } )
51+ setValidationResult ( value as ValidationResult [ ] )
52+ globalPendingValidations . delete ( key ) ;
53+ return value ;
6354 } ) ;
55+
56+ globalPendingValidations . set ( key , promise as Promise < ValidationResult [ ] > ) ;
6457 } , 200 ) ;
6558
6659 return ( ) => clearTimeout ( timeout ) ;
67- } , [ flow , functions , dataTypes , flowStore ] )
60+ } , [ flow ?. editedAt , functions . length , dataTypes . length ] )
6861
6962 return validationResult
7063}
0 commit comments