8a8cfa32
#1704 Thanks @blimmer! - The defaultclock
methods (setTimeout
andclearTimeout
) are now invoked properly with the global context preserved for those invocations which matter for some JS environments. More details can be found in the corresponding issue: #1703.
-
6596d0ba
#1622 Thanks @davidkpiano! - Spawned/invoked actors and interpreters are now typed as extendingActorRef
(e.g.,SpawnedActorRef
) rather thanActor
orInterpreter
. This unification of types should make it more straightforward to provide actor types:import { - Actor + ActorRef } from 'xstate'; // ... interface SomeContext { - server?: Actor; + server?: ActorRef<ServerEvent>; }
It's also easier to specify the type of a spawned/invoked machine with
ActorRefFrom
:import { createMachine, - Actor + ActorRefFrom } from 'xstate'; const serverMachine = createMachine<ServerContext, ServerEvent>({ // ... }); interface SomeContext { - server?: Actor; // difficult to type + server?: ActorRefFrom<typeof serverMachine>; }
75a91b07
#1692 Thanks @Andarist! - Fixed an issue with history state entering a wrong state if the most recent visit in its parent has been caused by a transient transition.
02c76350
#1656 Thanks @Andarist! - Exit actions will now be properly called when a service gets canceled by calling itsstop
method.
-
119db8fb
#1577 Thanks @davidkpiano! - Expressions can now be used in thestop()
action creator:// ... actions: stop(context => context.someActor);
8c78e120
#1570 Thanks @davidkpiano! - The return type ofspawn(machine)
will now beActor<State<TContext, TEvent>, TEvent>
, which is a supertype ofInterpreter<...>
.
602687c2
#1566 Thanks @davidkpiano! - Exit actions will now be properly called when an invoked machine reaches its final state. See #1109 for more details.
6e44d02a
#1553 Thanks @davidkpiano! - Thestate.children
property now properly shows all spawned and invoked actors. See #795 for more details.
72b0880e
#1504 Thanks @Andarist! - Addedstatus
property on theInterpreter
- this can be used to differentiate not started, running and stopped interpreters. This property is best compared to values on the newInterpreterStatus
export.
f51614df
#1409 Thanks @jirutka! - Fix typeExtractStateValue
so that it generates a type actually describing aState.value
b1684ead
#1402 Thanks @Andarist! - Improved TypeScript type-checking performance a little bit by using distributive conditional type withinTransitionsConfigArray
declarations instead of a mapped type. Kudos to @amcasey, some discussion around this can be found here
ad3026d4
#1407 Thanks @tomenden! - Fixed an issue with not being able to run XState in Web Workers due to assuming thatwindow
orglobal
object is available in the executing environment, but none of those are actually available in the Web Workers context.
-
4e949ec8
#1401 Thanks @Andarist! - Fixed an issue with spawned actors being spawned multiple times when they got spawned in an initial state of a child machine that is invoked in the initial state of a parent machine.Illustrating example for curious readers.
const child = createMachine({ initial: 'bar', context: {}, states: { bar: { entry: assign({ promise: () => { return spawn(() => Promise.resolve('answer')); } }) } } }); const parent = createMachine({ initial: 'foo', states: { foo: { invoke: { src: child, onDone: 'end' } }, end: { type: 'final' } } }); interpret(parent).start();
b72e29dd
#1354 Thanks @davidkpiano! - TheAction
type was simplified, and as a result, you should see better TypeScript performance.
-
4dbabfe7
#1320 Thanks @davidkpiano! - Theinvoke.src
property now accepts an object that describes the invoke source with itstype
and other related metadata. This can be read from theservices
option in themeta.src
argument:const machine = createMachine( { initial: 'searching', states: { searching: { invoke: { src: { type: 'search', endpoint: 'example.com' } // ... } // ... } } }, { services: { search: (context, event, { src }) => { console.log(src); // => { endpoint: 'example.com' } } } } );
Specifying a string for
invoke.src
will continue to work the same; e.g., ifsrc: 'search'
was specified, this would be the same assrc: { type: 'search' }
.
8662e543
#1317 Thanks @Andarist! - AllTTypestate
type parameters default to{ value: any; context: TContext }
now and the parametrized type is passed correctly between various types which results in more accurate types involving typestates.
3ab3f25e
#1285 Thanks @Andarist! - Fixed an issue with initial state of invoked machines being read without custom data passed to them which could lead to a crash when evaluating transient transitions for the initial state.
-
a7da1451
#1290 Thanks @davidkpiano! - The "Attempted to spawn an Actor [...] outside of a service. This will have no effect." warnings are now silenced for "lazily spawned" actors, which are actors that aren't immediately active until the function that creates them are called:// ⚠️ "active" actor - will warn spawn(somePromise); // 🕐 "lazy" actor - won't warn spawn(() => somePromise); // 🕐 machines are also "lazy" - won't warn spawn(someMachine);
It is recommended that all
spawn(...)
-ed actors are lazy, to avoid accidentally initializing them e.g., when readingmachine.initialState
or calculating otherwise pure transitions. In V5, this will be enforced.
c1f3d260
#1317 Thanks @Andarist! - Fixed a type returned by araise
action - it's nowRaiseAction<TEvent> | SendAction<TContext, AnyEventObject, TEvent>
instead ofRaiseAction<TEvent> | SendAction<TContext, TEvent, TEvent>
. This makes it comaptible in a broader range of scenarios.
8270d5a7
#1372 Thanks @christianchown! - Narrowed theServiceConfig
type definition to use a specific event type to prevent compilation errors on strictly-typedMachineOptions
.
01e3e2dc
#1320 Thanks @davidkpiano! - The JSON definition forstateNode.invoke
objects will no longer include theonDone
andonError
transitions, since those transitions are already merged into thetransitions
array. This solves the issue of reviving a serialized machine from JSON, where before, theonDone
andonError
transitions for invocations were wrongly duplicated.
36ed8d0a
#1262 Thanks @Andarist! - Improved type inference forInvokeConfig['data']
. This has required renamingdata
property onStateNode
instances todoneData
. This property was never meant to be a part of the public API, so we don't consider this to be a breaking change.
-
2c75ab82
#1219 Thanks @davidkpiano! - The resolved value of theinvoke.data
property is now available in the "invoke meta" object, which is passed as the 3rd argument to the service creator inoptions.services
. This will work for all types of invoked services now, including promises, observables, and callbacks.const machine = createMachine({ initial: 'pending', context: { id: 42 }, states: { pending: { invoke: { src: 'fetchUser', data: { userId: (context) => context.id }, onDone: 'success' } }, success: { type: 'final' } } }, { services: { fetchUser: (ctx, _, { data }) => { return fetch(`some/api/user/${data.userId}`) .then(response => response.json()); } } }
-
a6c78ae9
#1249 Thanks @davidkpiano! - New property introduced for eventless (transient) transitions:always
, which indicates a transition that is always taken when in that state. Empty string transition configs for transient transitions are deprecated in favor ofalways
:// ... states: { playing: { + always: [ + { target: 'win', cond: 'didPlayerWin' }, + { target: 'lose', cond: 'didPlayerLose' }, + ], on: { // ⚠️ Deprecation warning - '': [ - { target: 'win', cond: 'didPlayerWin' }, - { target: 'lose', cond: 'didPlayerLose' }, - ] } } } // ...
The old empty string syntax (
'': ...
) will continue to work until V5.
36ed8d0a
#1262 Thanks @Andarist! -StateMachine<any, any, any>
is no longer a part of theInvokeConfig
type, but rather it creates a union withInvokeConfig
in places where it is needed. This change shouldn't affect consumers' code.
0133954
#1178 Thanks @davidkpiano! - The types for thesend()
andsendParent()
action creators have been changed to fix the issue of only being able to send events that the machine can receive. In reality, a machine can and should send events to other actors that it might not be able to receive itself. See #711 for more information.
a1f1239
#1189 Thanks @davidkpiano! - Previously,state.matches(...)
was problematic because it was castingstate
tonever
if it didn't match the state value. This is now fixed by making theTypestate
resolution more granular.
dbc6a16
#1183 Thanks @davidkpiano! - Actions from a restored state provided as a custom initial state tointerpret(machine).start(initialState)
are now executed properly. See #1174 for more information.
a10d604
#1176 Thanks @itfarrier! - Fix passing state schema into State generic
326db72
#1185 Thanks @Andarist! - Fixed an issue with invoked service not being correctly started if other service got stopped in a subsequent microstep (in response to raised or null event).
c3a496e
#1160 Thanks @davidkpiano! - Delayed transitions defined usingafter
were previously causing a circular dependency when the machine was converted using.toJSON()
. This has now been fixed.
e16e48e
#1153 Thanks @Andarist! - Fixed an issue withchoose
andpure
not being able to use actions defined in options.
d496ecb
#1165 Thanks @davidkpiano! - XState will now warn if you define an.onDone
transition on the root node. Root nodes which are "done" represent the machine being in its final state, and can no longer accept any events. This has been reported as confusing in #1111.
8a97785
#1137 Thanks @davidkpiano! - Added docs for thechoose()
andpure()
action creators, as well as exporting thepure()
action creator in theactions
object.
e65dee9
#1131 Thanks @wKovacs64! - Include the newchoose
action in theactions
export from thexstate
core package. This was missed in v4.9.0.
f3ff150
#1103 Thanks @davidkpiano! - Simplify theTransitionConfigArray
andTransitionConfigMap
types in order to fix excessively deep type instantiation TypeScript reports. This addresses #1015.
-
6c47b66
#1076 Thanks @Andarist! - Added support for conditional actions. It's possible now to have actions executed based on conditions using following:entry: [ choose([ { cond: ctx => ctx > 100, actions: raise('TOGGLE') }, { cond: 'hasMagicBottle', actions: [assign(ctx => ({ counter: ctx.counter + 1 }))] }, { actions: ['fallbackAction'] } ]) ];
It works very similar to the if-else syntax where only the first matched condition is causing associated actions to be executed and the last ones can be unconditional (serving as a general fallback, just like else branch).
1a129f0
#1073 Thanks @Andarist! - Cleanup internal structures upon receiving termination events from spawned actors.
e88aa18
#1085 Thanks @Andarist! - Fixed an issue with data expressions of root's final nodes being called twice.
-
88b17b2
#1090 Thanks @rjdestigter! - This change carries forward the typestate type information encoded in the arguments of the following functions and assures that the return type also has the same typestate type information:- Cloned state machine returned by
.withConfig
. .state
getter defined for services.start
method of services.
- Cloned state machine returned by
d5f622f
#1069 Thanks @davidkpiano! - Loosened event type forSendAction<TContext, AnyEventObject>
-
55aa589
#960 Thanks @davidkpiano! - The machine can now be safely JSON-serialized, usingJSON.stringify(machine)
. The shape of this serialization is defined inmachine.schema.json
and reflected inmachine.definition
.Note that
onEntry
andonExit
have been deprecated in the definition in favor ofentry
andexit
.
520580b
#967 Thanks @andrewgordstewart! - Add context & event types to InvokeConfig
c8db035
#936 Thanks @davidkpiano! - Theescalate()
action can now take in an expression, which will be evaluated against thecontext
,event
, andmeta
to return the error data.
2a3fea1
#952 Thanks @davidkpiano! - The typings for the raise() action have been fixed to allow any event to be raised. This typed behavior will be refined in version 5, to limit raised events to those that the machine accepts.
f86d419
#957 Thanks @Andarist! - Fixed memory leak - each created service has been registered in internal map but it was never removed from it. Registration has been moved to a point where Interpreter is being started and it's deregistered when it is being stopped.
- dae8818: Typestates are now propagated to interpreted services.
- 6b3d767: Fixed issue with delayed transitions scheduling a delayed event for each transition defined for a single delay.
- 9b043cd: The initial state is now cached inside of the service instance instead of the machine, which was the previous (faulty) strategy. This will prevent entry actions on initial states from being called more than once, which is important for ensuring that actors are not spawned more than once.
- 2b134eee: Fixed issue with events being forwarded to children after being processed by the current machine. Events are now always forwarded first.
- 2b134eee: Fixed issue with not being able to spawn an actor when processing an event batch.