Skip to content

Conversation

davidkpiano
Copy link
Member

  • Removed unused setter function and associated recipe parameter.
  • Updated transition logic to prevent unnecessary updates when the snapshot remains unchanged.
  • Enhanced tests to verify behavior of effect-only and emit-only transitions, ensuring no updates trigger when the snapshot is the same.

- Removed unused setter function and associated recipe parameter.
- Updated transition logic to prevent unnecessary updates when the snapshot remains unchanged.
- Enhanced tests to verify behavior of effect-only and emit-only transitions, ensuring no updates trigger when the snapshot is the same.
@davidkpiano davidkpiano requested a review from Andarist July 6, 2025 23:33
Copy link

changeset-bot bot commented Jul 6, 2025

🦋 Changeset detected

Latest commit: 1bdeb0f

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

store.getSnapshot().context.count satisfies string;
});

it('should not trigger update if the snapshot is the same', () => {
Copy link
Collaborator

@Andarist Andarist Jul 7, 2025

Choose a reason for hiding this comment

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

note this diverges from XState too:

import { createActor, createMachine } from "xstate";

const m = createMachine({
  on: {
    FOO: {
      actions: () => {},
    },
  },
});

const a = createActor(m).start();

let s = a.getSnapshot();

a.subscribe((_s) => {
  console.log("next", s === _s, _s);
  s = _s;
});

a.send({ type: "FOO" });
a.send({ type: "FOO" });
a.send({ type: "FOO" });

With the above we get:
Screenshot 2025-07-07 at 08 56 07

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you think about this behavior? If only actions/effects occur (invisible to the state, at least right now), then the state is going to be exactly the same. But I guess that's the responsibility of the consumer to "filter"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Similarly here, I think it would be totally OK to filter those out - but I think it would be great if this could be consistent between XState libraries.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think in XState v6, the purpose of the observer is to observe snapshot changes, so notifying on the same snapshot isn't useful.

@davidkpiano
Copy link
Member Author

@Andarist Is this one good to go? Since XState Store is forward-looking, I'll make sure that v6 has the same behavior of only updating when the snapshot changes.

//
});
return ctx; // Context is the same, so no update is triggered
// This is the same as not returning anything (void)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: it feels this "variant" is kind of hidden in this comment when "same state" is mentioned explicitly in the text leading to this code sample

Comment on lines 77 to 78
if (nextSnapshot === currentSnapshot && !effects.length) {
return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

all tests pass without this, I pushed out a commit removing this check

@davidkpiano davidkpiano merged commit cb08332 into main Oct 9, 2025
1 check passed
@davidkpiano davidkpiano deleted the davidkpiano/store-fix-unnecessary-update branch October 9, 2025 12:41
@github-actions github-actions bot mentioned this pull request Oct 9, 2025
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.

2 participants