preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.64k stars 89 forks source link

Add support for atomic transactions #461

Closed PsychoLlama closed 5 months ago

PsychoLlama commented 7 months ago

This is an implementation for my feature request in issue 460. It adds a new function called transaction(...) which behaves similar to batch(...), except it rolls back all the state changes if an error is thrown.

const a = signal('original')

try {
  transaction(() => {
    a.value = 'changed'
    throw new Error('kaboom')
  })
} catch {
  a.value // 'original'
}

Intermediate states aren't observable until the transaction is complete.


I know this change is public facing so I'm not expecting it to be straightforward. Think of the PR as a proof of concept and feel free to say no.

changeset-bot[bot] commented 7 months ago

⚠️ No Changeset found

Latest commit: c770ff22a40d7b2de4be9594877b4a295faea9cd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

netlify[bot] commented 7 months ago

Deploy Preview for preact-signals-demo ready!

Name Link
Latest commit c770ff22a40d7b2de4be9594877b4a295faea9cd
Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/657622214238a10008f430fa
Deploy Preview https://deploy-preview-461--preact-signals-demo.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

PsychoLlama commented 5 months ago

Closing - see related issue for discussion.