statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
27.21k stars 1.26k forks source link

[@xstate/store] Event emitter #5064

Closed davidkpiano closed 2 months ago

davidkpiano commented 2 months ago

You can now emit events from a store:

import { createStore } from '@xstate/store';

const store = createStore({
  context: {
    count: 0
  },
  on: {
    increment: (context, event, { emit }) => {
      emit({ type: 'incremented' });
      return { count: context.count + 1 };
    }
  }
});

store.on('incremented', () => {
  console.log('incremented!');
});

You can make emitted events type-safe via createStore({ types: { … } })

changeset-bot[bot] commented 2 months ago

🦋 Changeset detected

Latest commit: 996b511b4e410897800c2582940d128790e351a8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ------------- | ----- | | @xstate/store | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

davidkpiano commented 2 months ago

@tonivj5 Do you have any thoughts on this to share? (responding to emoji reaction)

davidkpiano commented 2 months ago

@Andarist Types are failing

tonivj5 commented 2 months ago

hey @davidkpiano! My "corcerns" (or doubts) about these changes were about make zod "first citizen", I love the idea itself, but I personally prefer to avoid "hard" coupling and let it open to integrate other solutions using adapters or some other kind of technique (and I think @colinhacks is drafting an awesome solution to achive it with https://github.com/standard-schema/standard-schema).

Zod is a brilliant piece of software, I've used it before although I love and use other libraries too as https://github.com/sinclairzx81/typebox and I think this field is a moving target that it's going to beneficate from making it elegible.

Anyway, I'm just passing through here so take my reactions and comments with a grain of salt 😅. I love from time to time review your work and check what's cooking here 😸

davidkpiano commented 2 months ago

@tonivj5 You're in luck - we moved the Zod stuff out and that will probably be a separate PR later, if we decide to do it.