-
Notifications
You must be signed in to change notification settings - Fork 4
/
Patch.js
207 lines (191 loc) · 6.42 KB
/
Patch.js
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
// TODO: NOTE:
// patch data should be:
// use immutable update
// have NAME_KEY, NAME_MODIFY_TIME
const createPatchKit = ({
NAME_KEY = 'key',
NAME_MODIFY_TIME = 'mtime',
EPSILON_MODIFY_TIME = 1,
isChanged = (data, prevData) => data[ NAME_MODIFY_TIME ] !== prevData[ NAME_MODIFY_TIME ],
shouldApplyDelete = (data, prevData) => data[ NAME_MODIFY_TIME ] >= prevData[ NAME_MODIFY_TIME ], // since delete will use last mtime, so should allow `=` check
shouldApplyUpdate = (data, prevData) => data[ NAME_MODIFY_TIME ] > prevData[ NAME_MODIFY_TIME ], // only allow bigger mtime
getPatchData = (data, prevData) => data[ NAME_MODIFY_TIME ] < prevData[ NAME_MODIFY_TIME ]
? { ...data, [ NAME_MODIFY_TIME ]: prevData[ NAME_MODIFY_TIME ] + EPSILON_MODIFY_TIME } // use old data as future data, step NAME_MODIFY_TIME up for EPSILON_MODIFY_TIME
: data // normal new data
}) => {
const hasPatch = (
dataMap, // { NAME_KEY: { NAME_KEY, NAME_MODIFY_TIME } } // TODO: NOTE: will mutate this map, pass new map in every time, do not reuse
prevDataList // [ { NAME_KEY, NAME_MODIFY_TIME } ]
) => {
for (let index = 0, indexMax = prevDataList.length; index < indexMax; index++) {
const prevData = prevDataList[ index ]
const key = prevData[ NAME_KEY ]
const data = dataMap.get(key)
if (data === undefined) return true
else if (isChanged(data, prevData)) return true
dataMap.delete(key)
}
return dataMap.size >= 1
}
const countPatch = (
dataMap, // { NAME_KEY: { NAME_KEY, NAME_MODIFY_TIME } } // TODO: NOTE: will mutate this map, pass new map in every time, do not reuse
prevDataList // [ { NAME_KEY, NAME_MODIFY_TIME } ]
) => {
let count = 0
for (let index = 0, indexMax = prevDataList.length; index < indexMax; index++) {
const prevData = prevDataList[ index ]
const key = prevData[ NAME_KEY ]
const data = dataMap.get(key)
if (data === undefined) count++
else if (isChanged(data, prevData)) count++
dataMap.delete(key)
}
count += dataMap.size
return count
}
const generatePatch = (
dataMap, // { NAME_KEY: { NAME_KEY, NAME_MODIFY_TIME } } // TODO: NOTE: will mutate this map, pass new map in every time, do not reuse
prevDataList // [ { NAME_KEY, NAME_MODIFY_TIME } ]
) => {
const deleteList = []
const updateList = []
for (let index = 0, indexMax = prevDataList.length; index < indexMax; index++) {
const prevData = prevDataList[ index ]
const key = prevData[ NAME_KEY ]
const data = dataMap.get(key)
if (data === undefined) deleteList.push({ [ NAME_KEY ]: key, [ NAME_MODIFY_TIME ]: prevData[ NAME_MODIFY_TIME ] }) // delete, pass NAME_KEY & NAME_MODIFY_TIME only
else if (isChanged(data, prevData)) updateList.push(getPatchData(data, prevData)) // update
dataMap.delete(key)
}
dataMap.forEach((data) => {
updateList.push(data) // add
})
return { deleteList, updateList }
}
const applyPatch = (
deleteList, // [ { NAME_KEY, NAME_MODIFY_TIME } ]
updateList, // [ { NAME_KEY, NAME_MODIFY_TIME } ]
dataList, // [ { NAME_KEY, NAME_MODIFY_TIME } ]
onData // (data, prevData) => {}
) => {
let isChanged = false
const deleteMap = deleteList.reduce(arrayMapReducer, new Map())
const updateMap = updateList.reduce(arrayMapReducer, new Map())
for (const data of dataList) {
const key = data[ NAME_KEY ]
const deleteData = deleteMap.get(key)
if (deleteData !== undefined && shouldApplyDelete(deleteData, data)) {
onData(undefined, data) // delete by drop
isChanged = true
continue
}
const updateData = updateMap.get(key)
if (updateData !== undefined) {
updateMap.delete(key)
if (shouldApplyUpdate(updateData, data)) {
onData(updateData, data) // update by replace
isChanged = true
continue
}
}
onData(data, data) // keep
}
updateMap.forEach((data) => {
onData(data, undefined) // update by append
isChanged = true
})
return isChanged
}
const arrayMapReducer = (o, value) => o.set(value[ NAME_KEY ], value)
return {
NAME_KEY,
NAME_MODIFY_TIME,
isChanged,
shouldApplyDelete,
shouldApplyUpdate,
hasPatch,
countPatch,
generatePatch,
applyPatch,
arrayMapReducer
}
}
const toObjectPatchKit = (patchKit) => {
const {
NAME_KEY,
hasPatch,
countPatch,
generatePatch,
applyPatch
} = patchKit
const hasObjectPatch = (object, prevObject) => hasPatch(
new Map(Object.entries(object)), // TODO: still cost a lot
Object.values(prevObject) // TODO: still cost a lot
)
const countObjectPatch = (object, prevObject) => countPatch(
new Map(Object.entries(object)),
Object.values(prevObject)
)
const generateObjectPatch = (object, prevObject) => generatePatch(
new Map(Object.entries(object)),
Object.values(prevObject)
)
const applyObjectPatch = (object, { deleteList, updateList }) => {
const nextObject = {}
const isChanged = applyPatch(
deleteList,
updateList,
Object.values(object),
(data, prevData) => { if (data !== undefined) nextObject[ data[ NAME_KEY ] ] = data }
)
return isChanged ? nextObject : object
}
return {
hasObjectPatch,
countObjectPatch,
generateObjectPatch,
applyObjectPatch
}
}
const toArrayWithKeyPatchKit = (patchKit) => {
const {
hasPatch,
countPatch,
generatePatch,
applyPatch,
arrayMapReducer
} = patchKit
const hasArrayWithKeyPatch = (array, prevArray) => hasPatch(
array.reduce(arrayMapReducer, new Map()), // TODO: still cost a lot
prevArray
)
const countArrayWithKeyPatch = (array, prevArray) => countPatch(
array.reduce(arrayMapReducer, new Map()),
prevArray
)
const generateArrayWithKeyPatch = (array, prevArray) => generatePatch(
array.reduce(arrayMapReducer, new Map()),
prevArray
)
const applyArrayWithKeyPatch = (array, { deleteList, updateList }) => {
const nextArray = []
const isChanged = applyPatch(
deleteList,
updateList,
array,
(data, prevData) => { if (data !== undefined) nextArray.push(data) }
)
return isChanged ? nextArray : array
}
return {
hasArrayWithKeyPatch,
countArrayWithKeyPatch,
generateArrayWithKeyPatch,
applyArrayWithKeyPatch
}
}
export {
createPatchKit,
toObjectPatchKit,
toArrayWithKeyPatchKit
}