Skip to content

Commit

Permalink
Merge pull request #879 from edew/767-chart-update-lag
Browse files Browse the repository at this point in the history
  • Loading branch information
timocov committed Nov 8, 2021
2 parents a4430b9 + 9340537 commit f96a6af
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/model/time-scale.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DateFormatter } from '../formatters/date-formatter';
import { DateTimeFormatter } from '../formatters/date-time-formatter';

import { lowerbound } from '../helpers/algorithms';
import { ensureNotNull } from '../helpers/assertions';
import { Delegate } from '../helpers/delegate';
import { ISubscription } from '../helpers/isubscription';
Expand Down Expand Up @@ -249,17 +250,13 @@ export class TimeScale {
return findNearest ? this._points.length - 1 as TimePointIndex : null;
}

for (let i = 0; i < this._points.length; ++i) {
if (time.timestamp === this._points[i].time.timestamp) {
return i as TimePointIndex;
}
const index = lowerbound(this._points, time.timestamp, (a: TimeScalePoint, b: UTCTimestamp) => a.time.timestamp < b);

if (time.timestamp < this._points[i].time.timestamp) {
return findNearest ? i as TimePointIndex : null;
}
if (time.timestamp < this._points[index].time.timestamp) {
return findNearest ? index as TimePointIndex : null;
}

return null;
return index as TimePointIndex;
}

public isEmpty(): boolean {
Expand Down
59 changes: 59 additions & 0 deletions tests/unittests/time-scale.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,63 @@ describe('TimeScale', () => {
expect(indexes[0].x).to.be.equal(expectedValue, 'indexesToCoordinates');
}
});

describe('timeToIndex', () => {
it('should return index for time on scale', () => {
const ts = new TimeScale(chartModelMock(), timeScaleOptionsDefaults, fakeLocalizationOptions);

ts.update(...tsUpdate(2));

expect(ts.timeToIndex({ timestamp: 0 as UTCTimestamp }, false)).to.be.equal(0);
expect(ts.timeToIndex({ timestamp: 1 as UTCTimestamp }, false)).to.be.equal(1);
expect(ts.timeToIndex({ timestamp: 2 as UTCTimestamp }, false)).to.be.equal(2);
});

it('should return null for time not on scale', () => {
const ts = new TimeScale(chartModelMock(), timeScaleOptionsDefaults, fakeLocalizationOptions);

ts.update(...tsUpdate(2));

expect(ts.timeToIndex({ timestamp: -1 as UTCTimestamp }, false)).to.be.equal(null);
expect(ts.timeToIndex({ timestamp: 3 as UTCTimestamp }, false)).to.be.equal(null);
});

it('should return null if time scale is empty', () => {
const ts = new TimeScale(chartModelMock(), timeScaleOptionsDefaults, fakeLocalizationOptions);

expect(ts.timeToIndex({ timestamp: 123 as UTCTimestamp }, false)).to.be.equal(null);
});

it('should return null if timestamp is between two values on the scale', () => {
const ts = new TimeScale(chartModelMock(), timeScaleOptionsDefaults, fakeLocalizationOptions);

ts.update(...tsUpdate(1));

expect(ts.timeToIndex({ timestamp: 0.5 as UTCTimestamp }, false)).to.be.equal(null);
});

it('should return last index if timestamp is greater than last timestamp and findNearest is parameter is true', () => {
const ts = new TimeScale(chartModelMock(), timeScaleOptionsDefaults, fakeLocalizationOptions);

ts.update(...tsUpdate(2));

expect(ts.timeToIndex({ timestamp: 3 as UTCTimestamp }, true)).to.be.equal(2);
});

it('should return first index if timestamp is less than first timestamp and findNearest is parameter is true', () => {
const ts = new TimeScale(chartModelMock(), timeScaleOptionsDefaults, fakeLocalizationOptions);

ts.update(...tsUpdate(2));

expect(ts.timeToIndex({ timestamp: -1 as UTCTimestamp }, true)).to.be.equal(0);
});

it('should return next index if timestamp is between two values on the scale and findNearest parameter is true', () => {
const ts = new TimeScale(chartModelMock(), timeScaleOptionsDefaults, fakeLocalizationOptions);

ts.update(...tsUpdate(1));

expect(ts.timeToIndex({ timestamp: 0.5 as UTCTimestamp }, true)).to.be.equal(1);
});
});
});

0 comments on commit f96a6af

Please sign in to comment.