aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/Proxy.ts
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2020-05-15 00:03:41 -0700
committerSam Wilkins <samwilkins333@gmail.com>2020-05-15 00:03:41 -0700
commit2b79008596351f6948d8de80c7887446d97b068c (patch)
tree7f393199bf780936cee1427c07c64bd2745fa3f9 /src/new_fields/Proxy.ts
parentb9440e34d89b28acacdd6eed2cda39cd2a1d8c46 (diff)
renamed new_fields to fields
Diffstat (limited to 'src/new_fields/Proxy.ts')
-rw-r--r--src/new_fields/Proxy.ts126
1 files changed, 0 insertions, 126 deletions
diff --git a/src/new_fields/Proxy.ts b/src/new_fields/Proxy.ts
deleted file mode 100644
index 555faaad0..000000000
--- a/src/new_fields/Proxy.ts
+++ /dev/null
@@ -1,126 +0,0 @@
-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/Scripting";
-import { Plugins } from "./util";
-
-@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.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);
- }
-
- [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>;
-
- 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;
- }));
- }
- return this.promise as any;
- }
- 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;
- }
-}
-
-export namespace ProxyField {
- let useProxy = true;
- export function DisableProxyFields() {
- useProxy = false;
- }
-
- export function EnableProxyFields() {
- useProxy = true;
- }
-
- export function WithoutProxy<T>(fn: () => T) {
- DisableProxyFields();
- try {
- return fn();
- } finally {
- EnableProxyFields();
- }
- }
-
- export function initPlugin() {
- Plugins.addGetterPlugin((doc, _, value) => {
- if (useProxy && value instanceof ProxyField) {
- return { value: value.value() };
- }
- });
- }
-}
-
-function prefetchValue(proxy: PrefetchProxy<RefField>) {
- return proxy.value() as any;
-}
-
-@scriptingGlobal
-@Deserializable("prefetch_proxy", prefetchValue)
-export class PrefetchProxy<T extends RefField> extends ProxyField<T> {
-}