66 */
77
88import { isServerPlatform } from '../platform/platform' ;
9- import { isPromise } from '../utils/promises' ;
109import type { VNode } from '../vnode/vnode' ;
11- import { executeChoreSequence } from './chore-execution' ;
10+ import {
11+ executeCleanup ,
12+ executeComponentChore ,
13+ executeCompute ,
14+ executeNodeDiff ,
15+ executeNodeProps ,
16+ executeTasks ,
17+ } from './chore-execution' ;
1218import type { Cursor } from './cursor' ;
1319import {
1420 getCursorPosition ,
1521 setCursorPosition ,
16- getCursorPromise ,
17- setCursorPromise ,
22+ getVNodePromise ,
23+ setVNodePromise ,
1824} from './cursor-props' ;
1925import { pauseCursor } from './cursor' ;
2026import { ChoreBits } from '../vnode/enums/chore-bits.enum' ;
@@ -25,6 +31,8 @@ import { createNextTick } from '../platform/next-tick';
2531import { vnode_isElementVNode } from '../../client/vnode' ;
2632import type { Container } from '../types' ;
2733import { VNodeFlags } from '../../client/types' ;
34+ import { isPromise } from '../utils/promises' ;
35+ import type { ValueOrPromise } from '../utils/types' ;
2836
2937export const dirtyChildrenStack : { parent : VNode ; index : number } [ ] = [ ] ;
3038
@@ -114,7 +122,7 @@ export function walkCursor(cursor: Cursor, options: WalkOptions): void {
114122 }
115123
116124 // Check if cursor is blocked by a promise
117- const blockingPromise = getCursorPromise ( cursor ) ;
125+ const blockingPromise = getVNodePromise ( cursor ) ;
118126 if ( blockingPromise ) {
119127 return ;
120128 }
@@ -123,7 +131,11 @@ export function walkCursor(cursor: Cursor, options: WalkOptions): void {
123131 // Get starting position (resume from last position or start at root)
124132 let currentVNode : VNode | null = null ;
125133
134+ let count = 0 ;
126135 while ( ( currentVNode = getCursorPosition ( cursor ) ) ) {
136+ if ( count ++ > 500000 ) {
137+ throw new Error ( 'Infinite loop detected in cursor walker' ) ;
138+ }
127139 // Check time budget (only for DOM, not SSR)
128140 // if (!isServer) {
129141 // const elapsed = performance.now() - startTime;
@@ -136,35 +148,57 @@ export function walkCursor(cursor: Cursor, options: WalkOptions): void {
136148 // }
137149
138150 // Skip if the vNode is not dirty
139- if ( ! currentVNode . dirty ) {
151+ if ( ! ( currentVNode . dirty & ChoreBits . DIRTY_MASK ) || getVNodePromise ( currentVNode ) ) {
140152 // Move to next node
141153 setCursorPosition ( cursor , getNextVNode ( ) ) ;
142154 continue ;
143155 }
144156
145- const result = executeChoreSequence ( currentVNode , container , cursor ) ;
157+ let result : ValueOrPromise < void > | undefined ;
158+ console . log ( 'executeChoreSequence' , currentVNode . dirty ) ;
159+ // Execute chores in order
160+ if ( currentVNode . dirty & ChoreBits . TASKS ) {
161+ result = executeTasks ( currentVNode , container , cursor ) ;
162+ } else if ( currentVNode . dirty & ChoreBits . NODE_DIFF ) {
163+ result = executeNodeDiff ( currentVNode , container ) ;
164+ } else if ( currentVNode . dirty & ChoreBits . COMPONENT ) {
165+ result = executeComponentChore ( currentVNode , container ) ;
166+ } else if ( currentVNode . dirty & ChoreBits . NODE_PROPS ) {
167+ executeNodeProps ( currentVNode , container ) ;
168+ } else if ( currentVNode . dirty & ChoreBits . COMPUTE ) {
169+ result = executeCompute ( currentVNode , container ) ;
170+ } else if ( currentVNode . dirty & ChoreBits . CHILDREN ) {
171+ const dirtyChildren = currentVNode . dirtyChildren ;
172+ if ( ! dirtyChildren || dirtyChildren . length === 0 ) {
173+ // No dirty children
174+ currentVNode . dirty &= ~ ChoreBits . CHILDREN ;
175+ } else {
176+ dirtyChildrenStack . push ( { parent : currentVNode , index : 0 } ) ;
177+ // descend
178+ setCursorPosition ( cursor , getNextVNode ( ) ) ;
179+ }
180+ } else if ( currentVNode . dirty & ChoreBits . CLEANUP ) {
181+ executeCleanup ( currentVNode , container ) ;
182+ }
146183
147184 // Handle blocking promise
148- // if (isPromise(result)) {
149- // // Store promise on cursor and pause
150- // setCursorPromise(cursor, result);
151- // pauseCursor(cursor, currentVNode);
152-
153- // result
154- // .then(() => {
155- // setCursorPromise(cursor, null);
156- // })
157- // .catch((error) => {
158- // setCursorPromise(cursor, null);
159- // container.handleError(error, currentVNode);
160- // })
161- // .finally(() => {
162- // triggerCursors();
163- // });
164- // return;
165- // }
185+ if ( result && isPromise ( result ) ) {
186+ // Store promise on cursor and pause
187+ setVNodePromise ( cursor , result ) ;
188+ // pauseCursor(cursor, currentVNode);
166189
167- startTime = performance . now ( ) ;
190+ result
191+ . then ( ( ) => {
192+ setVNodePromise ( cursor , null ) ;
193+ } )
194+ . catch ( ( error ) => {
195+ setVNodePromise ( cursor , null ) ;
196+ container . handleError ( error , currentVNode ) ;
197+ } )
198+ . finally ( ( ) => {
199+ triggerCursors ( ) ;
200+ } ) ;
201+ }
168202 }
169203 if ( ! ( cursor . dirty & ChoreBits . DIRTY_MASK ) ) {
170204 cursor . flags &= ~ VNodeFlags . Cursor ;
0 commit comments