-
Notifications
You must be signed in to change notification settings - Fork 2k
/
debouncer.html
86 lines (77 loc) · 2.96 KB
/
debouncer.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../lib/async.html">
<link rel="import" href="../lib/debounce.html">
<script>
Polymer.Base._addFeature({
_setupDebouncers: function() {
this._debouncers = {};
},
/**
* Call `debounce` to collapse multiple requests for a named task into
* one invocation which is made after the wait time has elapsed with
* no new request. If no wait time is given, the callback will be called
* at microtask timing (guaranteed before paint).
*
* debouncedClickAction: function(e) {
* // will not call `processClick` more than once per 100ms
* this.debounce('click', function() {
* this.processClick;
* }, 100);
* }
*
* @method debounce
* @param {String} jobName String to indentify the debounce job.
* @param {Function} callback Function that is called (with `this`
* context) when the wait time elapses.
* @param {number} wait Optional wait time in milliseconds (ms) after the
* last signal that must elapse before invoking `callback`
*/
debounce: function(jobName, callback, wait) {
return this._debouncers[jobName] = Polymer.Debounce.call(this,
this._debouncers[jobName], callback, wait);
},
/**
* Returns whether a named debouncer is active.
*
* @method isDebouncerActive
* @param {String} jobName The name of the debouncer started with `debounce`
* @return {boolean} Whether the debouncer is active (has not yet fired).
*/
isDebouncerActive: function(jobName) {
var debouncer = this._debouncers[jobName];
return debouncer && debouncer.finish;
},
/**
* Immediately calls the debouncer `callback` and inactivates it.
*
* @method flushDebouncer
* @param {String} jobName The name of the debouncer started with `debounce`
*/
flushDebouncer: function(jobName) {
var debouncer = this._debouncers[jobName];
if (debouncer) {
debouncer.complete();
}
},
/**
* Cancels an active debouncer. The `callback` will not be called.
*
* @method cancelDebouncer
* @param {String} jobName The name of the debouncer started with `debounce`
*/
cancelDebouncer: function(jobName) {
var debouncer = this._debouncers[jobName];
if (debouncer) {
debouncer.stop();
}
}
});
</script>