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.ts64
1 files changed, 25 insertions, 39 deletions
diff --git a/src/fields/Proxy.ts b/src/fields/Proxy.ts
index 48c336e60..21b9fe89b 100644
--- a/src/fields/Proxy.ts
+++ b/src/fields/Proxy.ts
@@ -16,9 +16,26 @@ function deserializeProxy(field: serializedProxyType) {
}
@Deserializable('proxy', (obj: unknown) => deserializeProxy(obj as serializedProxyType))
export class ProxyField<T extends Doc> extends ObjectField {
+ @serializable(primitive())
+ readonly fieldId: string = '';
+
+ private failed = false;
+
+ // 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; p: FieldWaiting<T> | undefined } = { field: undefined, p: undefined };
+ private get cache(): { field: T | undefined; p: FieldWaiting<T> | undefined } {
+ return this._cache;
+ }
+ private set cache(val: { field: T | undefined; p: FieldWaiting<T> | undefined }) {
+ runInAction(() => (this._cache = { ...val }));
+ }
+
constructor();
constructor(value: T);
constructor(fieldId: string);
+ constructor(value: T | string);
constructor(value?: T | string) {
super();
if (typeof value === 'string') {
@@ -30,13 +47,12 @@ export class ProxyField<T extends Doc> extends ObjectField {
}
}
- [ToValue](/* doc: any */) {
+ [ToValue]() {
return ProxyField.toValue(this);
}
[Copy]() {
- if (this.cache.field) return new ProxyField<T>(this.cache.field);
- return new ProxyField<T>(this.fieldId);
+ return new ProxyField<T>(this.cache.field ?? this.fieldId);
}
[ToJavascriptString]() {
@@ -46,27 +62,9 @@ export class ProxyField<T extends Doc> extends ObjectField {
return Field.toScriptString(this[ToValue]()?.value as T); // not sure this is quite right since it doesn't recreate a proxy field, but better than 'invalid' ?
}
[ToString]() {
- return Field.toString(this[ToValue]()?.value);
+ return Field.toString(this[ToValue]()?.value as T);
}
- @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; p: FieldWaiting<T> | undefined } = { field: undefined, p: undefined };
- private get cache(): { field: T | undefined; p: FieldWaiting<T> | undefined } {
- return this._cache;
- }
- private set cache(val: { field: T | undefined; p: FieldWaiting<T> | undefined }) {
- runInAction(() => {
- this._cache = { ...val };
- });
- }
-
- private failed = false;
-
@computed get value(): T | undefined | FieldWaiting<T> {
if (this.cache.field) return this.cache.field;
if (this.failed) return undefined;
@@ -94,32 +92,19 @@ export class ProxyField<T extends Doc> extends ObjectField {
return field;
}
}
-
-// eslint-disable-next-line no-redeclare, @typescript-eslint/no-namespace
export namespace ProxyField {
let useProxy = true;
- export function DisableProxyFields() {
+ export function DisableDereference<T>(fn: () => T) {
useProxy = false;
- }
-
- export function EnableProxyFields() {
- useProxy = true;
- }
-
- export function WithoutProxy<T>(fn: () => T) {
- DisableProxyFields();
try {
return fn();
} finally {
- EnableProxyFields();
+ useProxy = true;
}
}
export function toValue(value: { value: unknown }) {
- if (useProxy) {
- return { value: value.value };
- }
- return undefined;
+ return useProxy ? { value: value.value } : undefined;
}
}
@@ -129,5 +114,6 @@ function prefetchValue(proxy: PrefetchProxy<Doc>) {
}
@scriptingGlobal
-@Deserializable('prefetch_proxy', (obj:unknown) => prefetchValue(obj as PrefetchProxy<Doc>))
+// eslint-disable-next-line no-use-before-define
+@Deserializable('prefetch_proxy', (obj: unknown) => prefetchValue(obj as PrefetchProxy<Doc>))
export class PrefetchProxy<T extends Doc> extends ProxyField<T> {}