-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeDataStore.js
148 lines (129 loc) · 3.56 KB
/
TimeDataStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
define(
[
'./TimeDataPoint.js',
'./util/clone.js',
'./util/interpolate.js'
],
function(
TimeDataPoint,
clone,
interpolate
) {
function TimeDataStore(startMS,startData) {
this._startMS = startMS;
this._dataPoints = [];
this.addUpdate(startMS,startData);
}
TimeDataStore.prototype.addUpdate = function(timestamp,update) {
var i = 0;
for(var j=this._dataPoints.length-1; j>=0; j--) {
if(this._dataPoints[j].timestamp <= timestamp) {
i = j+1;
break;
}
}
var point = new TimeDataPoint(timestamp,update,{});
this.__insertDataPoint(i,point);
}
TimeDataStore.prototype.getDataAt = function(timestamp) {
var points = this.__findDataPoints(timestamp);
if(points.previous) {
if(!points.next || points.next === points.previous) {
return clone(points.previous.data);
}
var point = this.__interpolate(points.previous,points.next,timestamp);
return point.data;
}
return null;
}
TimeDataStore.prototype.__insertDataPoint = function(i,timeDataPoint) {
this._dataPoints.splice(i,0,timeDataPoint);
for(var j=i; j<this._dataPoints.length; j++) {
var data = this._dataPoints[j-1] ? this._dataPoints[j-1].data : {};
this._dataPoints[j].data = applyUpdate(data,this._dataPoints[j].update);
}
while(this._dataPoints.length > 500) {
this._dataPoints.shift();
}
}
TimeDataStore.prototype.__findDataPoints = function(timestamp) {
var result = {previous: null, next: null};
for(var i=this._dataPoints.length-1; i>=0; i--) {
if(this._dataPoints[i].timestamp === timestamp) {
result.previous = result.next = this._dataPoints[i];
break;
} else if(this._dataPoints[i].timestamp < timestamp) {
result.previous = this._dataPoints[i];
if(this._dataPoints[i+1]) {
result.next = this._dataPoints[i+1];
}
break;
}
}
return result;
}
TimeDataStore.prototype.__interpolate = function(pointA,pointB,timestamp) {
var result = new TimeDataPoint(timestamp);
var percentage = (timestamp - pointA.timestamp) / (pointB.timestamp - pointA.timestamp);
result.data = interpolate(pointA.data,pointB.data,percentage);
return result;
}
function applyUpdate(data,update) {
function handle(data,update) {
for(var i in update) {
switch(i) {
case "@push":
if(!data.__pushed) {
data.__pushed = {};
}
if(!data.__pushed[update[i]]) {
data.__pushed[update[i]] = [];
}
data.__pushed[update[i]].unshift(data[update[i]]);
data[update[i]] = undefined;
break;
case "@pop":
if(!data.__pushed) {
data.__pushed = {};
}
if(!data.__pushed[update[i]]) {
data.__pushed[update[i]] = [];
}
var result = data.__pushed[update[i]].shift();
data[update[i]] = result;
break;
case "@clear":
if(!data.__pushed) {
data.__pushed = {};
}
if(!data.__pushed[update[i]]) {
data.__pushed[update[i]] = [];
}
while(data.__pushed[update[i]][0]) {
data.__pushed[update[i]].shift();
}
data[update[i]] = undefined;
break;
case "@remove":
data[update[i]] = undefined;
break;
default:
if(update[i] && typeof update[i] === "object") {
if(typeof data[i] !== "object") {
data[i] = {};
}
handle(data[i],update[i]);
} else {
data[i] = update[i];
}
break;
}
}
}
var result = clone(data);
handle(result,update);
return result;
}
return TimeDataStore;
}
);