| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 @license |
| 3 Copyright (c) 2017 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 <link rel="import" href="boot.html"> |
| 11 <link rel="import" href="mixin.html"> |
| 12 <link rel="import" href="async.html"> |
| 13 |
| 14 <script> |
| 15 (function() { |
| 16 'use strict'; |
| 17 |
| 18 /** @typedef {{run: function(function(), number=):number, cancel: function(num
ber)}} */ |
| 19 let AsyncModule; // eslint-disable-line no-unused-vars |
| 20 |
| 21 class Debouncer { |
| 22 constructor() { |
| 23 this._asyncModule = null; |
| 24 this._callback = null; |
| 25 this._timer = null; |
| 26 } |
| 27 /** |
| 28 * Sets the scheduler; that is, a module with the Async interface, |
| 29 * a callback and optional arguments to be passed to the run function |
| 30 * from the async module. |
| 31 * |
| 32 * @param {!AsyncModule} asyncModule Object with Async interface. |
| 33 * @param {function()} callback Callback to run. |
| 34 */ |
| 35 setConfig(asyncModule, callback) { |
| 36 this._asyncModule = asyncModule; |
| 37 this._callback = callback; |
| 38 this._timer = this._asyncModule.run(() => { |
| 39 this._timer = null; |
| 40 this._callback() |
| 41 }); |
| 42 } |
| 43 /** |
| 44 * Cancels an active debouncer and returns a reference to itself. |
| 45 */ |
| 46 cancel() { |
| 47 if (this.isActive()) { |
| 48 this._asyncModule.cancel(this._timer); |
| 49 this._timer = null; |
| 50 } |
| 51 } |
| 52 /** |
| 53 * Flushes an active debouncer and returns a reference to itself. |
| 54 */ |
| 55 flush() { |
| 56 if (this.isActive()) { |
| 57 this.cancel(); |
| 58 this._callback(); |
| 59 } |
| 60 } |
| 61 /** |
| 62 * Returns true if the debouncer is active. |
| 63 * |
| 64 * @return {boolean} True if active. |
| 65 */ |
| 66 isActive() { |
| 67 return this._timer != null; |
| 68 } |
| 69 /** |
| 70 * Creates a debouncer if no debouncer is passed as a parameter |
| 71 * or it cancels an active debouncer otherwise. The following |
| 72 * example shows how a debouncer can be called multiple times within a |
| 73 * microtask and "debounced" such that the provided callback function is |
| 74 * called once. Add this method to a custom element: |
| 75 * |
| 76 * _debounceWork() { |
| 77 * this._debounceJob = Polymer.Debouncer.debounce(this._debounceJob, |
| 78 * Polymer.Async.microTask, () => { |
| 79 * this._doWork(); |
| 80 * }); |
| 81 * } |
| 82 * |
| 83 * If the `_debounceWork` method is called multiple times within the same |
| 84 * microtask, the `_doWork` function will be called only once at the next |
| 85 * microtask checkpoint. |
| 86 * |
| 87 * Note: In testing it is often convenient to avoid asynchrony. To accomplish |
| 88 * this with a debouncer, you can use `Polymer.enqueueDebouncer` and |
| 89 * `Polymer.flush`. For example, extend the above example by adding |
| 90 * `Polymer.enqueueDebouncer(this._debounceJob)` at the end of the |
| 91 * `_debounceWork` method. Then in a test, call `Polymer.flush` to ensure |
| 92 * the debouncer has completed. |
| 93 * |
| 94 * @param {Polymer.Debouncer?} debouncer Debouncer object. |
| 95 * @param {!AsyncModule} asyncModule Object with Async interface |
| 96 * @param {function()} callback Callback to run. |
| 97 * @return {!Debouncer} Returns a debouncer object. |
| 98 */ |
| 99 static debounce(debouncer, asyncModule, callback) { |
| 100 if (debouncer instanceof Debouncer) { |
| 101 debouncer.cancel(); |
| 102 } else { |
| 103 debouncer = new Debouncer(); |
| 104 } |
| 105 debouncer.setConfig(asyncModule, callback); |
| 106 return debouncer; |
| 107 } |
| 108 } |
| 109 |
| 110 /** |
| 111 * @memberof Polymer |
| 112 */ |
| 113 Polymer.Debouncer = Debouncer; |
| 114 })(); |
| 115 </script> |
| OLD | NEW |