statelyai / xstate

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

`fromPromise` passes signal to creator #4832

Closed cevr closed 5 months ago

cevr commented 6 months ago

Currently there is no notion of cancelling promises that are passed into a fromPromise logic creator if the state transitions before the promise is resolved.

This is a problem if the promise has side effects that run after resolving/rejecting - eg retries, cache invalidations, etc.

Now the signal can be used to cancel any side effects

changeset-bot[bot] commented 6 months ago

🦋 Changeset detected

Latest commit: 8ec3f9ac5a2e63ab9b76c3d3a6b3e2d23b730766

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

This PR includes changesets to release 1 package | Name | Type | | ------ | ----- | | xstate | 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

cevr commented 6 months ago

@Andarist Thanks for the quick review! I added some more test cases in line with your comments, and opted to using a WeakMap to capture references to unique actors, since id is not guaranteed to be unique within a machine.

What are your thoughts on that?

Andarist commented 5 months ago

Using WeakMap is fine 👍

cevr commented 5 months ago

@Andarist Updated the tests. Though I had to add ESNext.Promise to the tsconfig lib. Is that alright?

cevr commented 5 months ago

also my changes in 29ae633 (#4832) seem to have broken one seemingly unrelated test 🤔 not sure how yet

Andarist commented 5 months ago

also my changes in 29ae633 (#4832) seem to have broken one seemingly unrelated test 🤔 not sure how yet

it's a common~ issue with async tests that share a thread ;p an error that was "created" by one of the tests might get thrown (and caught by the global handler) while another test is already executing