Chromium Code Reviews| 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 timer which will fire on the next task after the last time it is reset. | |
|
calamity
2017/07/26 05:36:06
nit: This isn't strictly true. It gets added to th
tsergeant
2017/07/26 06:34:07
Done.
| |
| 32 * @param {function()} callback | |
| 33 * @constructor | |
| 34 */ | |
| 35 function Debouncer(callback) { | |
|
tsergeant
2017/07/25 00:13:00
I'm happy enough with this for now, but one unansw
calamity
2017/07/26 05:36:06
Acknowledged.
| |
| 36 this.callback_ = callback; | |
| 37 this.timerProxy_ = new TimerProxy(); | |
| 38 this.timer_ = null; | |
| 39 this.boundTimerCallback_ = this.timerCallback_.bind(this); | |
| 40 this.isDone_ = false; | |
| 41 this.promiseResolver_ = new PromiseResolver(); | |
|
calamity
2017/07/26 05:36:06
nit: Couple of types?
tsergeant
2017/07/26 06:34:07
Done.
| |
| 42 } | |
| 43 | |
| 44 Debouncer.prototype = { | |
| 45 resetTimer: function() { | |
|
calamity
2017/07/26 05:36:06
nit: resetTimeout perhaps? This will be given a de
tsergeant
2017/07/26 06:34:07
Done.
| |
| 46 if (this.timer_) | |
| 47 this.timerProxy_.clearTimeout(this.timer_); | |
| 48 this.timer_ = this.timerProxy_.setTimeout(this.boundTimerCallback_); | |
| 49 }, | |
| 50 | |
| 51 /** | |
| 52 * @return {boolean} True if the Debouncer has finished processing. | |
| 53 */ | |
| 54 done: function() { | |
| 55 return this.isDone_; | |
| 56 }, | |
| 57 | |
| 58 /** | |
| 59 * @return {Promise} Promise which resolves immediately after the callback. | |
| 60 */ | |
| 61 get promise() { | |
| 62 return this.promiseResolver_.promise; | |
| 63 }, | |
| 64 | |
| 65 /** @private */ | |
| 66 timerCallback_: function() { | |
| 67 this.isDone_ = true; | |
| 68 this.callback_.call(); | |
| 69 this.promiseResolver_.resolve(); | |
| 70 }, | |
| 71 }; | |
| 72 | |
| 30 return { | 73 return { |
| 74 Debouncer: Debouncer, | |
| 31 TimerProxy: TimerProxy, | 75 TimerProxy: TimerProxy, |
| 32 }; | 76 }; |
| 33 }); | 77 }); |
| OLD | NEW |