aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/util.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/new_fields/util.ts')
-rw-r--r--src/new_fields/util.ts34
1 files changed, 27 insertions, 7 deletions
diff --git a/src/new_fields/util.ts b/src/new_fields/util.ts
index 47e467041..c6f693f7f 100644
--- a/src/new_fields/util.ts
+++ b/src/new_fields/util.ts
@@ -1,5 +1,5 @@
import { UndoManager } from "../client/util/UndoManager";
-import { Doc, Field } from "./Doc";
+import { Doc, Field, FieldResult } from "./Doc";
import { SerializationHelper } from "../client/util/SerializationHelper";
import { ProxyField } from "./Proxy";
import { RefField } from "./RefField";
@@ -11,6 +11,20 @@ import { ComputedField } from "./ScriptField";
function _readOnlySetter(): never {
throw new Error("Documents can't be modified in read-only mode");
}
+
+export interface GetterResult {
+ value: FieldResult;
+ shouldReturn?: boolean;
+}
+export type GetterPlugin = (receiver: any, prop: string | number, currentValue: any) => GetterResult | undefined;
+const getterPlugins: GetterPlugin[] = [];
+
+export namespace Plugins {
+ export function addGetterPlugin(plugin: GetterPlugin) {
+ getterPlugins.push(plugin);
+ }
+}
+
const _setterImpl = action(function (target: any, prop: string | symbol | number, value: any, receiver: any): boolean {
//console.log("-set " + target[SelfProxy].title + "(" + target[SelfProxy][prop] + ")." + prop.toString() + " = " + value);
if (SerializationHelper.IsSerializing()) {
@@ -74,6 +88,9 @@ export function setter(target: any, prop: string | symbol | number, value: any,
}
export function getter(target: any, prop: string | symbol | number, receiver: any): any {
+ if (prop === "then") {//If we're being awaited
+ return undefined;
+ }
if (typeof prop === "symbol") {
return target.__fields[prop] || target[prop];
}
@@ -85,12 +102,15 @@ export function getter(target: any, prop: string | symbol | number, receiver: an
function getFieldImpl(target: any, prop: string | number, receiver: any, ignoreProto: boolean = false): any {
receiver = receiver || target[SelfProxy];
- const field = target.__fields[prop];
- if (field instanceof ProxyField) {
- return field.value();
- }
- if (field instanceof ComputedField) {
- return field.value(receiver);
+ let field = target.__fields[prop];
+ for (const plugin of getterPlugins) {
+ const res = plugin(receiver, prop, field);
+ if (res === undefined) continue;
+ if (res.shouldReturn) {
+ return res.value;
+ } else {
+ field = res.value;
+ }
}
if (field === undefined && !ignoreProto && prop !== "proto") {
const proto = getFieldImpl(target, "proto", receiver, true);//TODO tfs: instead of receiver we could use target[SelfProxy]... I don't which semantics we want or if it really matters