diff options
| author | Mohammad Amoush <mohammad_amoush@brown.edu> | 2019-07-22 13:57:10 -0400 | 
|---|---|---|
| committer | Mohammad Amoush <mohammad_amoush@brown.edu> | 2019-07-22 13:57:10 -0400 | 
| commit | 658ded478c273654174cd2706f8b3e021b8ceb95 (patch) | |
| tree | 7526f7850dd3702b9746125d2f4349a0d6aea8ee /src/new_fields/util.ts | |
| parent | 157060f7e6029c76765aa20d8fdbe325401a3880 (diff) | |
| parent | 8db50c6ba0be83b85c896043da53e40c17523e90 (diff) | |
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into youtube-api-muhammed
Diffstat (limited to 'src/new_fields/util.ts')
| -rw-r--r-- | src/new_fields/util.ts | 28 | 
1 files changed, 24 insertions, 4 deletions
| diff --git a/src/new_fields/util.ts b/src/new_fields/util.ts index 47e467041..b59ec9b9a 100644 --- a/src/new_fields/util.ts +++ b/src/new_fields/util.ts @@ -1,5 +1,5 @@  import { UndoManager } from "../client/util/UndoManager"; -import { Doc, Field } from "./Doc"; +import { Doc, Field, FieldResult } from "./Doc";  import { SerializationHelper } from "../client/util/SerializationHelper";  import { ProxyField } from "./Proxy";  import { RefField } from "./RefField"; @@ -11,6 +11,20 @@ import { ComputedField } from "./ScriptField";  function _readOnlySetter(): never {      throw new Error("Documents can't be modified in read-only mode");  } + +export interface GetterResult { +    value: FieldResult; +    shouldReturn: boolean; +} +export type GetterPlugin = (receiver: any, prop: string | number, currentValue: any) => GetterResult | undefined; +const getterPlugins: GetterPlugin[] = []; + +export namespace Plugins { +    export function addGetterPlugin(plugin: GetterPlugin) { +        getterPlugins.push(plugin); +    } +} +  const _setterImpl = action(function (target: any, prop: string | symbol | number, value: any, receiver: any): boolean {      //console.log("-set " + target[SelfProxy].title + "(" + target[SelfProxy][prop] + ")." + prop.toString() + " = " + value);      if (SerializationHelper.IsSerializing()) { @@ -85,12 +99,18 @@ export function getter(target: any, prop: string | symbol | number, receiver: an  function getFieldImpl(target: any, prop: string | number, receiver: any, ignoreProto: boolean = false): any {      receiver = receiver || target[SelfProxy]; -    const field = target.__fields[prop]; +    let field = target.__fields[prop];      if (field instanceof ProxyField) {          return field.value();      } -    if (field instanceof ComputedField) { -        return field.value(receiver); +    for (const plugin of getterPlugins) { +        const res = plugin(receiver, prop, field); +        if (res === undefined) continue; +        if (res.shouldReturn) { +            return res.value; +        } else { +            field = res.value; +        }      }      if (field === undefined && !ignoreProto && prop !== "proto") {          const proto = getFieldImpl(target, "proto", receiver, true);//TODO tfs: instead of receiver we could use target[SelfProxy]... I don't which semantics we want or if it really matters | 
