Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@xstate/store] Event emitter #5064

Merged
merged 36 commits into from
Sep 16, 2024
Merged

[@xstate/store] Event emitter #5064

merged 36 commits into from
Sep 16, 2024

Conversation

davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Sep 4, 2024

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: { … } })

Copy link

changeset-bot bot commented Sep 4, 2024

🦋 Changeset detected

Latest commit: 996b511

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
Copy link
Member Author

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

Comment on lines 307 to 309
export interface StoreSchemas {
emitted?: Record<string, { _output: unknown }>;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I'm not sure why this is introduced here. It should be a separate followup PR. I also don't necessarily understand why this one only adds support for schema.emitted. What about schema.events?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move schema types to a separate PR

@davidkpiano
Copy link
Member Author

@Andarist Types are failing

@tonivj5
Copy link

tonivj5 commented Sep 15, 2024

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
Copy link
Member Author

@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.

@davidkpiano davidkpiano merged commit 84aca37 into main Sep 16, 2024
1 check passed
@davidkpiano davidkpiano deleted the davidkpiano/xstate-store-emit branch September 16, 2024 00:14
@github-actions github-actions bot mentioned this pull request Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants