preactjs / signals

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

Allow setting a signal value inside a computed #525

Closed jviide closed 3 months ago

jviide commented 3 months ago

This pull request allows setting a signal's value inside a computed. Previously the following code was not allowed and raised an error with the message "Computed cannot have side-effects":

const s = signal(0);

const c = computed(() => {
  s.value = 1;
});

c.value; // Error: Computed cannot have side-effects

This suggested change is based on Slack discussions (TL;DR: there may be legitimate use-cases that the side-effect limitation blocks / the "regular" cycle detection might be enough for preventing runaway modification loops) and submitted here for further evaluation. /cc @developit @marvinhagemeister

This change saves some bytes from the output bundles:

Before this change:
       1481 B: signals-core.js.gz
       1350 B: signals-core.js.br
       1494 B: signals-core.mjs.gz
       1363 B: signals-core.mjs.br
       1488 B: signals-core.module.js.gz
       1352 B: signals-core.module.js.br
       1557 B: signals-core.min.js.gz
       1414 B: signals-core.min.js.br

After this change:
       1439 B: signals-core.js.gz
       1312 B: signals-core.js.br
       1452 B: signals-core.mjs.gz
       1325 B: signals-core.mjs.br
       1448 B: signals-core.module.js.gz
       1315 B: signals-core.module.js.br
       1518 B: signals-core.min.js.gz
       1381 B: signals-core.min.js.br
changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: cb6bdabbd31b27f8435c7976089fa276da6bfb7a

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

This PR includes changesets to release 1 package | Name | Type | | -------------------- | ----- | | @preact/signals-core | 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

netlify[bot] commented 3 months ago

Deploy Preview for preact-signals-demo ready!

Name Link
Latest commit cb6bdabbd31b27f8435c7976089fa276da6bfb7a
Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/65f2f9066f41a40008360054
Deploy Preview https://deploy-preview-525--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.