From 8854d3277541a67aef4187b5d3592bea5a7fcfa2 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Thu, 18 Jul 2019 14:14:35 -0400 Subject: Can edit computed fields in KVP --- src/new_fields/ScriptField.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/new_fields/ScriptField.ts') diff --git a/src/new_fields/ScriptField.ts b/src/new_fields/ScriptField.ts index e2994ed70..b5b1595cf 100644 --- a/src/new_fields/ScriptField.ts +++ b/src/new_fields/ScriptField.ts @@ -4,6 +4,7 @@ import { Copy, ToScriptString, Parent, SelfProxy } from "./FieldSymbols"; import { serializable, createSimpleSchema, map, primitive, object, deserialize, PropSchema, custom, SKIP } from "serializr"; import { Deserializable } from "../client/util/SerializationHelper"; import { Doc } from "../new_fields/Doc"; +import { Plugins } from "./util"; function optional(propSchema: PropSchema) { return custom(value => { @@ -93,4 +94,30 @@ export class ComputedField extends ScriptField { } return undefined; } +} + +export namespace ComputedField { + let useComputed = true; + export function DisableComputedFields() { + useComputed = false; + } + + export function EnableComputedFields() { + useComputed = true; + } + + export function WithoutComputed(fn: () => T) { + DisableComputedFields(); + try { + return fn(); + } finally { + EnableComputedFields(); + } + } + + Plugins.addGetterPlugin((doc, _, value) => { + if (useComputed && value instanceof ComputedField) { + return { value: value.value(doc), shouldReturn: true }; + } + }); } \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 6da5737c72abe18c366889273a179a3bf94351be Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Fri, 19 Jul 2019 19:23:01 -0400 Subject: Added caching to computed fields --- src/new_fields/ScriptField.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/new_fields/ScriptField.ts') diff --git a/src/new_fields/ScriptField.ts b/src/new_fields/ScriptField.ts index b5b1595cf..e8a1ea28a 100644 --- a/src/new_fields/ScriptField.ts +++ b/src/new_fields/ScriptField.ts @@ -5,6 +5,7 @@ import { serializable, createSimpleSchema, map, primitive, object, deserialize, import { Deserializable } from "../client/util/SerializationHelper"; import { Doc } from "../new_fields/Doc"; import { Plugins } from "./util"; +import { computedFn } from "mobx-utils"; function optional(propSchema: PropSchema) { return custom(value => { @@ -87,13 +88,13 @@ export class ScriptField extends ObjectField { @Deserializable("computed", deserializeScript) export class ComputedField extends ScriptField { //TODO maybe add an observable cache based on what is passed in for doc, considering there shouldn't really be that many possible values for doc - value(doc: Doc) { + value = computedFn((doc: Doc) => { const val = this.script.run({ this: doc }); if (val.success) { return val.result; } return undefined; - } + }); } export namespace ComputedField { -- cgit v1.2.3-70-g09d2