OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Scheduler for requests. Fetches requests from a queue and processes them | 6 * Scheduler for requests. Fetches requests from a queue and processes them |
7 * synchronously, taking into account priorities. The highest priority is 0. | 7 * synchronously, taking into account priorities. The highest priority is 0. |
8 * @constructor | 8 * @constructor |
9 */ | 9 */ |
10 function Scheduler() { | 10 function Scheduler() { |
11 /** | 11 /** |
12 * List of requests waiting to be checked. If these items are available in | 12 * List of requests waiting to be checked. If these items are available in |
13 * cache, then they are processed immediately after starting the scheduler. | 13 * cache, then they are processed immediately after starting the scheduler. |
14 * However, if they have to be downloaded, then these requests are moved | 14 * However, if they have to be downloaded, then these requests are moved |
15 * to pendingRequests_. | 15 * to pendingRequests_. |
16 * | 16 * |
17 * @type {Array.<Request>} | 17 * @type {Array.<ImageRequest>} |
18 * @private | 18 * @private |
19 */ | 19 */ |
20 this.newRequests_ = []; | 20 this.newRequests_ = []; |
21 | 21 |
22 /** | 22 /** |
23 * List of pending requests for images to be downloaded. | 23 * List of pending requests for images to be downloaded. |
24 * @type {Array.<Request>} | 24 * @type {Array.<ImageRequest>} |
25 * @private | 25 * @private |
26 */ | 26 */ |
27 this.pendingRequests_ = []; | 27 this.pendingRequests_ = []; |
28 | 28 |
29 /** | 29 /** |
30 * List of requests being processed. | 30 * List of requests being processed. |
31 * @type {Array.<Request>} | 31 * @type {Array.<ImageRequest>} |
32 * @private | 32 * @private |
33 */ | 33 */ |
34 this.activeRequests_ = []; | 34 this.activeRequests_ = []; |
35 | 35 |
36 /** | 36 /** |
37 * Hash array of requests being added to the queue, but not finalized yet. | 37 * Hash array of requests being added to the queue, but not finalized yet. |
38 * @type {Object} | 38 * @type {Object} |
39 * @private | 39 * @private |
40 */ | 40 */ |
41 this.requests_ = {}; | 41 this.requests_ = {}; |
(...skipping 11 matching lines...) Expand all Loading... |
53 * @type {number} | 53 * @type {number} |
54 * @const | 54 * @const |
55 */ | 55 */ |
56 Scheduler.MAXIMUM_IN_PARALLEL = 5; | 56 Scheduler.MAXIMUM_IN_PARALLEL = 5; |
57 | 57 |
58 /** | 58 /** |
59 * Adds a request to the internal priority queue and executes it when requests | 59 * Adds a request to the internal priority queue and executes it when requests |
60 * with higher priorities are finished. If the result is cached, then it is | 60 * with higher priorities are finished. If the result is cached, then it is |
61 * processed immediately once the scheduler is started. | 61 * processed immediately once the scheduler is started. |
62 * | 62 * |
63 * @param {Request} request Request object. | 63 * @param {ImageRequest} request Request object. |
64 */ | 64 */ |
65 Scheduler.prototype.add = function(request) { | 65 Scheduler.prototype.add = function(request) { |
66 if (!this.started_) { | 66 if (!this.started_) { |
67 this.newRequests_.push(request); | 67 this.newRequests_.push(request); |
68 this.requests_[request.getId()] = request; | 68 this.requests_[request.getId()] = request; |
69 return; | 69 return; |
70 } | 70 } |
71 | 71 |
72 // Enqueue the request, since already started. | 72 // Enqueue the request, since already started. |
73 this.pendingRequests_.push(request); | 73 this.pendingRequests_.push(request); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 function(currentRequest) { | 142 function(currentRequest) { |
143 currentRequest.downloadAndProcess( | 143 currentRequest.downloadAndProcess( |
144 this.finish_.bind(this, currentRequest)); | 144 this.finish_.bind(this, currentRequest)); |
145 }.bind(this, request)); | 145 }.bind(this, request)); |
146 } | 146 } |
147 }; | 147 }; |
148 | 148 |
149 /** | 149 /** |
150 * Handles finished requests. | 150 * Handles finished requests. |
151 * | 151 * |
152 * @param {Request} request Finished request. | 152 * @param {ImageRequest} request Finished request. |
153 * @private | 153 * @private |
154 */ | 154 */ |
155 Scheduler.prototype.finish_ = function(request) { | 155 Scheduler.prototype.finish_ = function(request) { |
156 var index = this.activeRequests_.indexOf(request); | 156 var index = this.activeRequests_.indexOf(request); |
157 if (index < 0) | 157 if (index < 0) |
158 console.warn('Request not found.'); | 158 console.warn('Request not found.'); |
159 this.activeRequests_.splice(index, 1); | 159 this.activeRequests_.splice(index, 1); |
160 delete this.requests_[request.getId()]; | 160 delete this.requests_[request.getId()]; |
161 | 161 |
162 // Continue handling the most important requests (if started). | 162 // Continue handling the most important requests (if started). |
163 if (this.started_) | 163 if (this.started_) |
164 this.continue_(); | 164 this.continue_(); |
165 }; | 165 }; |
OLD | NEW |