aboutsummaryrefslogtreecommitdiff
path: root/src/fields/Proxy.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/fields/Proxy.ts')
-rw-r--r--src/fields/Proxy.ts120
1 files changed, 56 insertions, 64 deletions
diff --git a/src/fields/Proxy.ts b/src/fields/Proxy.ts
index 2c5f38818..55d1d9ea4 100644
--- a/src/fields/Proxy.ts
+++ b/src/fields/Proxy.ts
@@ -1,95 +1,90 @@
-import { Deserializable } from "../client/util/SerializationHelper";
-import { FieldWaiting } from "./Doc";
-import { primitive, serializable } from "serializr";
-import { observable, action, runInAction } from "mobx";
-import { DocServer } from "../client/DocServer";
-import { RefField } from "./RefField";
-import { ObjectField } from "./ObjectField";
-import { Id, Copy, ToScriptString, ToString } from "./FieldSymbols";
-import { scriptingGlobal } from "../client/util/ScriptingGlobals";
-import { Plugins } from "./util";
+import { Deserializable } from '../client/util/SerializationHelper';
+import { FieldWaiting, Opt } from './Doc';
+import { primitive, serializable } from 'serializr';
+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, ToValue } from './FieldSymbols';
+import { scriptingGlobal } from '../client/util/ScriptingGlobals';
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)
+@Deserializable('proxy', deserializeProxy)
export class ProxyField<T extends RefField> extends ObjectField {
constructor();
constructor(value: T);
constructor(fieldId: string);
constructor(value?: T | string) {
super();
- if (typeof value === "string") {
- this.cache = DocServer.GetCachedRefField(value) as any;
+ if (typeof value === 'string') {
+ //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 'invalid';
}
[ToString]() {
- return "ProxyField";
+ return 'ProxyField';
}
@serializable(primitive())
- readonly fieldId: string = "";
+ readonly fieldId: string = '';
- // This getter/setter and nested object thing is
+ // 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>;
- value(): T | undefined | FieldWaiting<T> {
- if (this.cache) {
- return this.cache;
- }
- if (this.failed) {
- return undefined;
- }
- if (!this.promise) {
- const cached = DocServer.GetCachedRefField(this.fieldId);
- if (cached !== undefined) {
- runInAction(() => this.cache = cached as any);
- return cached as any;
- }
- this.promise = DocServer.GetRefField(this.fieldId).then(action((field: any) => {
- this.promise = undefined;
- this.cache = field;
- if (field === undefined) this.failed = true;
- return field;
- }));
+ @computed get value(): T | undefined | FieldWaiting<T> {
+ if (this.cache.field) return this.cache.field;
+ if (this.failed) return undefined;
+
+ 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 DocServer.GetCachedRefField(this.fieldId) ?? (this.promise as any);
+ return this.cache.field ?? this.cache.p;
}
- promisedValue(): string { return !this.cache && !this.failed && !this.promise ? this.fieldId : ""; }
- setPromise(promise: any) {
- this.promise = promise;
+ @computed get needsRequesting(): boolean {
+ return !this.cache.field && !this.failed && !this._cache.p && !DocServer.GetCachedRefField(this.fieldId) ? true : false;
+ }
+
+ 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,20 +108,17 @@ 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
-@Deserializable("prefetch_proxy", prefetchValue)
-export class PrefetchProxy<T extends RefField> extends ProxyField<T> {
-}
+@Deserializable('prefetch_proxy', prefetchValue)
+export class PrefetchProxy<T extends RefField> extends ProxyField<T> {}