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

Version Packages #5190

Merged
merged 1 commit into from
Feb 10, 2025
Merged

Version Packages #5190

merged 1 commit into from
Feb 10, 2025

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Feb 9, 2025

This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.

Releases

@xstate/[email protected]

Major Changes

  • #5175 38aa9f518ee2f9a5f481306a1dc68c0ad47d28d5 Thanks @davidkpiano! - The createStore function now only accepts a single configuration object argument. This is a breaking change that simplifies the API and aligns with the configuration pattern used throughout XState.

    // Before
    // createStore(
    //   {
    //     count: 0
    //   },
    //   {
    //     increment: (context) => ({ count: context.count + 1 })
    //   }
    // );
    
    // After
    createStore({
      context: {
        count: 0
      },
      on: {
        increment: (context) => ({ count: context.count + 1 })
      }
    });
  • #5175 38aa9f518ee2f9a5f481306a1dc68c0ad47d28d5 Thanks @davidkpiano! - You can now enqueue effects in state transitions.

    const store = createStore({
      context: {
        count: 0
      },
      on: {
        incrementDelayed: (context, event, enq) => {
          enq.effect(async () => {
            await new Promise((resolve) => setTimeout(resolve, 1000));
            store.send({ type: 'increment' });
          });
    
          return context;
        },
        increment: (context) => ({ count: context.count + 1 })
      }
    });
  • #5175 38aa9f518ee2f9a5f481306a1dc68c0ad47d28d5 Thanks @davidkpiano! - The fromStore(config) function now only supports a single config object argument.

    const storeLogic = fromStore({
      context: (input: { initialCount: number }) => ({
        count: input.initialCount
      }),
      on: {
        inc: (ctx, ev: { by: number }) => ({
          ...ctx,
          count: ctx.count + ev.by
        })
      }
    });
  • #5175 38aa9f518ee2f9a5f481306a1dc68c0ad47d28d5 Thanks @davidkpiano! - The createStoreWithProducer(…) function now only accepts two arguments: a producer and a config ({ context, on }) object.

    // Before
    // createStoreWithProducer(
    //   producer,
    //   {
    //     count: 0
    //   },
    //   {
    //     increment: (context) => {
    //       context.count++;
    //     }
    //   }
    // );
    
    // After
    createStoreWithProducer(producer, {
      context: {
        count: 0
      },
      on: {
        increment: (context) => {
          context.count++;
        }
      }
    });
  • #5175 38aa9f518ee2f9a5f481306a1dc68c0ad47d28d5 Thanks @davidkpiano! - Only complete assigner functions that replace the context fully are supported. This is a breaking change that simplifies the API and provides more type safety.

    const store = createStore({
      context: {
        items: [],
        count: 0
      },
      on: {
    -   increment: { count: (context) => context.count + 1 }
    -   increment: (context) => ({ count: context.count + 1 })
    +   increment: (context) => ({ ...context, count: context.count + 1 })
      }
    })
  • #5175 38aa9f518ee2f9a5f481306a1dc68c0ad47d28d5 Thanks @davidkpiano! - Emitted event types are now specified in functions on the emits property of the store definition:

    const store = createStore({
      // …
      emits: {
        increased: (payload: { upBy: number }) => {
          // You can execute a side-effect here
          // or leave it empty
        }
      },
      on: {
        inc: (ctx, ev: { by: number }, enq) => {
          enq.emit.increased({ upBy: ev.by });
    
          // …
        }
      }
    });

Minor Changes

  • #5175 38aa9f518ee2f9a5f481306a1dc68c0ad47d28d5 Thanks @davidkpiano! - Added store.trigger API for sending events with a fluent interface:

    const store = createStore({
      context: { count: 0 },
      on: {
        increment: (ctx, event: { by: number }) => ({
          count: ctx.count + event.by
        })
      }
    });
    
    // Instead of manually constructing event objects:
    store.send({ type: 'increment', by: 5 });
    
    // You can now use the fluent trigger API:
    store.trigger.increment({ by: 5 });

    The trigger API provides full type safety for event names and payloads, making it easier and safer to send events to the store.

@github-actions github-actions bot force-pushed the changeset-release/main branch from 5e972c7 to fb74281 Compare February 9, 2025 03:04
@davidkpiano davidkpiano merged commit a4f9ca3 into main Feb 10, 2025
@davidkpiano davidkpiano deleted the changeset-release/main branch February 10, 2025 14:23
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.

1 participant