@common.js/p-debounce 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/index.d.ts +72 -0
- package/index.js +233 -0
- package/license +9 -0
- package/package.json +29 -0
package/README.md
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
declare namespace pDebounce {
|
|
2
|
+
interface Options {
|
|
3
|
+
/**
|
|
4
|
+
Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1). Meaning immediately, instead of waiting for `wait` milliseconds.
|
|
5
|
+
|
|
6
|
+
@default false
|
|
7
|
+
*/
|
|
8
|
+
readonly before?: boolean;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare const pDebounce: {
|
|
13
|
+
/**
|
|
14
|
+
[Debounce](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions.
|
|
15
|
+
|
|
16
|
+
@param fn - Promise-returning/async function to debounce.
|
|
17
|
+
@param wait - Milliseconds to wait before calling `fn`.
|
|
18
|
+
@returns A function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.
|
|
19
|
+
|
|
20
|
+
@example
|
|
21
|
+
```
|
|
22
|
+
import pDebounce from 'p-debounce';
|
|
23
|
+
|
|
24
|
+
const expensiveCall = async input => input;
|
|
25
|
+
|
|
26
|
+
const debouncedFn = pDebounce(expensiveCall, 200);
|
|
27
|
+
|
|
28
|
+
for (const number of [1, 2, 3]) {
|
|
29
|
+
console.log(await debouncedFn(number));
|
|
30
|
+
}
|
|
31
|
+
//=> 3
|
|
32
|
+
//=> 3
|
|
33
|
+
//=> 3
|
|
34
|
+
```
|
|
35
|
+
*/
|
|
36
|
+
<ArgumentsType extends unknown[], ReturnType>(
|
|
37
|
+
fn: (...arguments: ArgumentsType) => PromiseLike<ReturnType> | ReturnType,
|
|
38
|
+
wait: number,
|
|
39
|
+
options?: pDebounce.Options
|
|
40
|
+
): (...arguments: ArgumentsType) => Promise<ReturnType>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
Execute `function_` unless a previous call is still pending, in which case, return the pending promise. Useful, for example, to avoid processing extra button clicks if the previous one is not complete.
|
|
44
|
+
|
|
45
|
+
@param function_ - Promise-returning/async function to debounce.
|
|
46
|
+
|
|
47
|
+
@example
|
|
48
|
+
```
|
|
49
|
+
import {setTimeout as delay} from 'timers/promises';
|
|
50
|
+
import pDebounce from 'p-debounce';
|
|
51
|
+
|
|
52
|
+
const expensiveCall = async value => {
|
|
53
|
+
await delay(200);
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const debouncedFn = pDebounce.promise(expensiveCall);
|
|
58
|
+
|
|
59
|
+
for (const number of [1, 2, 3]) {
|
|
60
|
+
console.log(await debouncedFn(number));
|
|
61
|
+
}
|
|
62
|
+
//=> 1
|
|
63
|
+
//=> 2
|
|
64
|
+
//=> 3
|
|
65
|
+
```
|
|
66
|
+
*/
|
|
67
|
+
promise<ArgumentsType extends unknown[], ReturnType>(
|
|
68
|
+
function_: (...arguments: ArgumentsType) => PromiseLike<ReturnType> | ReturnType
|
|
69
|
+
): (...arguments: ArgumentsType) => Promise<ReturnType>;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export default pDebounce;
|
package/index.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
12
|
+
try {
|
|
13
|
+
var info = gen[key](arg);
|
|
14
|
+
var value = info.value;
|
|
15
|
+
} catch (error) {
|
|
16
|
+
reject(error);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (info.done) {
|
|
20
|
+
resolve(value);
|
|
21
|
+
} else {
|
|
22
|
+
Promise.resolve(value).then(_next, _throw);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function _asyncToGenerator(fn) {
|
|
26
|
+
return function() {
|
|
27
|
+
var self = this, args = arguments;
|
|
28
|
+
return new Promise(function(resolve, reject) {
|
|
29
|
+
var gen = fn.apply(self, args);
|
|
30
|
+
function _next(value) {
|
|
31
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
32
|
+
}
|
|
33
|
+
function _throw(err) {
|
|
34
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
35
|
+
}
|
|
36
|
+
_next(undefined);
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
|
|
41
|
+
var f, y, t, g, _ = {
|
|
42
|
+
label: 0,
|
|
43
|
+
sent: function() {
|
|
44
|
+
if (t[0] & 1) throw t[1];
|
|
45
|
+
return t[1];
|
|
46
|
+
},
|
|
47
|
+
trys: [],
|
|
48
|
+
ops: []
|
|
49
|
+
};
|
|
50
|
+
return g = {
|
|
51
|
+
next: verb(0),
|
|
52
|
+
"throw": verb(1),
|
|
53
|
+
"return": verb(2)
|
|
54
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
55
|
+
return this;
|
|
56
|
+
}), g;
|
|
57
|
+
function verb(n) {
|
|
58
|
+
return function(v) {
|
|
59
|
+
return step([
|
|
60
|
+
n,
|
|
61
|
+
v
|
|
62
|
+
]);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function step(op) {
|
|
66
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
67
|
+
while(_)try {
|
|
68
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
69
|
+
if (y = 0, t) op = [
|
|
70
|
+
op[0] & 2,
|
|
71
|
+
t.value
|
|
72
|
+
];
|
|
73
|
+
switch(op[0]){
|
|
74
|
+
case 0:
|
|
75
|
+
case 1:
|
|
76
|
+
t = op;
|
|
77
|
+
break;
|
|
78
|
+
case 4:
|
|
79
|
+
_.label++;
|
|
80
|
+
return {
|
|
81
|
+
value: op[1],
|
|
82
|
+
done: false
|
|
83
|
+
};
|
|
84
|
+
case 5:
|
|
85
|
+
_.label++;
|
|
86
|
+
y = op[1];
|
|
87
|
+
op = [
|
|
88
|
+
0
|
|
89
|
+
];
|
|
90
|
+
continue;
|
|
91
|
+
case 7:
|
|
92
|
+
op = _.ops.pop();
|
|
93
|
+
_.trys.pop();
|
|
94
|
+
continue;
|
|
95
|
+
default:
|
|
96
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
97
|
+
_ = 0;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
101
|
+
_.label = op[1];
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
105
|
+
_.label = t[1];
|
|
106
|
+
t = op;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
if (t && _.label < t[2]) {
|
|
110
|
+
_.label = t[2];
|
|
111
|
+
_.ops.push(op);
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
if (t[2]) _.ops.pop();
|
|
115
|
+
_.trys.pop();
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
op = body.call(thisArg, _);
|
|
119
|
+
} catch (e) {
|
|
120
|
+
op = [
|
|
121
|
+
6,
|
|
122
|
+
e
|
|
123
|
+
];
|
|
124
|
+
y = 0;
|
|
125
|
+
} finally{
|
|
126
|
+
f = t = 0;
|
|
127
|
+
}
|
|
128
|
+
if (op[0] & 5) throw op[1];
|
|
129
|
+
return {
|
|
130
|
+
value: op[0] ? op[1] : void 0,
|
|
131
|
+
done: true
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var pDebounce = function(fn, wait) {
|
|
136
|
+
var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
137
|
+
if (!Number.isFinite(wait)) {
|
|
138
|
+
throw new TypeError("Expected `wait` to be a finite number");
|
|
139
|
+
}
|
|
140
|
+
var leadingValue;
|
|
141
|
+
var timeout;
|
|
142
|
+
var resolveList = [];
|
|
143
|
+
return function pDebounce() {
|
|
144
|
+
for(var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++){
|
|
145
|
+
arguments_[_key] = arguments[_key];
|
|
146
|
+
}
|
|
147
|
+
var _this = this;
|
|
148
|
+
return new Promise(function(resolve) {
|
|
149
|
+
var shouldCallNow = options.before && !timeout;
|
|
150
|
+
clearTimeout(timeout);
|
|
151
|
+
timeout = setTimeout(function() {
|
|
152
|
+
timeout = null;
|
|
153
|
+
var result = options.before ? leadingValue : fn.apply(_this, arguments_);
|
|
154
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
155
|
+
try {
|
|
156
|
+
for(var _iterator = resolveList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
157
|
+
resolve = _step.value;
|
|
158
|
+
resolve(result);
|
|
159
|
+
}
|
|
160
|
+
} catch (err) {
|
|
161
|
+
_didIteratorError = true;
|
|
162
|
+
_iteratorError = err;
|
|
163
|
+
} finally{
|
|
164
|
+
try {
|
|
165
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
166
|
+
_iterator.return();
|
|
167
|
+
}
|
|
168
|
+
} finally{
|
|
169
|
+
if (_didIteratorError) {
|
|
170
|
+
throw _iteratorError;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
resolveList = [];
|
|
175
|
+
}, wait);
|
|
176
|
+
if (shouldCallNow) {
|
|
177
|
+
leadingValue = fn.apply(_this, arguments_);
|
|
178
|
+
resolve(leadingValue);
|
|
179
|
+
} else {
|
|
180
|
+
resolveList.push(resolve);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
pDebounce.promise = function(function_) {
|
|
186
|
+
var currentPromise;
|
|
187
|
+
return /*#__PURE__*/ _asyncToGenerator(function() {
|
|
188
|
+
var _len, arguments_, _key;
|
|
189
|
+
var _arguments = arguments;
|
|
190
|
+
return __generator(this, function(_state) {
|
|
191
|
+
switch(_state.label){
|
|
192
|
+
case 0:
|
|
193
|
+
for(_len = _arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++){
|
|
194
|
+
arguments_[_key] = _arguments[_key];
|
|
195
|
+
}
|
|
196
|
+
if (currentPromise) {
|
|
197
|
+
return [
|
|
198
|
+
2,
|
|
199
|
+
currentPromise
|
|
200
|
+
];
|
|
201
|
+
}
|
|
202
|
+
_state.label = 1;
|
|
203
|
+
case 1:
|
|
204
|
+
_state.trys.push([
|
|
205
|
+
1,
|
|
206
|
+
,
|
|
207
|
+
3,
|
|
208
|
+
4
|
|
209
|
+
]);
|
|
210
|
+
currentPromise = function_.apply(this, arguments_);
|
|
211
|
+
return [
|
|
212
|
+
4,
|
|
213
|
+
currentPromise
|
|
214
|
+
];
|
|
215
|
+
case 2:
|
|
216
|
+
return [
|
|
217
|
+
2,
|
|
218
|
+
_state.sent()
|
|
219
|
+
];
|
|
220
|
+
case 3:
|
|
221
|
+
currentPromise = undefined;
|
|
222
|
+
return [
|
|
223
|
+
7
|
|
224
|
+
];
|
|
225
|
+
case 4:
|
|
226
|
+
return [
|
|
227
|
+
2
|
|
228
|
+
];
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
var _default = pDebounce;
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@common.js/p-debounce",
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "p-debounce package exported as CommonJS modules",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "etienne-martin/common.js",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=12"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "xo && ava && tsd"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"index.d.ts"
|
|
17
|
+
],
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"ava": "^3.15.0",
|
|
20
|
+
"in-range": "^2.0.0",
|
|
21
|
+
"time-span": "^4.0.0",
|
|
22
|
+
"tsd": "^0.14.0",
|
|
23
|
+
"xo": "^0.37.1",
|
|
24
|
+
"yoctodelay": "^1.1.0"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
27
|
+
"dependencies": {},
|
|
28
|
+
"main": "./index.js"
|
|
29
|
+
}
|