From f35c056bde056c92161054635b47ea7c0a734e43 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Mon, 26 Feb 2018 03:23:22 +0100 Subject: [PATCH] fix some tests --- rollup.test.js | 2 +- src/Type/YArray.js | 44 +++++++++++++++++++++++--------------------- test/index.js | 1 + 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/rollup.test.js b/rollup.test.js index 7e948a42..f4072276 100644 --- a/rollup.test.js +++ b/rollup.test.js @@ -3,7 +3,7 @@ import commonjs from 'rollup-plugin-commonjs' import multiEntry from 'rollup-plugin-multi-entry' export default { - input: 'test/y-text.tests.js', + input: 'test/index.js', name: 'y-tests', sourcemap: true, output: { diff --git a/src/Type/YArray.js b/src/Type/YArray.js index 90835566..b017266b 100644 --- a/src/Type/YArray.js +++ b/src/Type/YArray.js @@ -225,29 +225,31 @@ export default class YArray extends Type { }) } insert (pos, content) { - let left = null - let right = this._start - let count = 0 - const y = this._y - while (right !== null) { - const rightLen = right._deleted ? 0 : (right._length - 1) - if (count <= pos && pos <= count + rightLen) { - const splitDiff = pos - count - right = right._splitAt(y, splitDiff) - left = right._left - count += splitDiff - break + this._transact(() => { + let left = null + let right = this._start + let count = 0 + const y = this._y + while (right !== null) { + const rightLen = right._deleted ? 0 : (right._length - 1) + if (count <= pos && pos <= count + rightLen) { + const splitDiff = pos - count + right = right._splitAt(y, splitDiff) + left = right._left + count += splitDiff + break + } + if (!right._deleted) { + count += right._length + } + left = right + right = right._right } - if (!right._deleted) { - count += right._length + if (pos > count) { + throw new Error('Position exceeds array range!') } - left = right - right = right._right - } - if (pos > count) { - throw new Error('Position exceeds array range!') - } - this.insertAfter(left, content) + this.insertAfter(left, content) + }) } push (content) { let n = this._start diff --git a/test/index.js b/test/index.js index f59d2a8b..1ef3afc8 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,6 @@ import './red-black-tree.js' import './y-array.tests.js' +import './y-text.tests.js' import './y-map.tests.js' import './y-xml.tests.js' import './encode-decode.tests.js'