forked from mobxjs/mobx
/
array.ts
160 lines (140 loc) · 5.54 KB
/
array.ts
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
import { observable, IObservableArray, IArrayWillChange, IArrayWillSplice, IArrayChange, IArraySplice, action, intercept, observe } from "mobx"
import {
getMSTAdministration,
IJsonPatch,
maybeMST,
MSTAdministration,
valueToSnapshot
} from "../../core"
import { identity, nothing } from "../../utils"
import { IType, IComplexType, isType } from "../type"
import { ComplexType } from "./complex-type"
import { createDefaultValueFactory } from "../utility-types/with-default"
export class ArrayType<T> extends ComplexType<T[], IObservableArray<T>> {
isArrayFactory = true
subType: IType<any, any>
constructor(name: string, subType: IType<any, any>) {
super(name)
this.subType = subType
}
describe() {
return this.subType.describe() + "[]"
}
createNewInstance() {
return observable.shallowArray()
}
finalizeNewInstance(instance: IObservableArray<any>, snapshot: any) {
intercept(instance, change => this.willChange(change) as any)
observe(instance, this.didChange)
getMSTAdministration(instance).applySnapshot(snapshot)
}
getChildMSTs(node: MSTAdministration): [string, MSTAdministration][] {
const target = node.target as IObservableArray<any>
const res: [string, MSTAdministration][] = []
target.forEach((value, index) => {
maybeMST(value, childNode => { res.push(["" + index, childNode])})
})
return res
}
getChildMST(node: MSTAdministration, key: string): MSTAdministration | null {
const target = node.target as IObservableArray<any>
const index = parseInt(key, 10)
if (index < target.length)
return maybeMST(target[index], identity, nothing)
return null
}
willChange(change: IArrayWillChange<any> | IArrayWillSplice<any>): Object | null {
const node = getMSTAdministration(change.object)
node.assertWritable()
switch (change.type) {
case "update":
if (change.newValue === change.object[change.index])
return null
change.newValue = node.reconcileChildren(this.subType, [change.object[change.index]], [change.newValue], [change.index])[0]
break
case "splice":
const {index, removedCount, added, object} = change
change.added = node.reconcileChildren(
this.subType,
object.slice(index, index + removedCount),
added,
added.map((_, i) => index + i)
)
// update paths of remaining items
for (let i = index + removedCount; i < object.length; i++) {
maybeMST(object[i], child => {
child.setParent(node, "" + (i + added.length - removedCount))
})
}
break
}
return change
}
serialize(node: MSTAdministration): any {
const target = node.target as IObservableArray<any>
return target.map(valueToSnapshot)
}
didChange(this: {}, change: IArrayChange<any> | IArraySplice<any>): void {
const node = getMSTAdministration(change.object)
switch (change.type) {
case "update":
return void node.emitPatch({
op: "replace",
path: "" + change.index,
value: valueToSnapshot(change.newValue)
}, node)
case "splice":
for (let i = change.index + change.removedCount - 1; i >= change.index; i--)
node.emitPatch({
op: "remove",
path: "" + i
}, node)
for (let i = 0; i < change.addedCount; i++)
node.emitPatch({
op: "add",
path: "" + (change.index + i),
value: valueToSnapshot(change.added[i])
}, node)
return
}
}
applyPatchLocally(node: MSTAdministration, subpath: string, patch: IJsonPatch): void {
const target = node.target as IObservableArray<any>
const index = subpath === "-" ? target.length : parseInt(subpath)
switch (patch.op) {
case "replace":
target[index] = patch.value
break
case "add":
target.splice(index, 0, patch.value)
break
case "remove":
target.splice(index, 1)
break
}
}
@action applySnapshot(node: MSTAdministration, snapshot: any[]): void {
node.pseudoAction(() => {
const target = node.target as IObservableArray<any>
target.replace(snapshot)
})
}
getChildType(key: string): IType<any, any> {
return this.subType
}
isValidSnapshot(snapshot: any) {
return Array.isArray(snapshot) && snapshot.every(item => this.subType.is(item))
}
getDefaultSnapshot() {
return []
}
removeChild(node: MSTAdministration, subpath: string) {
node.target.splice(parseInt(subpath, 10), 1)
}
}
export function createArrayFactory<S, T>(subtype: IType<S, T>): IComplexType<S[], IObservableArray<T>> {
return createDefaultValueFactory(new ArrayType(subtype.name + "[]", subtype), [])
}
export function isArrayFactory<S, T>(type: any): type is IComplexType<S[], IObservableArray<T>> {
return isType(type) && (type as any).isArrayFactory === true
}