8
8
import { debounce } from "lodash" ;
9
9
import log from "loglevel" ;
10
10
import { CompAction , CompActionTypes , CompConstructor } from "lowcoder-core" ;
11
- import { useEffect , useMemo , useRef , useState } from "react" ;
11
+ import { useEffect , useMemo , useState } from "react" ;
12
12
import { PriorityQueue , Queue } from "typescript-collections" ;
13
13
import { JSONValue } from "util/jsonTypes" ;
14
14
import {
@@ -78,14 +78,7 @@ export function actionHandlerGenerator() {
78
78
ratio : ( ( 1 - statsData . groups / statsData . total ) * 100 ) . toFixed ( 2 ) + "%" ,
79
79
} ;
80
80
} ;
81
-
82
- const cleanup = ( ) => {
83
- while ( deferQueue . size ( ) > 0 ) {
84
- deferQueue . dequeue ( ) ;
85
- }
86
- } ;
87
-
88
- return [ actionHandler , stats , cleanup ] as const ;
81
+ return [ actionHandler , stats ] as const ;
89
82
}
90
83
91
84
// Reduce the nested dispatch in call order, then evaluate to update comp once.
@@ -99,8 +92,8 @@ export function nestDispatchHandlerGenerator() {
99
92
const diff = a . depth - b . depth ;
100
93
return diff !== 0 ? diff : b . seq - a . seq ;
101
94
} ) ;
102
-
103
- const dispatchHandler = ( action : CompAction , reduceFn : ( action : CompAction ) => void ) => {
95
+ return ( action : CompAction , reduceFn : ( action : CompAction ) => void ) => {
96
+ // Find the nested dispatch action, add it to the queue, and execute reduce later
104
97
if ( depth > 0 ) {
105
98
++ seq ;
106
99
queue . enqueue ( { action, depth, seq } ) ;
@@ -118,14 +111,6 @@ export function nestDispatchHandlerGenerator() {
118
111
depth = 0 ;
119
112
seq = 0 ;
120
113
} ;
121
-
122
- const cleanup = ( ) => {
123
- while ( queue . size ( ) > 0 ) {
124
- queue . dequeue ( ) ;
125
- }
126
- } ;
127
-
128
- return [ dispatchHandler , cleanup ] as const ;
129
114
}
130
115
131
116
type CompContainerChangeHandler = ( actions ?: CompAction [ ] ) => void ;
@@ -161,8 +146,8 @@ export function getCompContainer<T extends CompConstructor>(params: GetContainer
161
146
if ( ! initialValue || ! isReady ) {
162
147
return null ;
163
148
}
164
- const [ actionHandler , stats , cleanupActionHandler ] = actionHandlerGenerator ( ) ;
165
- const [ nestDispatchHandler , cleanupNestDispatch ] = nestDispatchHandlerGenerator ( ) ;
149
+ const [ actionHandler , stats ] = actionHandlerGenerator ( ) ;
150
+ const nestDispatchHandler = nestDispatchHandlerGenerator ( ) ;
166
151
167
152
class CompContainer {
168
153
comp : InstanceType < T > ;
@@ -173,10 +158,6 @@ export function getCompContainer<T extends CompConstructor>(params: GetContainer
173
158
initializing : boolean = false ;
174
159
175
160
private changeListeners : CompContainerChangeHandler [ ] = [ ] ;
176
- private clearQueueTimerHandle = 0 ;
177
- private appCalmDownTimerHandle = 0 ;
178
- private appCalmDowned = false ;
179
- private idleCallbackId : number | null = null ;
180
161
181
162
constructor ( ) {
182
163
this . dispatch = this . dispatch . bind ( this ) ;
@@ -201,6 +182,10 @@ export function getCompContainer<T extends CompConstructor>(params: GetContainer
201
182
return this . comp ;
202
183
}
203
184
185
+ clearQueueTimerHandle = 0 ;
186
+ appCalmDownTimerHandle = 0 ;
187
+ appCalmDowned = false ;
188
+
204
189
dispatch ( action ?: CompAction ) {
205
190
if ( ! this . initialized ) {
206
191
throw new Error ( "comp container is not initialized" ) ;
@@ -291,29 +276,6 @@ export function getCompContainer<T extends CompConstructor>(params: GetContainer
291
276
// FIXME: Check it out, why does it take 30ms to change the code editor, and only 1ms for others
292
277
showCost ( "setComp" , ( ) => this . changeListeners . forEach ( ( x ) => x ( actions ) ) ) ;
293
278
}
294
-
295
- cleanup ( ) {
296
- // Clear timers
297
- if ( this . clearQueueTimerHandle ) {
298
- clearTimeout ( this . clearQueueTimerHandle ) ;
299
- }
300
- if ( this . appCalmDownTimerHandle ) {
301
- clearTimeout ( this . appCalmDownTimerHandle ) ;
302
- }
303
- if ( this . idleCallbackId !== null ) {
304
- cancelIdleCallback ( this . idleCallbackId ) ;
305
- }
306
-
307
- // Clear queues
308
- cleanupActionHandler ( ) ;
309
- cleanupNestDispatch ( ) ;
310
-
311
- // Clear listeners
312
- this . changeListeners = [ ] ;
313
-
314
- // Clear comp reference
315
- this . comp = null as any ;
316
- }
317
279
}
318
280
319
281
return new CompContainer ( ) ;
@@ -330,42 +292,28 @@ export function useCompInstance<T extends CompConstructor>(
330
292
) {
331
293
const [ comp , setComp ] = useState < InstanceType < T > | null > ( null ) ;
332
294
const container = useCompContainer ( params ) ;
333
- const mountedRef = useRef ( true ) ;
334
295
335
- useEffect ( ( ) => {
336
- return ( ) => {
337
- mountedRef . current = false ;
338
- } ;
339
- } , [ ] ) ;
296
+ if ( container && ! container . initialized ) {
297
+ container . init ( ) . then ( setComp ) ;
298
+ }
340
299
341
300
useEffect ( ( ) => {
342
301
if ( ! container ) {
343
302
return ( ) => { } ;
344
303
}
345
304
346
- if ( ! container . initialized ) {
347
- container . init ( ) . then ( ( comp ) => {
348
- if ( mountedRef . current ) {
349
- setComp ( comp ) ;
350
- }
351
- } ) ;
352
- }
305
+ let updateHandler = ( ) => setComp ( container . comp ) ;
353
306
354
- const updateHandler = debounce ( ( ) => {
355
- if ( mountedRef . current ) {
307
+ // if (UPDATE_ROOT_VIEW_DEBOUNCE > 0) {
308
+ updateHandler = debounce ( ( ) => {
356
309
setComp ( container . comp ) ;
357
- }
358
- } , UPDATE_ROOT_VIEW_DEBOUNCE || 50 ) ;
310
+ } , 50 /* UPDATE_ROOT_VIEW_DEBOUNCE */ ) ;
311
+ // }
359
312
360
313
const finalHandlers = [ ...( handlers || [ ] ) , updateHandler ] ;
361
314
finalHandlers . forEach ( ( handler ) => container . addChangeListener ( handler ) ) ;
362
-
363
315
return ( ) => {
364
316
finalHandlers . forEach ( ( handler ) => container . removeChangeListener ( handler ) ) ;
365
- updateHandler . cancel ( ) ;
366
- if ( container ) {
367
- container . cleanup ( ) ;
368
- }
369
317
} ;
370
318
} , [ container , handlers ] ) ;
371
319
0 commit comments