diff options
author | bobzel <zzzman@gmail.com> | 2021-09-17 17:51:46 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2021-09-17 17:51:46 -0400 |
commit | 385c635159bfa9bff86ddd6e46c96b934d2a9391 (patch) | |
tree | 302c833f762a68e573e38b8d33383206c967339d /src/fields/Proxy.ts | |
parent | b4a3dd6551a0ad6582c2a1a42168faae74ef1913 (diff) |
fixed following links to documents in sidebars that haven't been opened to still open the sidebar and show the document. required a change to how ProxyFields set field promises and use field caches.
Diffstat (limited to 'src/fields/Proxy.ts')
-rw-r--r-- | src/fields/Proxy.ts | 66 |
1 files changed, 10 insertions, 56 deletions
diff --git a/src/fields/Proxy.ts b/src/fields/Proxy.ts index 62734d3d2..07553f17c 100644 --- a/src/fields/Proxy.ts +++ b/src/fields/Proxy.ts @@ -9,72 +9,33 @@ import { Id, Copy, ToScriptString, ToString } from "./FieldSymbols"; import { scriptingGlobal } from "../client/util/Scripting"; import { Plugins } from "./util"; -function deserializeProxy(field: any) { - if (!field.cache) { - field.cache = DocServer.GetCachedRefField(field.fieldId) as any; - } -} -@Deserializable("proxy", deserializeProxy) +@Deserializable("proxy") 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; - this.fieldId = value; - } else if (value) { - this.cache = value; - this.fieldId = value[Id]; - } - } - - [Copy]() { - if (this.cache) return new ProxyField<T>(this.cache); - return new ProxyField<T>(this.fieldId); + this.fieldId = typeof value === "string" ? value : (value?.[Id] ?? ""); } - [ToScriptString]() { - return "invalid"; - } - [ToString]() { - return "ProxyField"; - } + [Copy]() { return new ProxyField<T>((this.cache ?? this.fieldId) as T); } + [ToScriptString]() { return "invalid"; } + [ToString]() { return "ProxyField"; } @serializable(primitive()) readonly fieldId: string = ""; - - // 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 set cache(field: T | undefined) { - this._cache = { field }; - } - private failed = false; private promise?: Promise<any>; + private get cache(): T | undefined { return DocServer.GetCachedRefField(this.fieldId) as T } + value(): T | undefined | FieldWaiting<T> { - if (this.cache) { - return this.cache; - } - if (this.failed) { - return undefined; - } + 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; })); @@ -83,14 +44,7 @@ export class ProxyField<T extends RefField> extends ObjectField { } promisedValue(): string { return !this.cache && !this.failed && !this.promise ? this.fieldId : ""; } setPromise(promise: any) { - this.promise = promise; - } - @action - setValue(field: any) { - this.promise = undefined; - this.cache = field; - if (field === undefined) this.failed = true; - return field; + if (this.cache === undefined) this.promise = promise; } } |