| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 A class that mediates window timer calls to support mockability | 6 * @fileoverview A class that mediates window timer calls to support mockability |
| 7 * in tests. | 7 * in tests. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 cr.define('bookmarks', function() { | 10 cr.define('bookmarks', function() { |
| 11 /** @constructor */ | 11 /** @constructor */ |
| 12 function TimerProxy() {} | 12 function TimerProxy() {} |
| 13 | 13 |
| 14 TimerProxy.prototype = { | 14 TimerProxy.prototype = { |
| 15 /** | 15 /** |
| 16 * @param {Function|string} fn | 16 * @param {function()|string} fn |
| 17 * @param {number=} delay | 17 * @param {number=} delay |
| 18 * @return {number} | 18 * @return {number} |
| 19 */ | 19 */ |
| 20 setTimeout: function(fn, delay) { | 20 setTimeout: function(fn, delay) { |
| 21 return window.setTimeout(fn, delay); | 21 return window.setTimeout(fn, delay); |
| 22 }, | 22 }, |
| 23 | 23 |
| 24 /** @param {number|undefined?} id */ | 24 /** @param {number|undefined?} id */ |
| 25 clearTimeout: function(id) { | 25 clearTimeout: function(id) { |
| 26 window.clearTimeout(id); | 26 window.clearTimeout(id); |
| 27 }, | 27 }, |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 /** |
| 31 * A one-shot debouncer which fires the given callback after a delay. The |
| 32 * delay can be refreshed by calling resetTimer. Resetting with no delay moves |
| 33 * the callback to the end of the task queue. |
| 34 * @param {!function()} callback |
| 35 * @constructor |
| 36 */ |
| 37 function Debouncer(callback) { |
| 38 /** @private {!function()} */ |
| 39 this.callback_ = callback; |
| 40 /** @private {!bookmarks.TimerProxy} */ |
| 41 this.timerProxy_ = new TimerProxy(); |
| 42 /** @private {?number} */ |
| 43 this.timer_ = null; |
| 44 /** @private {!function()} */ |
| 45 this.boundTimerCallback_ = this.timerCallback_.bind(this); |
| 46 /** @private {boolean} */ |
| 47 this.isDone_ = false; |
| 48 /** @private {!PromiseResolver} */ |
| 49 this.promiseResolver_ = new PromiseResolver(); |
| 50 } |
| 51 |
| 52 Debouncer.prototype = { |
| 53 /** |
| 54 * @param {number=} delay |
| 55 */ |
| 56 resetTimeout: function(delay) { |
| 57 if (this.timer_) |
| 58 this.timerProxy_.clearTimeout(this.timer_); |
| 59 this.timer_ = |
| 60 this.timerProxy_.setTimeout(this.boundTimerCallback_, delay); |
| 61 }, |
| 62 |
| 63 /** |
| 64 * @return {boolean} True if the Debouncer has finished processing. |
| 65 */ |
| 66 done: function() { |
| 67 return this.isDone_; |
| 68 }, |
| 69 |
| 70 /** |
| 71 * @return {Promise} Promise which resolves immediately after the callback. |
| 72 */ |
| 73 get promise() { |
| 74 return this.promiseResolver_.promise; |
| 75 }, |
| 76 |
| 77 /** @private */ |
| 78 timerCallback_: function() { |
| 79 this.isDone_ = true; |
| 80 this.callback_.call(); |
| 81 this.promiseResolver_.resolve(); |
| 82 }, |
| 83 }; |
| 84 |
| 30 return { | 85 return { |
| 86 Debouncer: Debouncer, |
| 31 TimerProxy: TimerProxy, | 87 TimerProxy: TimerProxy, |
| 32 }; | 88 }; |
| 33 }); | 89 }); |
| OLD | NEW |