]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: writable computed
authorEvan You <yyx990803@gmail.com>
Thu, 6 Jun 2019 04:35:49 +0000 (12:35 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 6 Jun 2019 04:35:49 +0000 (12:35 +0800)
packages/observer/src/computed.ts

index f91bfb472c777499ee11ec6fb13511629ea17a84..81a6f7efb49e6b4f868f9c69913443188a0a0b7e 100644 (file)
@@ -7,13 +7,13 @@ export interface ComputedValue<T> {
   readonly effect: ReactiveEffect
 }
 
-export function computed<T, C = null>(
-  getter: (this: C, ctx: C) => T,
-  context?: C
+export function computed<T>(
+  getter: () => T,
+  setter?: (v: T) => void
 ): ComputedValue<T> {
   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<T, C = null>(
       // 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)