-
Notifications
You must be signed in to change notification settings - Fork 13
/
queue.js
169 lines (143 loc) · 3.55 KB
/
queue.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* RequestQueue
* Request images from the camera server, and keep a recent history.
* Allows us to throttle camera requests, drop old frames, etc.
*/
function RequestQueue()
{
this.queue = []; // A queue of {url, date, image}
this.maxLength = 4;
// References to other objects.
this.camera = null;
/**
* Make a request to the image server.
* Has internal callbacks that handle state information.
*/
this.request = function(url)
{
var that = this;
var d = new Date();
var now = d.getTime();
var entry = {};
// Success Cb
var onLoad = function(url, requestDate)
{
var time = (new Date()).getTime();
that.camera.counts.load++;
// Don't update image if it's older than the current one.
if(that.camera.times.lastLoaded_requestDate >= requestDate) {
return;
}
// Update image.
that.camera.curFrame.src = url;
// TODO/FIXME [android-webkit-bug]
// XXX: Stupid Android Webkit (only) bug?
// onLoad() fires, but image is not complete!
if(!that.camera.curFrame.complete) {
console.log('Image load incomplete: ' + url);
that.camera.counts.load--;
return;
}
that.camera.times.lastLoaded = time;
that.camera.times.lastLoaded_requestDate = requestDate;
if(!that.camera.times.firstLoaded) {
that.camera.times.firstLoaded = time;
}
// FIXME: Ugly call
that.camera.controller.view.updateCameraView(that.camera);
}
// Fail Cb
var onFail = function() {
that.camera.times.lastFailed = (new Date()).getTime();
that.camera.counts.fail++;
// FIXME: Ugly call.
that.camera.controller.view.updateCameraView(that.camera);
}
// Abort Cb
var onAbort = function() {
that.camera.times.lastAborted = (new Date()).getTime();
that.camera.counts.abort++;
// FIXME: Ugly call.
that.camera.controller.view.updateCameraView(that.camera);
}
// If queue is full, make room.
while(this.queue.length >= this.maxLength) {
var data = this.queue.shift();
data.image.src = null;
}
// Perform request
entry['url'] = url;
entry['date'] = now;
entry['image'] = new Image();
entry['image'].onload = function() { onLoad(url, now); };
entry['image'].onerror = function() { onFail(); };
entry['image'].onabort = function() { onAbort(); };
entry['image'].src = url;
this.queue.push(entry);
this.camera.counts.request++;
this.camera.times.lastRequested = now;
if(!this.camera.times.firstRequested) {
this.camera.times.firstRequested = now;
}
// FIXME: Ugly call
this.camera.controller.view.updateCameraView(that.camera);
}
/**
* Queue capacity exhausted?
*/
this.isFull = function()
{
return this.queue.length >= this.maxLength;
}
/**
* Percentage of queue used.
*/
this.percentFull = function()
{
return this.queue.length / this.maxLength;
}
/**
* Age of the last queue item.
*/
this.newest = function()
{
if(this.queue.length == 0) {
return null;
}
return this.queue[this.queue.length-1].date;
}
/**
* Age of the first queue item.
*/
this.oldest = function()
{
if(this.queue.length == 0) {
return null;
}
return this.queue[0].date;
}
/**
* Purge all queue items from before (and including) last met
* request date.
*/
this.purgeOld = function()
{
var found = 0;
var obj = null;
for(var i = 0; i < this.queue.length; i++) {
if(this.queue[i].date > this.camera.times.lastLoaded_requestDate) {
break;
}
found++;
}
if(!found) {
return;
}
this.camera.counts.cachePurge += found-1;
while(found > 0) {
obj = this.queue.shift();
delete obj.image;
found--;
}
}
}