Skip to content

Instantly share code, notes, and snippets.

@markmals
Last active November 8, 2024 22:10
Show Gist options
  • Save markmals/242b3ac4247e5ad3c479bfedfd49280f to your computer and use it in GitHub Desktop.
Save markmals/242b3ac4247e5ad3c479bfedfd49280f to your computer and use it in GitHub Desktop.
Helper functions missing from Angular's Signal implementation
import { signal, computed } from '@angular/core'
function writable<T>(fn: () => T) {
const c = computed(() => signal(fn()))
const w = () => c()()
Object.assign(w, { set: value => c().set(value) })
return w
}
function derived<T>(value: T, fn: (previous: T) => T) {
return computed(() => (value = fn(value)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment