aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/List.ts
blob: a1a623f83c510b3cc188874eb38cede80e09158d (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
import { Deserializable, autoObject } from "../client/util/SerializationHelper";
import { Field, ObjectField, Update, OnUpdate, Self } from "./Doc";
import { setter, getter } from "./util";
import { serializable, alias, list } from "serializr";
import { observable } from "mobx";

@Deserializable("list")
class ListImpl<T extends Field> extends ObjectField {
    constructor() {
        super();
        const list = new Proxy<this>(this, {
            set: function (a, b, c, d) { return setter(a, b, c, d); },
            get: getter,
            deleteProperty: () => { throw new Error("Currently properties can't be deleted from documents, assign to undefined instead"); },
            defineProperty: () => { throw new Error("Currently properties can't be defined on documents using Object.defineProperty"); },
        });
        return list;
    }

    [key: number]: T | null | undefined;

    @serializable(alias("fields", list(autoObject())))
    @observable
    private __fields: (T | null | undefined)[] = [];

    private [Update] = (diff: any) => {
        console.log(diff);
        const update = this[OnUpdate];
        update && update(diff);
    }

    private [Self] = this;
}
export type List<T extends Field> = ListImpl<T> & T[];
export const List: { new <T extends Field>(): List<T> } = ListImpl as any;