@cjser/p-debounce 5.1.0-cjser.2 → 5.1.1-cjser.2
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/dist-cjser/index.cjs +4 -2
- package/index.d.ts +1 -1
- package/index.js +3 -1
- package/package.json +3 -3
- package/readme.md +29 -0
package/dist-cjser/index.cjs
CHANGED
|
@@ -16,7 +16,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
};
|
|
17
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
18
|
|
|
19
|
-
// packages/@cjser/p-debounce/index.js
|
|
19
|
+
// packages/@cjser/p-debounce.tmp-26-1789230473445/index.js
|
|
20
20
|
var index_exports = {};
|
|
21
21
|
__export(index_exports, {
|
|
22
22
|
default: () => index_default
|
|
@@ -107,10 +107,12 @@ pDebounce.promise = (function_, options = {}) => {
|
|
|
107
107
|
currentPromise = (async () => {
|
|
108
108
|
let result;
|
|
109
109
|
let initialError;
|
|
110
|
+
let didReject = false;
|
|
110
111
|
try {
|
|
111
112
|
result = await function_.apply(this, arguments_);
|
|
112
113
|
} catch (error) {
|
|
113
114
|
initialError = error;
|
|
115
|
+
didReject = true;
|
|
114
116
|
}
|
|
115
117
|
while (queuedCall) {
|
|
116
118
|
const call = queuedCall;
|
|
@@ -126,7 +128,7 @@ pDebounce.promise = (function_, options = {}) => {
|
|
|
126
128
|
}
|
|
127
129
|
}
|
|
128
130
|
}
|
|
129
|
-
if (
|
|
131
|
+
if (didReject) {
|
|
130
132
|
throw initialError;
|
|
131
133
|
}
|
|
132
134
|
return result;
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -110,11 +110,13 @@ pDebounce.promise = (function_, options = {}) => {
|
|
|
110
110
|
currentPromise = (async () => {
|
|
111
111
|
let result;
|
|
112
112
|
let initialError;
|
|
113
|
+
let didReject = false;
|
|
113
114
|
|
|
114
115
|
try {
|
|
115
116
|
result = await function_.apply(this, arguments_);
|
|
116
117
|
} catch (error) {
|
|
117
118
|
initialError = error;
|
|
119
|
+
didReject = true;
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
// Process queued calls regardless of initial result
|
|
@@ -135,7 +137,7 @@ pDebounce.promise = (function_, options = {}) => {
|
|
|
135
137
|
}
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
if (
|
|
140
|
+
if (didReject) {
|
|
139
141
|
throw initialError;
|
|
140
142
|
}
|
|
141
143
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cjser/p-debounce",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.1-cjser.2",
|
|
4
4
|
"description": "Debounce promise-returning & async functions",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -58,11 +58,11 @@
|
|
|
58
58
|
"types": "./index.d.ts",
|
|
59
59
|
"main": "./dist-cjser/index.cjs",
|
|
60
60
|
"cjser": {
|
|
61
|
-
"sourceVersion": "5.1.
|
|
61
|
+
"sourceVersion": "5.1.1",
|
|
62
62
|
"cjserVersion": 2,
|
|
63
63
|
"original": {
|
|
64
64
|
"name": "p-debounce",
|
|
65
|
-
"version": "5.1.
|
|
65
|
+
"version": "5.1.1",
|
|
66
66
|
"exports": {
|
|
67
67
|
"types": "./index.d.ts",
|
|
68
68
|
"default": "./index.js"
|
package/readme.md
CHANGED
|
@@ -127,6 +127,35 @@ debouncedSave('data2'); // This will run after the first save completes
|
|
|
127
127
|
//=> Saved: data2
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
+
## Recipes
|
|
131
|
+
|
|
132
|
+
### Autosave
|
|
133
|
+
|
|
134
|
+
For autosave, combine trailing debounce with serialization to prevent concurrent saves from racing:
|
|
135
|
+
|
|
136
|
+
```js
|
|
137
|
+
import pDebounce from 'p-debounce';
|
|
138
|
+
|
|
139
|
+
const saveDocument = async content => {
|
|
140
|
+
await fetch('/api/save', {
|
|
141
|
+
method: 'POST',
|
|
142
|
+
body: JSON.stringify({content}),
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// Serialize saves, then debounce keystrokes
|
|
147
|
+
const serializedSave = pDebounce.promise(saveDocument, {after: true});
|
|
148
|
+
const autosave = pDebounce(serializedSave, 1000);
|
|
149
|
+
|
|
150
|
+
textArea.addEventListener('input', () => {
|
|
151
|
+
autosave(textArea.value);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// 1. Waits 1s after typing stops
|
|
155
|
+
// 2. If user types during save, queues next save with latest content
|
|
156
|
+
// 3. Prevents race conditions where slow saves overwrite newer data
|
|
157
|
+
```
|
|
158
|
+
|
|
130
159
|
## Related
|
|
131
160
|
|
|
132
161
|
- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
|