diff options
Diffstat (limited to 'src/fields/Proxy.ts')
-rw-r--r-- | src/fields/Proxy.ts | 85 |
1 files changed, 39 insertions, 46 deletions
diff --git a/src/fields/Proxy.ts b/src/fields/Proxy.ts index e924ef7a3..c076f5fe1 100644 --- a/src/fields/Proxy.ts +++ b/src/fields/Proxy.ts @@ -1,17 +1,16 @@ import { Deserializable } from '../client/util/SerializationHelper'; -import { FieldWaiting } from './Doc'; +import { Field, FieldWaiting, Opt } from './Doc'; import { primitive, serializable } from 'serializr'; -import { observable, action, runInAction } from 'mobx'; +import { observable, action, runInAction, computed } from 'mobx'; import { DocServer } from '../client/DocServer'; import { RefField } from './RefField'; import { ObjectField } from './ObjectField'; -import { Id, Copy, ToScriptString, ToString } from './FieldSymbols'; +import { Id, Copy, ToScriptString, ToString, ToValue } from './FieldSymbols'; import { scriptingGlobal } from '../client/util/ScriptingGlobals'; -import { Plugins } from './util'; function deserializeProxy(field: any) { - if (!field.cache) { - field.cache = DocServer.GetCachedRefField(field.fieldId) as any; + if (!field.cache.field) { + field.cache = { field: DocServer.GetCachedRefField(field.fieldId) as any, p: undefined }; } } @Deserializable('proxy', deserializeProxy) @@ -25,18 +24,22 @@ export class ProxyField<T extends RefField> extends ObjectField { //this.cache = DocServer.GetCachedRefField(value) as any; this.fieldId = value; } else if (value) { - this.cache = value; + this.cache = { field: value, p: undefined }; this.fieldId = value[Id]; } } + [ToValue](doc: any) { + return ProxyField.toValue(this); + } + [Copy]() { - if (this.cache) return new ProxyField<T>(this.cache); + if (this.cache.field) return new ProxyField<T>(this.cache.field); return new ProxyField<T>(this.fieldId); } [ToScriptString]() { - return 'invalid'; + return Field.toScriptString(this[ToValue](undefined)?.value); // not sure this is quite right since it doesn't recreate a proxy field, but better than 'invalid' ? } [ToString]() { return 'ProxyField'; @@ -48,48 +51,40 @@ export class ProxyField<T extends RefField> extends ObjectField { // This getter/setter and nested object thing is // because mobx doesn't play well with observable proxies @observable.ref - private _cache: { readonly field: T | undefined } = { field: undefined }; - private get cache(): T | undefined { - return this._cache.field; + private _cache: { readonly field: T | undefined; p: FieldWaiting<T> | undefined } = { field: undefined, p: undefined }; + private get cache(): { field: T | undefined; p: FieldWaiting<T> | undefined } { + return this._cache; } - private set cache(field: T | undefined) { - this._cache = { field }; + private set cache(val: { field: T | undefined; p: FieldWaiting<T> | undefined }) { + runInAction(() => (this._cache = { ...val })); } private failed = false; - private promise?: Promise<any>; - @action - value(): T | undefined | FieldWaiting<T> { - if (this.cache) return this.cache; + @computed get value(): T | undefined | FieldWaiting<T> { + if (this.cache.field) return this.cache.field; if (this.failed) return undefined; - const cached = DocServer.GetCachedRefField(this.fieldId) as T; - if (cached !== undefined) { - this.cache = cached; - } else if (!this.promise) { - this.promise = DocServer.GetRefField(this.fieldId).then( - action((field: any) => { - this.promise = undefined; - this.cache = field; - this.failed = field === undefined; - return field; - }) - ) as FieldWaiting<T>; + this.cache.field = DocServer.GetCachedRefField(this.fieldId) as T; + if (!this.cache.field && !this.cache.p) { + this.cache = { + field: undefined, + p: DocServer.GetRefField(this.fieldId).then(val => this.setValue(val as T)) as FieldWaiting<T>, + }; } - return cached ?? this.promise; + return this.cache.field ?? this.cache.p; } - promisedValue(): string { - return !this.cache && !this.failed && !this.promise && !DocServer.GetCachedRefField(this.fieldId) ? this.fieldId : ''; + @computed get needsRequesting(): boolean { + return !this.cache.field && !this.failed && !this._cache.p && !DocServer.GetCachedRefField(this.fieldId) ? true : false; } - setPromise(promise: any) { - this.promise = promise; + + setExternalValuePromise(externalValuePromise: Promise<any>) { + this.cache.p = externalValuePromise.then(() => this.value) as FieldWaiting<T>; } @action - setValue(field: any) { - this.promise = undefined; - this.cache = field; - if (field === undefined) this.failed = true; + setValue(field: Opt<T>) { + this.cache = { field, p: undefined }; + this.failed = field === undefined; return field; } } @@ -113,17 +108,15 @@ export namespace ProxyField { } } - export function initPlugin() { - Plugins.addGetterPlugin((doc, _, value) => { - if (useProxy && value instanceof ProxyField) { - return { value: value.value() }; - } - }); + export function toValue(value: any) { + if (useProxy) { + return { value: value.value }; + } } } function prefetchValue(proxy: PrefetchProxy<RefField>) { - return proxy.value() as any; + return proxy.value as any; } @scriptingGlobal |