|
| 1 | +export enum OpType { |
| 2 | + // 0 = reuse |
| 3 | + // 1 = use new item |
| 4 | + Reuse, |
| 5 | + New, |
| 6 | +} |
| 7 | + |
| 8 | +export type Op = { |
| 9 | + type: OpType |
| 10 | + index: number |
| 11 | +} |
| 12 | + |
| 13 | +export enum OpsType { |
| 14 | + // 0 = error |
| 15 | + // 1 = success |
| 16 | + // 2 = success but should skip |
| 17 | + Error, |
| 18 | + Success, |
| 19 | + ShouldSkip, |
| 20 | +} |
| 21 | + |
| 22 | +export type Ops = { |
| 23 | + type: OpsType |
| 24 | + ops: Op[] |
| 25 | + message?: string |
| 26 | +} |
| 27 | + |
| 28 | +// as an example, use diff to patch data |
| 29 | +export const patch = <T>(ops: ReadonlyArray<Op>, oldList: ReadonlyArray<T>, newList: ReadonlyArray<T>) => { |
| 30 | + if (!oldList.length) { |
| 31 | + return newList |
| 32 | + } |
| 33 | + |
| 34 | + return newList.map((data, i) => { |
| 35 | + const op = ops[i] |
| 36 | + |
| 37 | + if (op.type === OpType.Reuse) { |
| 38 | + return oldList[op.index] |
| 39 | + } |
| 40 | + |
| 41 | + return data |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +export const getPatchResult = <T>(oldList: ReadonlyArray<T>, newList: ReadonlyArray<T>, ops: Ops): ReadonlyArray<T> => { |
| 46 | + switch (ops.type) { |
| 47 | + case OpsType.Error: |
| 48 | + return newList |
| 49 | + case OpsType.ShouldSkip: |
| 50 | + return oldList |
| 51 | + case OpsType.Success: |
| 52 | + default: |
| 53 | + return patch(ops.ops, oldList, newList) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function fastEqual(left: object, right: object) { |
| 58 | + if (left === right) { |
| 59 | + return true |
| 60 | + } |
| 61 | + |
| 62 | + if (left && right && typeof left == 'object' && typeof right == 'object') { |
| 63 | + const isLeftArray = Array.isArray(left) |
| 64 | + const isRightArray = Array.isArray(right) |
| 65 | + |
| 66 | + if (isLeftArray && isRightArray) { |
| 67 | + const length = (left as any[]).length |
| 68 | + |
| 69 | + if (length != (right as any[]).length) { |
| 70 | + return false |
| 71 | + } |
| 72 | + |
| 73 | + for (let i = length; i-- !== 0; ) { |
| 74 | + if (!fastEqual(left[i], right[i])) { |
| 75 | + return false |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return true |
| 80 | + } |
| 81 | + |
| 82 | + if (isLeftArray !== isRightArray) { |
| 83 | + return false |
| 84 | + } |
| 85 | + |
| 86 | + const isLeftDate = left instanceof Date |
| 87 | + const isRightDate = right instanceof Date |
| 88 | + |
| 89 | + if (isLeftDate != isRightDate) { |
| 90 | + return false |
| 91 | + } |
| 92 | + |
| 93 | + if (isLeftDate && isRightDate) { |
| 94 | + return (left as Date).getTime() == (right as Date).getTime() |
| 95 | + } |
| 96 | + |
| 97 | + const keys = Object.keys(left) |
| 98 | + const LeftLen = keys.length |
| 99 | + |
| 100 | + if (LeftLen !== Object.keys(right).length) { |
| 101 | + return false |
| 102 | + } |
| 103 | + |
| 104 | + for (let k = LeftLen; k-- !== 0; ) { |
| 105 | + if (!right.hasOwnProperty(keys[k])) { |
| 106 | + return false |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + for (let j = LeftLen; j-- !== 0; ) { |
| 111 | + const key = keys[j] |
| 112 | + if (!fastEqual(left[key], right[key])) { |
| 113 | + return false |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return true |
| 118 | + } |
| 119 | + |
| 120 | + return left !== left && right !== right |
| 121 | +} |
| 122 | + |
| 123 | +export function diff<T>(oldList: ReadonlyArray<T>, newList: ReadonlyArray<T>, pk = '_id'): Ops { |
| 124 | + const prev = oldList |
| 125 | + const curr = newList |
| 126 | + |
| 127 | + if (!Array.isArray(prev) || !Array.isArray(curr)) { |
| 128 | + return { |
| 129 | + type: OpsType.Error, |
| 130 | + ops: [], |
| 131 | + message: `cannot compare non-list object`, |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + const index = {} |
| 136 | + for (let i = 0; i < prev.length; i++) { |
| 137 | + const value = prev[i][pk] |
| 138 | + if (value === undefined) { |
| 139 | + return { |
| 140 | + type: OpsType.Error, |
| 141 | + ops: [], |
| 142 | + message: `cannot find pk: ${pk} at prev.${i}`, |
| 143 | + } |
| 144 | + } |
| 145 | + index[value] = i |
| 146 | + } |
| 147 | + |
| 148 | + const ret: Op[] = [] |
| 149 | + let reused = 0 |
| 150 | + |
| 151 | + for (let k = 0; k < curr.length; k++) { |
| 152 | + const key = curr[k][pk] |
| 153 | + if (key === undefined) { |
| 154 | + return { |
| 155 | + type: OpsType.Error, |
| 156 | + ops: [], |
| 157 | + message: `cannot find pk: ${pk} at curr.${k}`, |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + const prevIndex = index[key] |
| 162 | + |
| 163 | + if (prevIndex !== undefined) { |
| 164 | + const isEqual = fastEqual((curr as any)[k], (prev as any)[prevIndex]) |
| 165 | + // if equal then reuse the previous data otherwise use the new data |
| 166 | + const op: Op = isEqual ? { type: OpType.Reuse, index: prevIndex } : { type: OpType.New, index: k } |
| 167 | + |
| 168 | + if (prevIndex === k && isEqual) { |
| 169 | + reused++ |
| 170 | + } |
| 171 | + ret.push(op) |
| 172 | + } else { |
| 173 | + ret.push({ type: OpType.New, index: k }) |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + const arrayIsSame = reused === curr.length && prev.length === curr.length |
| 178 | + return { |
| 179 | + type: arrayIsSame ? OpsType.ShouldSkip : OpsType.Success, |
| 180 | + ops: ret, |
| 181 | + } |
| 182 | +} |
0 commit comments