svecosystem / runed

Magical utilities for your Svelte applications (WIP)
https://runed.dev
MIT License
387 stars 21 forks source link

feat: update `watch` #83

Closed abdel-17 closed 1 month ago

abdel-17 commented 1 month ago

This PR changes two things about the watch helper.

  1. Remove the once: boolean option in favor of a separate watchOnce function. The default behavior for watch is to run immediately, unlike in Vue where it runs lazily when a dependency changes, so having once: true behave just like onMount by default makes no sense. As an added bonus, watch is now much simpler internally.

  2. To watch for changes to multiple values, you pass an array of getters. Previously, you would pass a getter to an array of state values, but this is kind of ambiguous. What if I want to watch for changes to a single state that happens to be an array?

    
    let items = $state([]);
    watch(() => items, (items, previousItems) => {
    // `previousItems` is an empty array, but it should be `undefined`!
    });

// proposed change: let a = $state(); let b = $state(); watch([() => a, () => b], ([a, b], [prevA, prevB]) => {...});

changeset-bot[bot] commented 1 month ago

🦋 Changeset detected

Latest commit: 4ff1e8a2bf80c328af8e96c4ae755c6e7c98319b

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

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

github-actions[bot] commented 1 month ago
built with Refined Cloudflare Pages Action

âš¡ Cloudflare Pages Deployment

Name Status Preview Last Commit
runed ✅ Ready (View Log) Visit Preview 4ff1e8a2bf80c328af8e96c4ae755c6e7c98319b