| 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 |
ve.dm.TransactionSquasher = function VeDmTransactionSquasher( transaction ) { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
this.transaction = transaction.clone(); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
this.operations = this.transaction.operations; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
this.op = this.operations[ 0 ]; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
this.index = 0; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
this.offset = 0; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
this.globalOffset = 0; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
this.attributeOperations = {}; |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
OO.initClass( ve.dm.TransactionSquasher ); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
ve.dm.TransactionSquasher.static.equalItems = function ( item1, item2 ) { |
| 97 |
function stringifyItem( item ) { |
| 98 |
return JSON.stringify( item, ( key, value ) => key === 'changesSinceLoad' ? undefined : value ); |
| 99 |
} |
| 100 |
|
| 101 |
return stringifyItem( item1 ) === stringifyItem( item2 ); |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
ve.dm.TransactionSquasher.static.squash = function ( transactions ) { |
| 111 |
if ( transactions.length === 0 ) { |
| 112 |
throw new Error( 'Cannot squash empty transaction array' ); |
| 113 |
} |
| 114 |
const squasher = new ve.dm.TransactionSquasher( transactions[ 0 ] ); |
| 115 |
for ( let i = 1, iLen = transactions.length; i < iLen; i++ ) { |
| 116 |
squasher.squashIn( transactions[ i ] ); |
| 117 |
} |
| 118 |
return squasher.getTransaction(); |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
ve.dm.TransactionSquasher.prototype.getTransaction = function () { |
| 129 |
return this.transaction; |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
ve.dm.TransactionSquasher.prototype.squashIn = function ( tx ) { |
| 142 |
|
| 143 |
|
| 144 |
this.index = 0; |
| 145 |
this.offset = 0; |
| 146 |
this.globalOffset = 0; |
| 147 |
this.op = this.operations[ this.index ]; |
| 148 |
this.changes = []; |
| 149 |
this.oldChanges = []; |
| 150 |
this.readAttributes(); |
| 151 |
|
| 152 |
for ( let i = 0; i < tx.operations.length; i++ ) { |
| 153 |
const op = tx.operations[ i ]; |
| 154 |
if ( op.type === 'retain' ) { |
| 155 |
let retainLength = op.length; |
| 156 |
while ( retainLength > 0 ) { |
| 157 |
const consumed = this.processRetain( retainLength ); |
| 158 |
retainLength -= consumed; |
| 159 |
} |
| 160 |
} else if ( op.type === 'replace' ) { |
| 161 |
let items = JSON.parse( JSON.stringify( op.remove ) ); |
| 162 |
while ( items.length > 0 ) { |
| 163 |
const consumed = this.processRemove( items ); |
| 164 |
items.splice( 0, consumed ); |
| 165 |
} |
| 166 |
items = JSON.parse( JSON.stringify( op.insert ) ); |
| 167 |
while ( items.length > 0 ) { |
| 168 |
const consumed = this.processInsert( items ); |
| 169 |
items.splice( 0, consumed ); |
| 170 |
} |
| 171 |
} else if ( op.type === 'attribute' ) { |
| 172 |
this.processAttribute( op.key, op.from, op.to ); |
| 173 |
} else { |
| 174 |
throw new Error( 'Unknown operation type ' + op.type ); |
| 175 |
} |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
ve.dm.TransactionSquasher.prototype.processRetain = function ( maxLength ) { |
| 187 |
this.normalizePosition(); |
| 188 |
if ( !this.op ) { |
| 189 |
throw new Error( 'Past end of transaction' ); |
| 190 |
} |
| 191 |
|
| 192 |
if ( this.op.type === 'retain' ) { |
| 193 |
const len = Math.min( maxLength, this.op.length - this.offset ); |
| 194 |
this.offset += len; |
| 195 |
this.globalOffset += len; |
| 196 |
this.attributeOperations = {}; |
| 197 |
return len; |
| 198 |
} |
| 199 |
if ( this.op.type === 'replace' ) { |
| 200 |
|
| 201 |
const len = Math.min( maxLength, this.op.insert.length - this.offset ); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
this.offset += len; |
| 207 |
this.globalOffset += len; |
| 208 |
this.readAttributes(); |
| 209 |
return len; |
| 210 |
} |
| 211 |
if ( this.op.type === 'attribute' ) { |
| 212 |
|
| 213 |
this.index++; |
| 214 |
this.op = this.operations[ this.index ]; |
| 215 |
|
| 216 |
this.attributeOperations[ this.op.key ] = this.op; |
| 217 |
return 0; |
| 218 |
} |
| 219 |
throw new Error( 'Unknown op type: ' + this.op.type ); |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
ve.dm.TransactionSquasher.prototype.processRemove = function ( items ) { |
| 233 |
let tryUnsplit = false; |
| 234 |
|
| 235 |
this.normalizePosition(); |
| 236 |
if ( !this.op ) { |
| 237 |
throw new Error( 'Past end of transaction' ); |
| 238 |
} |
| 239 |
if ( this.op.type === 'retain' ) { |
| 240 |
this.splitIfInterior(); |
| 241 |
|
| 242 |
const len = Math.min( items.length, this.op.length ); |
| 243 |
this.op.length -= len; |
| 244 |
if ( this.op.length === 0 ) { |
| 245 |
tryUnsplit = true; |
| 246 |
|
| 247 |
this.operations.splice( this.index, 1 ); |
| 248 |
|
| 249 |
this.op = this.operations[ this.index ]; |
| 250 |
} |
| 251 |
const removal = items.slice( 0, len ); |
| 252 |
if ( this.offset === 0 && this.op && this.op.type === 'replace' ) { |
| 253 |
|
| 254 |
ve.batchSplice( |
| 255 |
this.op.remove, |
| 256 |
0, |
| 257 |
0, |
| 258 |
removal |
| 259 |
); |
| 260 |
} else { |
| 261 |
|
| 262 |
let replaceOp = this.operations[ this.index - 1 ]; |
| 263 |
if ( !replaceOp || replaceOp.type !== 'replace' ) { |
| 264 |
replaceOp = { type: 'replace', remove: [], insert: [] }; |
| 265 |
this.operations.splice( this.index, 0, replaceOp ); |
| 266 |
this.index++; |
| 267 |
} |
| 268 |
ve.batchSplice( |
| 269 |
replaceOp.remove, |
| 270 |
replaceOp.remove.length, |
| 271 |
0, |
| 272 |
removal |
| 273 |
); |
| 274 |
} |
| 275 |
if ( tryUnsplit ) { |
| 276 |
this.tryUnsplit(); |
| 277 |
} |
| 278 |
this.readAttributes(); |
| 279 |
return len; |
| 280 |
} |
| 281 |
if ( this.op.type === 'replace' ) { |
| 282 |
|
| 283 |
const len = Math.min( items.length, this.op.insert.length - this.offset ); |
| 284 |
|
| 285 |
const removal = items.slice( 0, len ); |
| 286 |
for ( let i = 0; i < len; i++ ) { |
| 287 |
if ( !this.constructor.static.equalItems( |
| 288 |
removal[ i ], |
| 289 |
this.op.insert[ this.offset + i ] |
| 290 |
) ) { |
| 291 |
throw new Error( 'Remove does not match insert' ); |
| 292 |
} |
| 293 |
} |
| 294 |
this.op.insert.splice( this.offset, len ); |
| 295 |
if ( this.op.remove.length === 0 && this.op.insert.length === 0 ) { |
| 296 |
|
| 297 |
this.operations.splice( this.index, 1 ); |
| 298 |
this.op = this.operations[ this.index ]; |
| 299 |
|
| 300 |
this.tryUnsplit(); |
| 301 |
} |
| 302 |
this.readAttributes(); |
| 303 |
return len; |
| 304 |
} |
| 305 |
if ( this.op.type === 'attribute' ) { |
| 306 |
|
| 307 |
this.changeElement( items[ 0 ], this.op.key, this.op.to, this.op.from ); |
| 308 |
this.operations.splice( this.index, 1 ); |
| 309 |
this.op = this.operations[ this.index ]; |
| 310 |
|
| 311 |
this.tryUnsplit(); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
throw new Error( 'Unknown operation type ' + this.op.type ); |
| 315 |
}; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
ve.dm.TransactionSquasher.prototype.processInsert = function ( items ) { |
| 332 |
this.normalizePosition(); |
| 333 |
if ( !this.op || this.op.type === 'retain' || this.op.type === 'attribute' ) { |
| 334 |
if ( this.op && this.op.type === 'retain' ) { |
| 335 |
|
| 336 |
this.splitIfInterior(); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
let replaceOp = this.operations[ this.index - 1 ]; |
| 341 |
if ( !replaceOp || replaceOp.type !== 'replace' ) { |
| 342 |
replaceOp = { type: 'replace', remove: [], insert: [] }; |
| 343 |
this.operations.splice( this.index, 0, replaceOp ); |
| 344 |
this.index++; |
| 345 |
|
| 346 |
} |
| 347 |
ve.batchSplice( |
| 348 |
replaceOp.insert, |
| 349 |
replaceOp.insert.length, |
| 350 |
0, |
| 351 |
items |
| 352 |
); |
| 353 |
this.globalOffset += items.length; |
| 354 |
this.attributeOperations = {}; |
| 355 |
return items.length; |
| 356 |
} |
| 357 |
if ( this.op.type === 'replace' ) { |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
ve.batchSplice( this.op.insert, this.offset, 0, items ); |
| 363 |
this.offset += items.length; |
| 364 |
this.globalOffset += items.length; |
| 365 |
this.attributeOperations = {}; |
| 366 |
return items.length; |
| 367 |
} |
| 368 |
throw new Error( 'Unknown operation type ' + this.op.type ); |
| 369 |
}; |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
ve.dm.TransactionSquasher.prototype.processAttribute = function ( key, from, to ) { |
| 383 |
this.normalizePosition(); |
| 384 |
if ( this.op && this.op.type === 'replace' && this.offset < this.op.insert.length ) { |
| 385 |
|
| 386 |
const item = this.op.insert[ this.offset ]; |
| 387 |
if ( !item || !item.type || item.type[ 0 ] === '/' ) { |
| 388 |
throw new Error( 'Expected open element' ); |
| 389 |
} |
| 390 |
this.changeElement( this.op.insert[ this.offset ], key, from, to ); |
| 391 |
} else { |
| 392 |
let op = this.attributeOperations[ key ]; |
| 393 |
if ( op ) { |
| 394 |
if ( !this.constructor.static.equalItems( op.to, from ) ) { |
| 395 |
throw new Error( 'Unexpected prior attribute value' ); |
| 396 |
} |
| 397 |
|
| 398 |
op.to = to; |
| 399 |
} else { |
| 400 |
op = { |
| 401 |
type: 'attribute', |
| 402 |
key: key, |
| 403 |
from: from, |
| 404 |
to: to |
| 405 |
}; |
| 406 |
this.splitIfInterior(); |
| 407 |
this.operations.splice( this.index, 0, op ); |
| 408 |
this.attributeOperations[ key ] = op; |
| 409 |
this.index++; |
| 410 |
} |
| 411 |
} |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
ve.dm.TransactionSquasher.prototype.changeElement = function ( openElement, key, from, to ) { |
| 424 |
if ( !openElement.attributes ) { |
| 425 |
openElement.attributes = {}; |
| 426 |
} |
| 427 |
if ( !this.constructor.static.equalItems( |
| 428 |
openElement.attributes[ key ], |
| 429 |
from |
| 430 |
) ) { |
| 431 |
throw new Error( 'Unexpected prior attribute value' ); |
| 432 |
} |
| 433 |
if ( to === undefined ) { |
| 434 |
delete openElement.attributes[ key ]; |
| 435 |
if ( !Object.keys( openElement.attributes ).length ) { |
| 436 |
delete openElement.attributes; |
| 437 |
} |
| 438 |
} else { |
| 439 |
openElement.attributes[ key ] = to; |
| 440 |
} |
| 441 |
}; |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
ve.dm.TransactionSquasher.prototype.normalizePosition = function () { |
| 449 |
while ( this.op && ( |
| 450 |
( this.op.type === 'retain' && this.offset === this.op.length ) || |
| 451 |
( this.op.type === 'replace' && this.offset === this.op.insert.length ) |
| 452 |
) ) { |
| 453 |
this.index++; |
| 454 |
this.offset = 0; |
| 455 |
|
| 456 |
this.op = this.operations[ this.index ]; |
| 457 |
this.readAttributes(); |
| 458 |
} |
| 459 |
}; |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
ve.dm.TransactionSquasher.prototype.readAttributes = function () { |
| 470 |
let index = this.index, |
| 471 |
op = this.operations[ index ], |
| 472 |
offset = this.offset; |
| 473 |
|
| 474 |
this.attributeOperations = {}; |
| 475 |
while ( true ) { |
| 476 |
if ( !op ) { |
| 477 |
return; |
| 478 |
} |
| 479 |
if ( op.type === 'replace' ) { |
| 480 |
if ( offset < op.insert.length ) { |
| 481 |
|
| 482 |
return; |
| 483 |
} |
| 484 |
} else if ( op.type === 'attribute' ) { |
| 485 |
this.attributeOperations[ op.key ] = op; |
| 486 |
} else if ( op.type === 'retain' && offset < op.length ) { |
| 487 |
return; |
| 488 |
} |
| 489 |
|
| 490 |
index++; |
| 491 |
offset = 0; |
| 492 |
op = this.operations[ index ]; |
| 493 |
} |
| 494 |
}; |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
ve.dm.TransactionSquasher.prototype.splitIfInterior = function () { |
| 507 |
const type = this.op && this.op.type; |
| 508 |
if ( this.offset === 0 ) { |
| 509 |
|
| 510 |
return; |
| 511 |
} |
| 512 |
if ( type !== 'retain' ) { |
| 513 |
throw new Error( 'Non-zero offset, but op type is ' + type ); |
| 514 |
} |
| 515 |
const len = this.op.length; |
| 516 |
const remainder = len - this.offset; |
| 517 |
if ( remainder < 0 ) { |
| 518 |
throw new Error( 'Beyond the end of retain' ); |
| 519 |
} |
| 520 |
if ( remainder === 0 ) { |
| 521 |
throw new Error( 'Cannot split at the end of retain' ); |
| 522 |
} |
| 523 |
this.op.length = this.offset; |
| 524 |
this.index++; |
| 525 |
this.offset = 0; |
| 526 |
this.op = { type: 'retain', length: remainder }; |
| 527 |
this.operations.splice( this.index, 0, this.op ); |
| 528 |
}; |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
ve.dm.TransactionSquasher.prototype.tryUnsplit = function () { |
| 536 |
const prevOp = this.operations[ this.index - 1 ]; |
| 537 |
if ( !this.op || !prevOp ) { |
| 538 |
return; |
| 539 |
} |
| 540 |
if ( prevOp.type === 'retain' && this.op.type === 'retain' ) { |
| 541 |
this.offset += prevOp.length; |
| 542 |
prevOp.length += this.op.length; |
| 543 |
this.operations.splice( this.index, 1 ); |
| 544 |
this.index--; |
| 545 |
this.op = prevOp; |
| 546 |
} else if ( prevOp.type === 'replace' && this.op.type === 'replace' ) { |
| 547 |
this.offset += prevOp.insert.length; |
| 548 |
ve.batchSplice( |
| 549 |
prevOp.remove, |
| 550 |
prevOp.remove.length, |
| 551 |
0, |
| 552 |
this.op.remove |
| 553 |
); |
| 554 |
ve.batchSplice( |
| 555 |
prevOp.insert, |
| 556 |
prevOp.insert.length, |
| 557 |
0, |
| 558 |
this.op.insert |
| 559 |
); |
| 560 |
this.operations.splice( this.index, 1 ); |
| 561 |
this.index--; |
| 562 |
this.op = prevOp; |
| 563 |
} |
| 564 |
|
| 565 |
}; |