aboutsummaryrefslogtreecommitdiff
path: root/src/fields/ObjectField.ts
blob: 231086262a71ab15fe413f2d54c25e4472d2c0ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { ScriptingGlobals } from '../client/util/ScriptingGlobals';
import { Copy, FieldChanged, Parent, ToJavascriptString, ToScriptString, ToString } from './FieldSymbols';
import { RefField } from './RefField';

export abstract class ObjectField {
    // prettier-ignore
    public [FieldChanged]?: (diff?: { op: '$addToSet' | '$remFromSet' | '$set'; 
                             // eslint-disable-next-line no-use-before-define
                             items: FieldType[] | undefined; 
                             length: number | undefined; 
                             hint?: any }, serverOp?: any) => void;
    // eslint-disable-next-line no-use-before-define
    public [Parent]?: RefField | ObjectField;
    abstract [Copy](): ObjectField;

    abstract [ToJavascriptString](): string;
    abstract [ToScriptString](): string;
    abstract [ToString](): string;
    static MakeCopy<T extends ObjectField>(field: T) {
        return field?.[Copy]();
    }
}
export type FieldType = number | string | boolean | ObjectField | RefField; // bcz: hack for now .. must match the type definition in Doc.ts .. put here to avoid import cycles

// eslint-disable-next-line import/no-mutable-exports
export let ObjGetRefField: (id: string, force?: boolean) => Promise<RefField | undefined>;
// eslint-disable-next-line import/no-mutable-exports
export let ObjGetRefFields: (ids: string[]) => Promise<{ [id: string]: RefField | undefined }>;

export function SetObjGetRefField(func: (id: string, force?: boolean) => Promise<RefField | undefined>) {
    ObjGetRefField = func;
}
export function SetObjGetRefFields(func: (ids: string[]) => Promise<{ [id: string]: RefField | undefined }>) {
    ObjGetRefFields = func;
}
ScriptingGlobals.add(ObjectField);