From: Evert Timberg Date: Sat, 14 Aug 2021 13:07:58 +0000 (-0400) Subject: Keep track of parsed array changes when parsing===false (#9525) X-Git-Tag: v3.5.1~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7835973eb0f51453f6874fcc4d3abb006e3f189a;p=thirdparty%2FChart.js.git Keep track of parsed array changes when parsing===false (#9525) * Keep track of parsed array changes when parsing===false * Code review feedback --- diff --git a/src/core/core.datasetController.js b/src/core/core.datasetController.js index f504d3f5c..812ee96bc 100644 --- a/src/core/core.datasetController.js +++ b/src/core/core.datasetController.js @@ -1003,42 +1003,51 @@ export default class DatasetController { meta.data.splice(start, count); } + _sync(args) { + if (this._parsing) { + this._syncList.push(args); + } else { + const [method, arg1, arg2] = args; + this[method](arg1, arg2); + } + } + /** * @private */ _onDataPush() { const count = arguments.length; - this._syncList.push(['_insertElements', this.getDataset().data.length - count, count]); + this._sync(['_insertElements', this.getDataset().data.length - count, count]); } /** * @private */ _onDataPop() { - this._syncList.push(['_removeElements', this._cachedMeta.data.length - 1, 1]); + this._sync(['_removeElements', this._cachedMeta.data.length - 1, 1]); } /** * @private */ _onDataShift() { - this._syncList.push(['_removeElements', 0, 1]); + this._sync(['_removeElements', 0, 1]); } /** * @private */ _onDataSplice(start, count) { - this._syncList.push(['_removeElements', start, count]); - this._syncList.push(['_insertElements', start, arguments.length - 2]); + this._sync(['_removeElements', start, count]); + this._sync(['_insertElements', start, arguments.length - 2]); } /** * @private */ _onDataUnshift() { - this._syncList.push(['_insertElements', 0, arguments.length]); + this._sync(['_insertElements', 0, arguments.length]); } } diff --git a/test/specs/core.datasetController.tests.js b/test/specs/core.datasetController.tests.js index 5ceeae205..372655c0a 100644 --- a/test/specs/core.datasetController.tests.js +++ b/test/specs/core.datasetController.tests.js @@ -376,6 +376,48 @@ describe('Chart.DatasetController', function() { expect(controller.getParsed(9)).toBe(last); }); + it('should synchronize insert before removal when parsing is off', function() { + // https://github.com/chartjs/Chart.js/issues/9511 + const data = [{x: 0, y: 1}, {x: 2, y: 7}, {x: 3, y: 5}]; + var chart = acquireChart({ + type: 'scatter', + data: { + datasets: [{ + data: data, + }], + }, + options: { + parsing: false, + scales: { + x: { + type: 'linear', + min: 0, + max: 10, + }, + y: { + type: 'linear', + min: 0, + max: 10, + }, + }, + }, + }); + + var meta = chart.getDatasetMeta(0); + var controller = meta.controller; + + data.push({ + x: 10, + y: 6 + }); + data.splice(0, 1); + chart.update(); + + expect(meta.data.length).toBe(3); + expect(controller.getParsed(0)).toBe(data[0]); + expect(controller.getParsed(2)).toBe(data[2]); + }); + it('should re-synchronize metadata when the data object reference changes', function() { var data0 = [0, 1, 2, 3, 4, 5]; var data1 = [6, 7, 8];