github statelyai/xstate xstate@4.20.0

2 years ago

Minor Changes

  • 28059b9f #2197 Thanks @davidkpiano! - All spawned and invoked actors now have a .getSnapshot() method, which allows you to retrieve the latest value emitted from that actor. That value may be undefined if no value has been emitted yet.

    const machine = createMachine({
      context: {
        promiseRef: null
      },
      initial: 'pending',
      states: {
        pending: {
          entry: assign({
            promiseRef: () => spawn(fetch(/* ... */), 'some-promise')
          })
        }
      }
    });
    
    const service = interpret(machine)
      .onTransition(state => {
        // Read promise value synchronously
        const resolvedValue = state.context.promiseRef?.getSnapshot();
        // => undefined (if promise not resolved yet)
        // => { ... } (resolved data)
      })
      .start();
    
    // ...

Patch Changes

  • 4ef03465 #2240 Thanks @VanTanev! - Preserve StateMachine type when .withConfig() and .withContext() modifiers are used on a machine.

Don't miss a new xstate release

NewReleases is sending notifications on new releases.