@@ -23,9 +23,11 @@ export type NodeRegistry = Map<string, { node: ViewTreeNode; parentId?: string }
2323export type SwiftUIContextValue = {
2424 getEventHandler : ( id : string , name : string ) => EventHandler | undefined ;
2525 nodesKey : string ;
26+ renderSequenceKey : string ;
2627 getNodes : ( ) => NodeRegistry ;
2728 nativeRef : RefObject < React . ComponentRef < typeof SwiftUIRootNativeComponent > | null > ;
2829 recordRenderOrder : ( id : string ) => void ;
30+ commitRenderSequence : ( ) => void ;
2931 registerEvents : ( id : string , events : Record < string , EventHandler | undefined > ) => void ;
3032 registerEvent : ( id : string , name : string , handler : EventHandler ) => void ;
3133 registerNode : ( node : ViewTreeNode , parentId : string ) => void ;
@@ -49,7 +51,9 @@ export const SwiftUIProvider: FunctionComponent<PropsWithChildren<SwiftUIProvide
4951 const eventRegistry = useRef < EventRegistry > ( new Map ( ) ) ;
5052 const nodeRegistry = useRef < NodeRegistry > ( new Map ( ) ) ;
5153 const [ nodeRegistryVersion , setNodeRegistryVersion ] = useState ( 0 ) ;
54+ const [ renderSequenceVersion , setRenderSequenceVersion ] = useState ( 0 ) ;
5255 const renderSequence = useRef < string [ ] > ( [ ] ) ;
56+ const previousRenderSequence = useRef < string [ ] > ( [ ] ) ;
5357 const nativeRef = useRef < React . ComponentRef < typeof SwiftUIRootNativeComponent > | null > ( null ) ;
5458
5559 const log = useCallback (
@@ -67,6 +71,11 @@ export const SwiftUIProvider: FunctionComponent<PropsWithChildren<SwiftUIProvide
6771 // eslint-disable-next-line react-hooks/exhaustive-deps
6872 } , [ nodeRegistryVersion ] ) ;
6973
74+ const renderSequenceKey = useMemo ( ( ) => {
75+ return JSON . stringify ( renderSequence . current ) ;
76+ // eslint-disable-next-line react-hooks/exhaustive-deps
77+ } , [ renderSequenceVersion ] ) ;
78+
7079 const getEventHandler = ( id : string , name : string ) => {
7180 return eventRegistry . current . get ( id ) ?. get ( name ) ;
7281 } ;
@@ -127,8 +136,23 @@ export const SwiftUIProvider: FunctionComponent<PropsWithChildren<SwiftUIProvide
127136 const getNodes = useCallback ( ( ) => nodeRegistry . current , [ ] ) ;
128137
129138 const recordRenderOrder = useCallback ( ( id : string ) => {
130- if ( ! renderSequence . current . includes ( id ) ) {
131- renderSequence . current . push ( id ) ;
139+ // During render, just append to the sequence - don't trigger state updates
140+ // The sequence will be reset before each render pass in SwiftUIRoot
141+ renderSequence . current . push ( id ) ;
142+ } , [ ] ) ;
143+
144+ const commitRenderSequence = useCallback ( ( ) => {
145+ // After render, check if order actually changed and only then bump version
146+ const currentSequence = renderSequence . current ;
147+ const prevSequence = previousRenderSequence . current ;
148+
149+ // Compare sequences - only update if they differ
150+ if (
151+ currentSequence . length !== prevSequence . length ||
152+ currentSequence . some ( ( id , index ) => id !== prevSequence [ index ] )
153+ ) {
154+ previousRenderSequence . current = [ ...currentSequence ] ;
155+ setRenderSequenceVersion ( ( prev ) => prev + 1 ) ;
132156 }
133157 } , [ ] ) ;
134158
@@ -153,9 +177,11 @@ export const SwiftUIProvider: FunctionComponent<PropsWithChildren<SwiftUIProvide
153177 const context = {
154178 getEventHandler,
155179 nodesKey,
180+ renderSequenceKey,
156181 getNodes,
157182 nativeRef,
158183 recordRenderOrder,
184+ commitRenderSequence,
159185 registerEvents,
160186 registerEvent,
161187 registerNode,
0 commit comments