OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 * @fileoverview Provides a countdown-based timer implementation. | 6 * @fileoverview Provides a countdown-based timer implementation. |
7 */ | 7 */ |
8 'use strict'; | 8 'use strict'; |
9 | 9 |
10 /** | 10 /** |
(...skipping 25 matching lines...) Expand all Loading... |
36 * @return {boolean} whether the timeout could be set. | 36 * @return {boolean} whether the timeout could be set. |
37 */ | 37 */ |
38 CountdownTimer.prototype.setTimeout = function(timeoutMillis, cb) { | 38 CountdownTimer.prototype.setTimeout = function(timeoutMillis, cb) { |
39 if (this.timeoutId) | 39 if (this.timeoutId) |
40 return false; | 40 return false; |
41 if (!timeoutMillis || timeoutMillis < 0) | 41 if (!timeoutMillis || timeoutMillis < 0) |
42 return false; | 42 return false; |
43 this.remainingMillis = timeoutMillis; | 43 this.remainingMillis = timeoutMillis; |
44 this.cb = cb; | 44 this.cb = cb; |
45 if (this.remainingMillis > CountdownTimer.TIMER_INTERVAL_MILLIS) { | 45 if (this.remainingMillis > CountdownTimer.TIMER_INTERVAL_MILLIS) { |
46 this.timeoutId = | 46 this.timeoutId = this.sysTimer_.setInterval( |
47 this.sysTimer_.setInterval(this.timerTick.bind(this), | 47 this.timerTick.bind(this), CountdownTimer.TIMER_INTERVAL_MILLIS); |
48 CountdownTimer.TIMER_INTERVAL_MILLIS); | |
49 } else { | 48 } else { |
50 // Set a one-shot timer for the last interval. | 49 // Set a one-shot timer for the last interval. |
51 this.timeoutId = | 50 this.timeoutId = this.sysTimer_.setTimeout( |
52 this.sysTimer_.setTimeout( | 51 this.timerTick.bind(this), this.remainingMillis); |
53 this.timerTick.bind(this), this.remainingMillis); | |
54 } | 52 } |
55 return true; | 53 return true; |
56 }; | 54 }; |
57 | 55 |
58 /** Clears this timer's timeout. Timers that are cleared become expired. */ | 56 /** Clears this timer's timeout. Timers that are cleared become expired. */ |
59 CountdownTimer.prototype.clearTimeout = function() { | 57 CountdownTimer.prototype.clearTimeout = function() { |
60 if (this.timeoutId) { | 58 if (this.timeoutId) { |
61 this.sysTimer_.clearTimeout(this.timeoutId); | 59 this.sysTimer_.clearTimeout(this.timeoutId); |
62 this.timeoutId = undefined; | 60 this.timeoutId = undefined; |
63 } | 61 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 /** @private {!SystemTimer} */ | 105 /** @private {!SystemTimer} */ |
108 this.sysTimer_ = sysTimer; | 106 this.sysTimer_ = sysTimer; |
109 } | 107 } |
110 | 108 |
111 /** | 109 /** |
112 * Creates a new timer. | 110 * Creates a new timer. |
113 * @param {number} timeoutMillis How long, in milliseconds, the countdown lasts. | 111 * @param {number} timeoutMillis How long, in milliseconds, the countdown lasts. |
114 * @param {function()=} opt_cb Called back when the countdown expires. | 112 * @param {function()=} opt_cb Called back when the countdown expires. |
115 * @return {!Countdown} The timer. | 113 * @return {!Countdown} The timer. |
116 */ | 114 */ |
117 CountdownTimerFactory.prototype.createTimer = | 115 CountdownTimerFactory.prototype.createTimer = function(timeoutMillis, opt_cb) { |
118 function(timeoutMillis, opt_cb) { | |
119 return new CountdownTimer(this.sysTimer_, timeoutMillis, opt_cb); | 116 return new CountdownTimer(this.sysTimer_, timeoutMillis, opt_cb); |
120 }; | 117 }; |
121 | 118 |
122 /** | 119 /** |
123 * Minimum timeout attenuation, below which a response couldn't be reasonably | 120 * Minimum timeout attenuation, below which a response couldn't be reasonably |
124 * guaranteed, in seconds. | 121 * guaranteed, in seconds. |
125 * @const | 122 * @const |
126 */ | 123 */ |
127 var MINIMUM_TIMEOUT_ATTENUATION_SECONDS = 1; | 124 var MINIMUM_TIMEOUT_ATTENUATION_SECONDS = 1; |
128 | 125 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 167 |
171 /** | 168 /** |
172 * Creates a new countdown for the given timeout value, attenuated to ensure a | 169 * Creates a new countdown for the given timeout value, attenuated to ensure a |
173 * response is given prior to the countdown's expiration, using the given timer | 170 * response is given prior to the countdown's expiration, using the given timer |
174 * factory. | 171 * factory. |
175 * @param {CountdownFactory} timerFactory The factory to use. | 172 * @param {CountdownFactory} timerFactory The factory to use. |
176 * @param {number} timeoutValueSeconds | 173 * @param {number} timeoutValueSeconds |
177 * @param {number=} opt_attenuationSeconds Attenuation value in seconds. | 174 * @param {number=} opt_attenuationSeconds Attenuation value in seconds. |
178 * @return {!Countdown} A countdown timer. | 175 * @return {!Countdown} A countdown timer. |
179 */ | 176 */ |
180 function createAttenuatedTimer(timerFactory, timeoutValueSeconds, | 177 function createAttenuatedTimer( |
181 opt_attenuationSeconds) { | 178 timerFactory, timeoutValueSeconds, opt_attenuationSeconds) { |
182 timeoutValueSeconds = attenuateTimeoutInSeconds(timeoutValueSeconds, | 179 timeoutValueSeconds = |
183 opt_attenuationSeconds); | 180 attenuateTimeoutInSeconds(timeoutValueSeconds, opt_attenuationSeconds); |
184 return timerFactory.createTimer(timeoutValueSeconds * 1000); | 181 return timerFactory.createTimer(timeoutValueSeconds * 1000); |
185 } | 182 } |
OLD | NEW |