diff --git a/packages/observer/src/computed.ts b/packages/observer/src/computed.ts index f91bfb47..81a6f7ef 100644 --- a/packages/observer/src/computed.ts +++ b/packages/observer/src/computed.ts @@ -7,13 +7,13 @@ export interface ComputedValue { readonly effect: ReactiveEffect } -export function computed( - getter: (this: C, ctx: C) => T, - context?: C +export function computed( + getter: () => T, + setter?: (v: T) => void ): ComputedValue { let dirty: boolean = true let value: any = undefined - const runner = effect(() => getter.call(context, context), { + const runner = effect(getter, { lazy: true, // mark effect as computed so that it gets priority during trigger computed: true, @@ -34,6 +34,13 @@ export function computed( // This should also apply for chained computed properties. trackChildRun(runner) return value + }, + set value(newValue) { + if (setter) { + setter(newValue) + } else { + // TODO warn attempting to mutate readonly computed value + } } } knownValues.add(computedValue)