Skip to content

Commit 446184b

Browse files
committed
WIP useWritableComputed
2
1 parent 3ab6e11 commit 446184b

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { MaybeRefOrGetter } from 'vue'
2+
import { computed, ref, toValue } from 'vue'
3+
4+
export function useWritableComputed<TValue, TSet = TValue>({
5+
get = (v) => v as unknown as TValue,
6+
set = (v) => v as unknown as TValue,
7+
initial = undefined,
8+
}: {
9+
get?: (val: TValue) => TValue
10+
set?: (val: TSet) => TValue
11+
initial?: MaybeRefOrGetter<TValue>
12+
} = {}) {
13+
const innerValue = ref(toValue(initial))
14+
15+
return computed<TValue, TSet>({
16+
get: () => get(innerValue.value),
17+
set: (val) => (innerValue.value = set(val)),
18+
})
19+
}

packages/composables/src/useWritableComputed.unit.test.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)