aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/List.ts
blob: 96018dafa4c0445a9f274368f476182326246efd (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { Deserializable, autoObject } from "../client/util/SerializationHelper";
import { Field, Update, Self, FieldResult } from "./Doc";
import { setter, getter, deleteProperty } from "./util";
import { serializable, alias, list } from "serializr";
import { observable, action } from "mobx";
import { ObjectField, OnUpdate, Copy } from "./ObjectField";
import { RefField } from "./RefField";
import { ProxyField } from "./Proxy";

const listHandlers: any = {
    /// Mutator methods
    copyWithin() {
        throw new Error("copyWithin not supported yet");
    },
    fill(value: any, start?: number, end?: number) {
        if (value instanceof RefField) {
            throw new Error("fill with RefFields not supported yet");
        }
        const res = this[Self].__fields.fill(value, start, end);
        this[Update]();
        return res;
    },
    pop(): any {
        const field = toRealField(this[Self].__fields.pop());
        this[Update]();
        return field;
    },
    push: action(function (this: any, ...items: any[]) {
        items = items.map(toObjectField);
        const res = this[Self].__fields.push(...items);
        this[Update]();
        return res;
    }),
    reverse() {
        const res = this[Self].__fields.reverse();
        this[Update]();
        return res;
    },
    shift() {
        const res = toRealField(this[Self].__fields.shift());
        this[Update]();
        return res;
    },
    sort(cmpFunc: any) {
        const res = this[Self].__fields.sort(cmpFunc ? (first: any, second: any) => cmpFunc(toRealField(first), toRealField(second)) : undefined);
        this[Update]();
        return res;
    },
    splice: action(function (this: any, start: number, deleteCount: number, ...items: any[]) {
        items = items.map(toObjectField);
        const res = this[Self].__fields.splice(start, deleteCount, ...items);
        this[Update]();
        return res.map(toRealField);
    }),
    unshift(...items: any[]) {
        items = items.map(toObjectField);
        const res = this[Self].__fields.unshift(...items);
        this[Update]();
        return res;

    },
    /// Accessor methods
    concat: action(function (this: any, ...items: any[]) {
        return this[Self].__fields.map(toRealField).concat(...items);
    }),
    includes(valueToFind: any, fromIndex: number) {
        const fields = this[Self].__fields;
        if (valueToFind instanceof RefField) {
            return fields.map(toRealField).includes(valueToFind, fromIndex);
        } else {
            return fields.includes(valueToFind, fromIndex);
        }
    },
    indexOf(valueToFind: any, fromIndex: number) {
        const fields = this[Self].__fields;
        if (valueToFind instanceof RefField) {
            return fields.map(toRealField).indexOf(valueToFind, fromIndex);
        } else {
            return fields.indexOf(valueToFind, fromIndex);
        }
    },
    join(separator: any) {
        return this[Self].__fields.map(toRealField).join(separator);
    },
    lastIndexOf(valueToFind: any, fromIndex: number) {
        const fields = this[Self].__fields;
        if (valueToFind instanceof RefField) {
            return fields.map(toRealField).lastIndexOf(valueToFind, fromIndex);
        } else {
            return fields.lastIndexOf(valueToFind, fromIndex);
        }
    },
    slice(begin: number, end: number) {
        return this[Self].__fields.slice(begin, end).map(toRealField);
    },

    /// Iteration methods
    entries() {
        return this[Self].__fields.map(toRealField).entries();
    },
    every(callback: any, thisArg: any) {
        return this[Self].__fields.map(toRealField).every(callback, thisArg);
        // TODO This is probably more efficient, but technically the callback can take the array, which would mean we would have to map the actual array anyway.
        // If we don't want to support the array parameter, we should use this version instead
        // return this[Self].__fields.every((element:any, index:number, array:any) => callback(toRealField(element), index, array), thisArg);
    },
    filter(callback: any, thisArg: any) {
        return this[Self].__fields.map(toRealField).filter(callback, thisArg);
        // TODO This is probably more efficient, but technically the callback can take the array, which would mean we would have to map the actual array anyway.
        // If we don't want to support the array parameter, we should use this version instead
        // return this[Self].__fields.filter((element:any, index:number, array:any) => callback(toRealField(element), index, array), thisArg);
    },
    find(callback: any, thisArg: any) {
        return this[Self].__fields.map(toRealField).find(callback, thisArg);
        // TODO This is probably more efficient, but technically the callback can take the array, which would mean we would have to map the actual array anyway.
        // If we don't want to support the array parameter, we should use this version instead
        // return this[Self].__fields.find((element:any, index:number, array:any) => callback(toRealField(element), index, array), thisArg);
    },
    findIndex(callback: any, thisArg: any) {
        return this[Self].__fields.map(toRealField).findIndex(callback, thisArg);
        // TODO This is probably more efficient, but technically the callback can take the array, which would mean we would have to map the actual array anyway.
        // If we don't want to support the array parameter, we should use this version instead
        // return this[Self].__fields.findIndex((element:any, index:number, array:any) => callback(toRealField(element), index, array), thisArg);
    },
    forEach(callback: any, thisArg: any) {
        return this[Self].__fields.map(toRealField).forEach(callback, thisArg);
        // TODO This is probably more efficient, but technically the callback can take the array, which would mean we would have to map the actual array anyway.
        // If we don't want to support the array parameter, we should use this version instead
        // return this[Self].__fields.forEach((element:any, index:number, array:any) => callback(toRealField(element), index, array), thisArg);
    },
    map(callback: any, thisArg: any) {
        return this[Self].__fields.map(toRealField).map(callback, thisArg);
        // TODO This is probably more efficient, but technically the callback can take the array, which would mean we would have to map the actual array anyway.
        // If we don't want to support the array parameter, we should use this version instead
        // return this[Self].__fields.map((element:any, index:number, array:any) => callback(toRealField(element), index, array), thisArg);
    },
    reduce(callback: any, initialValue: any) {
        return this[Self].__fields.map(toRealField).reduce(callback, initialValue);
        // TODO This is probably more efficient, but technically the callback can take the array, which would mean we would have to map the actual array anyway.
        // If we don't want to support the array parameter, we should use this version instead
        // return this[Self].__fields.reduce((acc:any, element:any, index:number, array:any) => callback(acc, toRealField(element), index, array), initialValue);
    },
    reduceRight(callback: any, initialValue: any) {
        return this[Self].__fields.map(toRealField).reduceRight(callback, initialValue);
        // TODO This is probably more efficient, but technically the callback can take the array, which would mean we would have to map the actual array anyway.
        // If we don't want to support the array parameter, we should use this version instead
        // return this[Self].__fields.reduceRight((acc:any, element:any, index:number, array:any) => callback(acc, toRealField(element), index, array), initialValue);
    },
    some(callback: any, thisArg: any) {
        return this[Self].__fields.map(toRealField).some(callback, thisArg);
        // TODO This is probably more efficient, but technically the callback can take the array, which would mean we would have to map the actual array anyway.
        // If we don't want to support the array parameter, we should use this version instead
        // return this[Self].__fields.some((element:any, index:number, array:any) => callback(toRealField(element), index, array), thisArg);
    },
    values() {
        return this[Self].__fields.map(toRealField).values();
    },
    [Symbol.iterator]() {
        return this[Self].__fields.map(toRealField).values();
    }
};

function toObjectField(field: Field) {
    return field instanceof RefField ? new ProxyField(field) : field;
}

function toRealField(field: Field) {
    return field instanceof ProxyField ? field.value() : field;
}

function listGetter(target: any, prop: string | number | symbol, receiver: any): any {
    if (listHandlers.hasOwnProperty(prop)) {
        return listHandlers[prop];
    }
    return getter(target, prop, receiver);
}

interface ListSpliceUpdate<T> {
    type: "splice";
    index: number;
    added: T[];
    removedCount: number;
}

interface ListIndexUpdate<T> {
    type: "update";
    index: number;
    newValue: T;
}

type ListUpdate<T> = ListSpliceUpdate<T> | ListIndexUpdate<T>;

type StoredType<T extends Field> = T extends RefField ? ProxyField<T> : T;

@Deserializable("list")
class ListImpl<T extends Field> extends ObjectField {
    constructor(fields: T[] = []) {
        super();
        const list = new Proxy<this>(this, {
            set: setter,
            get: listGetter,
            deleteProperty: deleteProperty,
            defineProperty: () => { throw new Error("Currently properties can't be defined on documents using Object.defineProperty"); },
        });
        (list as any).push(...fields);
        return list;
    }

    [key: number]: FieldResult<T>;

    @serializable(alias("fields", list(autoObject())))
    private get __fields() {
        return this.___fields;
    }

    private set __fields(value) {
        this.___fields = value;
    }

    [Copy]() {
        let copiedData = this[Self].__fields.map(f => f instanceof ObjectField ? f[Copy]() : f);
        let deepCopy = new ListImpl<T>(copiedData as any);
        return deepCopy;
    }

    // @serializable(alias("fields", list(autoObject())))
    @observable
    private ___fields: StoredType<T>[] = [];

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

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