55import * as Helpers from '../helpers/helpers.js' ;
66import * as Types from '../types/types.js' ;
77
8+ import { data as flowsHandlerData } from './FlowsHandler.js' ;
9+
810const lastScheduleStyleRecalcByFrame = new Map < string , Types . Events . ScheduleStyleRecalculation > ( ) ;
911
1012// This tracks the last event that is considered to have invalidated the layout
@@ -20,10 +22,6 @@ const lastInvalidationEventForFrame = new Map<string, Types.Events.Event>();
2022// is called.
2123const lastUpdateLayoutTreeByFrame = new Map < string , Types . Events . UpdateLayoutTree > ( ) ;
2224
23- // This tracks postmessage dispatch and handler events for creating initiator association
24- const postMessageHandlerEvents : Types . Events . HandlePostMessage [ ] = [ ] ;
25- const schedulePostMessageEventByTraceId : Map < string , Types . Events . SchedulePostMessage > = new Map ( ) ;
26-
2725// These two maps store the same data but in different directions.
2826// For a given event, tell me what its initiator was. An event can only have one initiator.
2927const eventToInitiatorMap = new Map < Types . Events . Event , Types . Events . Event > ( ) ;
@@ -48,8 +46,6 @@ export function reset(): void {
4846 requestIdleCallbackEventsById . clear ( ) ;
4947 webSocketCreateEventsById . clear ( ) ;
5048 schedulePostTaskCallbackEventsById . clear ( ) ;
51- schedulePostMessageEventByTraceId . clear ( ) ;
52- postMessageHandlerEvents . length = 0 ;
5349}
5450
5551function storeInitiator ( data : { initiator : Types . Events . Event , event : Types . Events . Event } ) : void {
@@ -164,34 +160,20 @@ export function handleEvent(event: Types.Events.Event): void {
164160 storeInitiator ( { event, initiator : matchingSchedule } ) ;
165161 }
166162 }
167- // Store schedulePostMessage Events by their traceIds.
168- // so they can be reconciled later with matching handlePostMessage events with same traceIds.
169- else if ( Types . Events . isHandlePostMessage ( event ) ) {
170- postMessageHandlerEvents . push ( event ) ;
171- } else if ( Types . Events . isSchedulePostMessage ( event ) ) {
172- const traceId = event . args . data ?. traceId ;
173- if ( traceId ) {
174- schedulePostMessageEventByTraceId . set ( traceId , event ) ;
175- }
176- }
177163}
178164
179- function finalizeInitiatorRelationship ( ) : void {
180- for ( const handlerEvent of postMessageHandlerEvents ) {
181- const traceId = handlerEvent . args . data ?. traceId ;
182- const matchingSchedulePostMesssageEvent = schedulePostMessageEventByTraceId . get ( traceId ) ;
183- if ( matchingSchedulePostMesssageEvent ) {
184- // Set schedulePostMesssage events as initiators for handler events.
185- storeInitiator ( { event : handlerEvent , initiator : matchingSchedulePostMesssageEvent } ) ;
165+ function createRelationshipsFromFlows ( ) : void {
166+ const flows = flowsHandlerData ( ) . flows ;
167+ for ( let i = 0 ; i < flows . length ; i ++ ) {
168+ const flow = flows [ i ] ;
169+ for ( let j = 0 ; j < flow . length - 1 ; j ++ ) {
170+ storeInitiator ( { event : flow [ j + 1 ] , initiator : flow [ j ] } ) ;
186171 }
187172 }
188173}
189174
190175export async function finalize ( ) : Promise < void > {
191- // During event processing, we may encounter initiators before the handler events themselves
192- // (e.g dispatch events on worker and handler events on the main thread)
193- // we don't want to miss out on events whose initiators haven't been processed yet
194- finalizeInitiatorRelationship ( ) ;
176+ createRelationshipsFromFlows ( ) ;
195177}
196178
197179export interface InitiatorsData {
@@ -205,3 +187,7 @@ export function data(): InitiatorsData {
205187 initiatorToEvents : initiatorToEventsMap ,
206188 } ;
207189}
190+
191+ export function deps ( ) : [ 'FlowsHandler' ] {
192+ return [ 'FlowsHandler' ] ;
193+ }
0 commit comments