Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Adding caching to Range getMillisecondsPerPixel function #3154

Merged
merged 1 commit into from
Jun 13, 2017
Merged
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
9 changes: 7 additions & 2 deletions lib/timeline/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function Range(body, options) {
var now = moment().hours(0).minutes(0).seconds(0).milliseconds(0);
var start = now.clone().add(-3, 'days').valueOf();
var end = now.clone().add(3, 'days').valueOf();

this.millisecondsPerPixelCache = undefined;

if(options === undefined) {
this.start = start;
this.end = end;
Expand Down Expand Up @@ -200,6 +201,7 @@ Range.prototype.setRange = function(start, end, options, callback) {
var finalStart = start != undefined ? util.convert(start, 'Date').valueOf() : null;
var finalEnd = end != undefined ? util.convert(end, 'Date').valueOf() : null;
this._cancelAnimation();
this.millisecondsPerPixelCache = undefined;

if (options.animation) { // true or an Object
var initStart = this.start;
Expand Down Expand Up @@ -280,7 +282,10 @@ Range.prototype.setRange = function(start, end, options, callback) {
* Get the number of milliseconds per pixel.
*/
Range.prototype.getMillisecondsPerPixel = function() {
return (this.end - this.start) / this.body.dom.center.clientWidth;
if (this.millisecondsPerPixelCache === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cleaner if it would be:

if (!this.millisecondsPerPixelCache) { ... }
return this.millisecondsPerPixelCache

this.millisecondsPerPixelCache = (this.end - this.start) / this.body.dom.center.clientWidth;
}
return this.millisecondsPerPixelCache;
}

/**
Expand Down