Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/chart/src/core/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export class Chart {
} else {
throw new Error('chart-ts: inject a container canvas or a ChartDocument');
}
this.store.setData(this.config.data);
this.store.setData(this.config.data.slice());
this.timeSlots.sync(this.store.raw(), this.config.timeScale, 'data');
this.syncInlineBook();
this.syncHeatmap();
Expand Down Expand Up @@ -452,7 +452,7 @@ export class Chart {
setData(candles: readonly Candle[]): void {
this.rangeAnimation?.clear();
this.candleAnimation?.clear();
this.store.setData(candles);
this.store.setData(candles.slice());
this.timeSlots.sync(this.store.raw(), this.config.timeScale, 'data');
this.events.dataLoaded('set', this.store.length);
this.indicatorCache.clear();
Expand Down Expand Up @@ -577,7 +577,7 @@ export class Chart {
if (partial.data !== undefined) {
this.rangeAnimation?.clear();
this.candleAnimation?.clear();
this.store.setData(this.config.data);
this.store.setData(this.config.data.slice());
this.events.dataLoaded('set', this.store.length);
this.indicatorCache.clear();
}
Expand Down
11 changes: 8 additions & 3 deletions packages/chart/src/core/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,16 @@ export class DataStore {
/**
* Replaces the whole dataset. Non-finite rows are dropped, duplicate times
* keep the last row, `high`/`low` expand to cover `open` and `close`, and a
* negative or non-finite volume becomes 0. Already-clean input keeps the
* same candle objects.
* negative or non-finite volume becomes 0. An array that is already finite,
* strictly increasing, unique and OHLC-consistent is stored as-is: no copy.
* Callers must not mutate that array.
*/
setData(candles: readonly Candle[]): void {
this.candles = firstDirty(candles) === candles.length ? candles.slice() : sanitizeCandles(candles);
if (firstDirty(candles) === candles.length) {
this.candles = candles as Candle[];
return;
}
this.candles = sanitizeCandles(candles);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/chart/test/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ describe('DataStore', () => {
assert.equal(s.last(), undefined);
assert.deepEqual(s.all(), []);
});
it('setData sorts defensively and copies', () => {
it('setData sorts defensively and copies only when a row is dirty', () => {
const s = new DataStore();
const input = [candle(3), candle(1), candle(2)];
s.setData(input);
assert.deepEqual(s.all().map((c) => c.time), [1, 2, 3]);
input.push(candle(9));
assert.equal(s.length, 3);
const clean = [candle(1), candle(2), candle(3)];
s.setData(clean);
assert.equal(s.raw(), clean);
assert.equal(s.at(0), clean[0]);
});
it('append inserts in order and out of order', () => {
const s = new DataStore();
Expand Down