From 385c635159bfa9bff86ddd6e46c96b934d2a9391 Mon Sep 17 00:00:00 2001 From: bobzel Date: Fri, 17 Sep 2021 17:51:46 -0400 Subject: 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. --- src/fields/Proxy.ts | 66 ++++++++--------------------------------------------- 1 file changed, 10 insertions(+), 56 deletions(-) (limited to 'src/fields/Proxy.ts') 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 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(this.cache); - return new ProxyField(this.fieldId); + this.fieldId = typeof value === "string" ? value : (value?.[Id] ?? ""); } - [ToScriptString]() { - return "invalid"; - } - [ToString]() { - return "ProxyField"; - } + [Copy]() { return new ProxyField((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; + private get cache(): T | undefined { return DocServer.GetCachedRefField(this.fieldId) as T } + value(): T | undefined | FieldWaiting { - 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 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; } } -- cgit v1.2.3-70-g09d2 From 7e3bb99b81dd9494c927abacc2ebf920282f3ccb Mon Sep 17 00:00:00 2001 From: bobzel Date: Fri, 17 Sep 2021 19:13:23 -0400 Subject: reverting last change. --- src/fields/Proxy.ts | 66 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 10 deletions(-) (limited to 'src/fields/Proxy.ts') diff --git a/src/fields/Proxy.ts b/src/fields/Proxy.ts index 07553f17c..62734d3d2 100644 --- a/src/fields/Proxy.ts +++ b/src/fields/Proxy.ts @@ -9,33 +9,72 @@ import { Id, Copy, ToScriptString, ToString } from "./FieldSymbols"; import { scriptingGlobal } from "../client/util/Scripting"; import { Plugins } from "./util"; -@Deserializable("proxy") +function deserializeProxy(field: any) { + if (!field.cache) { + field.cache = DocServer.GetCachedRefField(field.fieldId) as any; + } +} +@Deserializable("proxy", deserializeProxy) export class ProxyField extends ObjectField { constructor(); constructor(value: T); constructor(fieldId: string); constructor(value?: T | string) { super(); - this.fieldId = typeof value === "string" ? value : (value?.[Id] ?? ""); + 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(this.cache); + return new ProxyField(this.fieldId); } - [Copy]() { return new ProxyField((this.cache ?? this.fieldId) as T); } - [ToScriptString]() { return "invalid"; } - [ToString]() { return "ProxyField"; } + [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; - private get cache(): T | undefined { return DocServer.GetCachedRefField(this.fieldId) as T } - value(): T | undefined | FieldWaiting { - 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; })); @@ -44,7 +83,14 @@ export class ProxyField extends ObjectField { } promisedValue(): string { return !this.cache && !this.failed && !this.promise ? this.fieldId : ""; } setPromise(promise: any) { - if (this.cache === undefined) this.promise = promise; + this.promise = promise; + } + @action + setValue(field: any) { + this.promise = undefined; + this.cache = field; + if (field === undefined) this.failed = true; + return field; } } -- cgit v1.2.3-70-g09d2 From 75c47275853bfe422d6ae8a53be8ffe1996e1e76 Mon Sep 17 00:00:00 2001 From: bobzel Date: Fri, 17 Sep 2021 19:46:42 -0400 Subject: more conservative fix for previous. --- src/fields/Proxy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/fields/Proxy.ts') diff --git a/src/fields/Proxy.ts b/src/fields/Proxy.ts index 62734d3d2..f01b502c9 100644 --- a/src/fields/Proxy.ts +++ b/src/fields/Proxy.ts @@ -79,7 +79,7 @@ export class ProxyField extends ObjectField { return field; })); } - return this.promise as any; + return DocServer.GetCachedRefField(this.fieldId) ?? (this.promise as any); } promisedValue(): string { return !this.cache && !this.failed && !this.promise ? this.fieldId : ""; } setPromise(promise: any) { -- cgit v1.2.3-70-g09d2