Last active
November 11, 2025 05:04
-
-
Save Shilo/62e74e920232efabe3d6bf6e3893b99c to your computer and use it in GitHub Desktop.
Svelte 5 ref wrapper for universal reactivity. (Not sure if it's useful)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function ref(initialValue) { | |
| const state = $state({value: initialValue}); | |
| return state; | |
| } | |
| export function watchedRef(initialValue, onSet, onGet) { | |
| let value = $state(initialValue) | |
| return { | |
| set value(newValue) { | |
| const oldValue = value | |
| value = newValue | |
| onSet?.(value, oldValue) | |
| }, | |
| get value() { | |
| onGet?.(value) | |
| return value | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment