github statelyai/xstate xstate@4.22.0

2 years ago

Minor Changes

  • 1b32aa0d #2356 Thanks @davidkpiano! - The model created from createModel(...) now provides a .createMachine(...) method that does not require passing any generic type parameters:

    const model = createModel(/* ... */);
    
    -const machine = createMachine<typeof model>(/* ... */);
    +const machine = model.createMachine(/* ... */);
  • 432b60f7 #2280 Thanks @davidkpiano! - Actors can now be invoked/spawned from reducers using the fromReducer(...) behavior creator:

    import { fromReducer } from 'xstate/lib/behaviors';
    
    type CountEvent = { type: 'INC' } | { type: 'DEC' };
    
    const countReducer = (count: number, event: CountEvent): number => {
      if (event.type === 'INC') {
        return count + 1;
      } else if (event.type === 'DEC') {
        return count - 1;
      }
    
      return count;
    };
    
    const countMachine = createMachine({
      invoke: {
        id: 'count',
        src: () => fromReducer(countReducer, 0)
      },
      on: {
        INC: {
          actions: forwardTo('count')
        },
        DEC: {
          actions: forwardTo('count')
        }
      }
    });
  • f9bcea2c #2366 Thanks @davidkpiano! - Actors can now be spawned directly in the initial machine.context using lazy initialization, avoiding the need for intermediate states and unsafe typings for immediately spawned actors:

    const machine = createMachine<{ ref: ActorRef<SomeEvent> }>({
      context: () => ({
        ref: spawn(anotherMachine, 'some-id') // spawn immediately!
      })
      // ...
    });

Don't miss a new xstate release

NewReleases is sending notifications on new releases.