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 | 1× 1× 1× 1732× 1× 1731× 1731× 1723× 1× 1722× 1722× 1722× 2× 1487× 4× 1483× 1332× 1× 1331× 1331× 151× 1× 150× 1× 149× 149× 38× 38× 218× 218× 217× 59× 218× 10× 674× 674× 668× 668× 668× 177× 3× 269× 293× 8× 66× 1× 1× 1× 11× 9× 5× 4× 1× 7× 5× 2× 1× 1× 1× 4× 2× 2× 2× 2× 1× 1× 1× 6× 3× 3× 3× 6× 4× 2× 1× 20× 20× 1× 19× 16× 16× 19× 17× 7× 10× 4× 6× 19× 1× 1× 1× 1148× 1× 83× 1× 236× 1× 1× 1× | import { cloneDeep, isEqual, isNumber } from '../util' import Conflict from './Conflict' const NOP = "NOP" const DELETE = "delete" const INSERT = "insert" class ArrayOperation { constructor(data) { if (!data || !data.type) { throw new Error("Illegal argument: insufficient data.") } this.type = data.type if (this.type === NOP) return if (this.type !== INSERT && this.type !== DELETE) { throw new Error("Illegal type.") } // the position where to apply the operation this.pos = data.pos // the value to insert or delete this.val = data.val if (!isNumber(this.pos) || this.pos < 0) { throw new Error("Illegal argument: expecting positive number as pos.") } } apply(array) { if (this.type === NOP) { return array } if (this.type === INSERT) { if (array.length < this.pos) { throw new Error("Provided array is too small.") } array.splice(this.pos, 0, this.val) return array } // Delete else /* if (this.type === DELETE) */ { if (array.length < this.pos) { throw new Error("Provided array is too small.") } if (!isEqual(array[this.pos], this.val)) { throw Error("Unexpected value at position " + this.pos + ". Expected " + this.val + ", found " + array[this.pos]) } array.splice(this.pos, 1) return array } } clone() { var data = { type: this.type, pos: this.pos, val: cloneDeep(this.val) } return new ArrayOperation(data) } invert() { var data = this.toJSON() if (this.type === NOP) data.type = NOP else if (this.type === INSERT) data.type = DELETE else /* if (this.type === DELETE) */ data.type = INSERT return new ArrayOperation(data) } hasConflict(other) { return ArrayOperation.hasConflict(this, other) } toJSON() { var result = { type: this.type, } if (this.type === NOP) return result result.pos = this.pos result.val = cloneDeep(this.val) return result } isInsert() { return this.type === INSERT } isDelete() { return this.type === DELETE } getOffset() { return this.pos } getValue() { return this.val } isNOP() { return this.type === NOP } toString() { return ["(", (this.isInsert() ? INSERT : DELETE), ",", this.getOffset(), ",'", this.getValue(), "')"].join('') } } ArrayOperation.prototype._isOperation = true ArrayOperation.prototype._isArrayOperation = true function hasConflict(a, b) { if (a.type === NOP || b.type === NOP) return false if (a.type === INSERT && b.type === INSERT) { return a.pos === b.pos } else { return false } } function transform_insert_insert(a, b) { if (a.pos === b.pos) { b.pos += 1 } // a before b else if (a.pos < b.pos) { b.pos += 1 } // a after b else { a.pos += 1 } } function transform_delete_delete(a, b) { // turn the second of two concurrent deletes into a NOP if (a.pos === b.pos) { b.type = NOP a.type = NOP return } if (a.pos < b.pos) { b.pos -= 1 } else { a.pos -= 1 } } function transform_insert_delete(a, b) { // reduce to a normalized case if (a.type === DELETE) { var tmp = a a = b b = tmp } if (a.pos <= b.pos) { b.pos += 1 } else { a.pos -= 1 } } var transform = function(a, b, options) { options = options || {} // enable conflicts when you want to notify the user of potential problems // Note that even in these cases, there is a defined result. if (options['no-conflict'] && hasConflict(a, b)) { throw new Conflict(a, b) } // this is used internally only as optimization, e.g., when rebasing an operation if (!options.inplace) { a = a.clone() b = b.clone() } if (a.type === NOP || b.type === NOP) { // nothing to transform } else if (a.type === INSERT && b.type === INSERT) { transform_insert_insert(a, b) } else if (a.type === DELETE && b.type === DELETE) { transform_delete_delete(a, b) } else { transform_insert_delete(a, b) } return [a, b] } ArrayOperation.transform = transform ArrayOperation.hasConflict = hasConflict /* Factories */ ArrayOperation.Insert = function(pos, val) { return new ArrayOperation({type:INSERT, pos: pos, val: val}) } ArrayOperation.Delete = function(pos, val) { return new ArrayOperation({ type:DELETE, pos: pos, val: val }) } ArrayOperation.fromJSON = function(data) { return new ArrayOperation(data) } ArrayOperation.NOP = NOP ArrayOperation.DELETE = DELETE ArrayOperation.INSERT = INSERT // Export // ======== export default ArrayOperation |