@@ -463,7 +463,8 @@ const resolveHistoryTarget = Effect.fnUntraced(function*(
463463 } ) ,
464464 actions : completed . actions ,
465465 raisedEvents : completed . raisedEvents ,
466- emittedEvents : completed . emittedEvents
466+ emittedEvents : completed . emittedEvents ,
467+ transitions : [ ]
467468 }
468469 }
469470
@@ -481,7 +482,37 @@ const resolveHistoryTarget = Effect.fnUntraced(function*(
481482 if ( collected . state === undefined || isHistoryTarget ( collected . state ) || ! isSnapshot ( collected . state ) ) {
482483 throw new Error ( `Machine history default for "${ target . path } " must return a complete snapshot containing its owner` )
483484 }
484- const fallbackConfiguration = yield * normalizeConfigurationEffect ( machine , collected . state as any )
485+ const fallbackChoice = choiceFromTarget ( collected . state )
486+ const choiceResolution = fallbackChoice === undefined
487+ ? undefined
488+ : yield * resolveChoiceTarget (
489+ machine ,
490+ {
491+ active : new Set ( ) ,
492+ values : new Map ( ) ,
493+ outputs : new Map ( ) ,
494+ history : configuration . history
495+ } ,
496+ collected . state ,
497+ event
498+ )
499+ let fallbackConfiguration = choiceResolution === undefined
500+ ? yield * normalizeConfigurationEffect ( machine , collected . state as any )
501+ : yield * normalizeTargetConfigurationEffect ( machine , {
502+ active : new Set ( ) ,
503+ values : new Map ( ) ,
504+ outputs : new Map ( ) ,
505+ history : configuration . history
506+ } , choiceResolution . target as any )
507+ for ( const additionalTarget of choiceResolution ?. additionalTargets ?? [ ] ) {
508+ fallbackConfiguration = yield * normalizeTargetConfigurationEffect (
509+ machine ,
510+ fallbackConfiguration ,
511+ additionalTarget as
512+ | Machine . Snapshot < any >
513+ | Machine . Target < any , string >
514+ )
515+ }
485516 if ( ! fallbackConfiguration . active . has ( target . parent ) ) {
486517 throw new Error (
487518 `Machine history default for "${ target . path } " returned a configuration that does not contain owner state "${ target . parent } "`
@@ -494,9 +525,10 @@ const resolveHistoryTarget = Effect.fnUntraced(function*(
494525 snapshot : snapshot as any ,
495526 values : values as any
496527 } ) ,
497- actions : collected . actions ,
498- raisedEvents : collected . raisedEvents ,
499- emittedEvents : collected . emittedEvents
528+ actions : [ ...collected . actions , ...( choiceResolution ?. actions ?? [ ] ) ] ,
529+ raisedEvents : [ ...collected . raisedEvents , ...( choiceResolution ?. raisedEvents ?? [ ] ) ] ,
530+ emittedEvents : [ ...collected . emittedEvents , ...( choiceResolution ?. emittedEvents ?? [ ] ) ] ,
531+ transitions : choiceResolution ?. transitions ?? [ ]
500532 }
501533} )
502534
@@ -1090,6 +1122,14 @@ const withChoiceValues = (target: unknown, values: Readonly<Record<string, unkno
10901122 } )
10911123}
10921124
1125+ interface ResolvedChoiceTransition {
1126+ readonly source : string
1127+ readonly trigger : Machine . TransitionTrigger
1128+ readonly reenter : false
1129+ readonly target : string
1130+ readonly resolvedTarget : string
1131+ }
1132+
10931133const resolveChoiceTarget = Effect . fnUntraced ( function * (
10941134 machine : Machine . Any ,
10951135 configuration : ActiveConfiguration ,
@@ -1101,13 +1141,7 @@ const resolveChoiceTarget = Effect.fnUntraced(function*(
11011141 const actions : Array < DeferredAction > = [ ]
11021142 const raisedEvents : Array < unknown > = [ ]
11031143 const emittedEvents : Array < unknown > = [ ]
1104- const transitions : Array < {
1105- readonly source : string
1106- readonly trigger : Machine . TransitionTrigger
1107- readonly reenter : false
1108- readonly target : string
1109- readonly resolvedTarget : string
1110- } > = [ ]
1144+ const transitions : Array < ResolvedChoiceTransition > = [ ]
11111145 let iterations = 0
11121146
11131147 while ( pending . length > 0 ) {
@@ -1231,6 +1265,7 @@ const collectEvaluatedTransition = Effect.fnUntraced(function*<
12311265 readonly actions : ReadonlyArray < DeferredAction >
12321266 readonly raisedEvents : ReadonlyArray < unknown >
12331267 readonly emittedEvents : ReadonlyArray < unknown >
1268+ readonly transitions : ReadonlyArray < ResolvedChoiceTransition >
12341269 } | undefined
12351270 const reenteredHistoryParent = choiceResolvedTarget !== undefined && isHistoryTarget ( choiceResolvedTarget ) &&
12361271 selection . transition . reenter && state . active . has ( choiceResolvedTarget . parent )
@@ -1269,6 +1304,7 @@ const collectEvaluatedTransition = Effect.fnUntraced(function*<
12691304 const additionalHistoryActions : Array < DeferredAction > = [ ]
12701305 const additionalHistoryRaisedEvents : Array < unknown > = [ ]
12711306 const additionalHistoryEmittedEvents : Array < unknown > = [ ]
1307+ const additionalHistoryChoiceTransitions : Array < ResolvedChoiceTransition > = [ ]
12721308 const additionalChoiceTargets : Array <
12731309 Machine . Snapshot < States > | Machine . Target < States , Machine . StateIdentifier < States > >
12741310 > = [ ]
@@ -1287,6 +1323,7 @@ const collectEvaluatedTransition = Effect.fnUntraced(function*<
12871323 additionalHistoryActions . push ( ...resolved . actions )
12881324 additionalHistoryRaisedEvents . push ( ...resolved . raisedEvents )
12891325 additionalHistoryEmittedEvents . push ( ...resolved . emittedEvents )
1326+ additionalHistoryChoiceTransitions . push ( ...resolved . transitions )
12901327 }
12911328 const targetPath = target === undefined
12921329 ? undefined
@@ -1331,7 +1368,11 @@ const collectEvaluatedTransition = Effect.fnUntraced(function*<
13311368 changed,
13321369 exitPaths : [ ] ,
13331370 entryPaths : [ ] ,
1334- choiceTransitions : choiceResolution ?. transitions ?? [ ]
1371+ choiceTransitions : [
1372+ ...( choiceResolution ?. transitions ?? [ ] ) ,
1373+ ...( historyResolution ?. transitions ?? [ ] ) ,
1374+ ...additionalHistoryChoiceTransitions
1375+ ]
13351376 } as EvaluatedTransition < States , Event , E , R , Context >
13361377 }
13371378
@@ -1380,7 +1421,11 @@ const collectEvaluatedTransition = Effect.fnUntraced(function*<
13801421 )
13811422 )
13821423 : getEntryPaths ( machine , stateAfterTransition , boundary ) ,
1383- choiceTransitions : choiceResolution ?. transitions ?? [ ]
1424+ choiceTransitions : [
1425+ ...( choiceResolution ?. transitions ?? [ ] ) ,
1426+ ...( historyResolution ?. transitions ?? [ ] ) ,
1427+ ...additionalHistoryChoiceTransitions
1428+ ]
13841429 } as EvaluatedTransition < States , Event , E , R , Context >
13851430} )
13861431
@@ -1553,6 +1598,7 @@ export const planInitial: <
15531598 const initialHistoryActions : Array < DeferredAction > = [ ]
15541599 const initialHistoryRaisedEvents : Array < unknown > = [ ]
15551600 const initialHistoryEmittedEvents : Array < unknown > = [ ]
1601+ const initialHistoryChoiceTransitions : Array < ResolvedChoiceTransition > = [ ]
15561602 const resolvedInitialTargets : Array < unknown > = [ ]
15571603 for (
15581604 const target of choiceResolution === undefined
@@ -1568,6 +1614,7 @@ export const planInitial: <
15681614 initialHistoryActions . push ( ...history . actions )
15691615 initialHistoryRaisedEvents . push ( ...history . raisedEvents )
15701616 initialHistoryEmittedEvents . push ( ...history . emittedEvents )
1617+ initialHistoryChoiceTransitions . push ( ...history . transitions )
15711618 }
15721619 let resolvedConfiguration = choiceResolution === undefined
15731620 ? yield * normalizeConfigurationEffect < States > ( machine , state as Machine . Snapshot < States > )
@@ -1585,19 +1632,7 @@ export const planInitial: <
15851632 additionalTarget as Machine . Snapshot < States > | Machine . Target < States , Machine . StateIdentifier < States > >
15861633 )
15871634 }
1588- const configuration : ActiveConfiguration = initialChoice === undefined
1589- ? resolvedConfiguration
1590- : {
1591- ...resolvedConfiguration ,
1592- active : new Set ( [
1593- ...resolvedConfiguration . active ,
1594- ...Object . keys ( initialChoice . values ) . filter ( ( path ) => {
1595- const node = getNode ( machine , path )
1596- return node . type !== "choice" && node . type !== "history"
1597- } )
1598- ] ) ,
1599- values : new Map ( [ ...Object . entries ( initialChoice . values ) , ...resolvedConfiguration . values ] )
1600- }
1635+ const configuration : ActiveConfiguration = resolvedConfiguration
16011636 validateInitialConfiguration ( machine , configuration )
16021637 const startingState = snapshotFromConfiguration < States > ( machine , configuration )
16031638 const initialEntryPaths = getInitialEntryPaths ( machine , configuration )
@@ -1634,7 +1669,7 @@ export const planInitial: <
16341669 choiceResolution === undefined ? [ ] : [ {
16351670 next : configuration ,
16361671 event : InitialEvent ,
1637- transitions : choiceResolution . transitions ,
1672+ transitions : [ ... choiceResolution . transitions , ... initialHistoryChoiceTransitions ] ,
16381673 actions : [ ...choiceResolution . actions , ...initialHistoryActions ] as ReadonlyArray < Effect . Effect < void , E , R > > ,
16391674 raisedEvents : [
16401675 ...choiceResolution . raisedEvents ,
0 commit comments