Index: third_party/google_input_tools/third_party/closure_library/closure/goog/timer/timer.js |
diff --git a/third_party/google_input_tools/third_party/closure_library/closure/goog/timer/timer.js b/third_party/google_input_tools/third_party/closure_library/closure/goog/timer/timer.js |
index ccee170d00cc3cc79bbf58d1836e2517bf0e7cc1..19a26b680705ae8e832afc5e77d591f917cb17ab 100644 |
--- a/third_party/google_input_tools/third_party/closure_library/closure/goog/timer/timer.js |
+++ b/third_party/google_input_tools/third_party/closure_library/closure/goog/timer/timer.js |
@@ -21,6 +21,7 @@ |
goog.provide('goog.Timer'); |
+goog.require('goog.Promise'); |
goog.require('goog.events.EventTarget'); |
@@ -91,6 +92,17 @@ goog.Timer.MAX_TIMEOUT_ = 2147483647; |
/** |
+ * A timer ID that cannot be returned by any known implmentation of |
+ * Window.setTimeout. Passing this value to window.clearTimeout should |
+ * therefore be a no-op. |
+ * |
+ * @const {number} |
+ * @private |
+ */ |
+goog.Timer.INVALID_TIMEOUT_ID_ = -1; |
+ |
+ |
+/** |
* Whether this timer is enabled |
* @type {boolean} |
*/ |
@@ -275,7 +287,7 @@ goog.Timer.callOnce = function(listener, opt_delay, opt_handler) { |
// Timeouts greater than MAX_INT return immediately due to integer |
// overflow in many browsers. Since MAX_INT is 24.8 days, just don't |
// schedule anything at all. |
- return -1; |
+ return goog.Timer.INVALID_TIMEOUT_ID_; |
} else { |
return goog.Timer.defaultTimerObject.setTimeout( |
listener, opt_delay || 0); |
@@ -290,3 +302,28 @@ goog.Timer.callOnce = function(listener, opt_delay, opt_handler) { |
goog.Timer.clear = function(timerId) { |
goog.Timer.defaultTimerObject.clearTimeout(timerId); |
}; |
+ |
+ |
+/** |
+ * @param {number} delay Milliseconds to wait. |
+ * @param {(RESULT|goog.Thenable<RESULT>|Thenable)=} opt_result The value |
+ * with which the promise will be resolved. |
+ * @return {!goog.Promise<RESULT>} A promise that will be resolved after |
+ * the specified delay, unless it is canceled first. |
+ * @template RESULT |
+ */ |
+goog.Timer.promise = function(delay, opt_result) { |
+ var timerKey = null; |
+ return new goog.Promise(function(resolve, reject) { |
+ timerKey = goog.Timer.callOnce(function() { |
+ resolve(opt_result); |
+ }, delay); |
+ if (timerKey == goog.Timer.INVALID_TIMEOUT_ID_) { |
+ reject(new Error('Failed to schedule timer.')); |
+ } |
+ }).thenCatch(function(error) { |
+ // Clear the timer. The most likely reason is "cancel" signal. |
+ goog.Timer.clear(timerKey); |
+ throw error; |
+ }); |
+}; |