socket-function 0.21.0 → 0.22.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/package.json +1 -1
- package/src/misc.ts +29 -35
package/package.json
CHANGED
package/src/misc.ts
CHANGED
|
@@ -205,47 +205,48 @@ export function throttleFunction<Args extends any[]>(
|
|
|
205
205
|
let nextAllowedCall = 0;
|
|
206
206
|
let pendingArgs: { args: Args; promiseObj: PromiseObj<void> } | undefined = undefined;
|
|
207
207
|
function doCall(args: Args, promiseObj: PromiseObj<void>) {
|
|
208
|
-
nextAllowedCall = Number.POSITIVE_INFINITY;
|
|
209
208
|
try {
|
|
210
209
|
let result = fnc(...args);
|
|
211
210
|
promiseObj.resolve(result);
|
|
212
211
|
if (result instanceof Promise) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
212
|
+
// NOTE: The caller should handle the promise. If not, they probably
|
|
213
|
+
// want the unresolved promise rejection, so they can handle it properly.
|
|
214
|
+
void result.finally(() => {
|
|
215
|
+
nextAllowedCall = Date.now() + delay;
|
|
216
|
+
runNextCall();
|
|
217
|
+
});
|
|
216
218
|
} else {
|
|
217
|
-
|
|
219
|
+
nextAllowedCall = Date.now() + delay;
|
|
220
|
+
runNextCall();
|
|
218
221
|
}
|
|
219
222
|
} catch (e: any) {
|
|
220
|
-
debugger;
|
|
221
223
|
promiseObj.reject(e);
|
|
222
|
-
|
|
224
|
+
nextAllowedCall = Date.now() + delay;
|
|
225
|
+
runNextCall();
|
|
223
226
|
}
|
|
224
227
|
}
|
|
225
|
-
function
|
|
226
|
-
|
|
227
|
-
// NOTE: Ignore error, we really shouldn't have any here
|
|
228
|
-
if (setNextAllowedCall) {
|
|
229
|
-
nextAllowedCall = setNextAllowedCall;
|
|
230
|
-
} else {
|
|
231
|
-
if (nextAllowedCall === Number.POSITIVE_INFINITY) return;
|
|
232
|
-
}
|
|
228
|
+
function runNextCall() {
|
|
229
|
+
if (nextAllowedCall === Number.POSITIVE_INFINITY) return;
|
|
233
230
|
if (!pendingArgs) return;
|
|
231
|
+
let time = Date.now();
|
|
234
232
|
if (time > nextAllowedCall) {
|
|
235
|
-
|
|
236
|
-
|
|
233
|
+
// Set nextAllowedCall to infinity, to prevent new calls from running
|
|
234
|
+
// until doCall finishes.
|
|
235
|
+
nextAllowedCall = Number.POSITIVE_INFINITY;
|
|
237
236
|
// Delay, so we don't turn a series of sequential calls to a series of nested calls
|
|
238
237
|
// (which will cause a stack overflow)
|
|
239
|
-
nextAllowedCall = Number.POSITIVE_INFINITY;
|
|
240
|
-
setImmediate(() => doCall(args.args, args.promiseObj));
|
|
241
|
-
} else {
|
|
242
238
|
setTimeout(() => {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
239
|
+
let args = pendingArgs;
|
|
240
|
+
pendingArgs = undefined;
|
|
241
|
+
if (!args) {
|
|
242
|
+
nextAllowedCall = 0;
|
|
243
|
+
console.warn(`Impossible, pendingArgs was reset when we shouldn't have even been in a call`);
|
|
244
|
+
return;
|
|
247
245
|
}
|
|
248
|
-
|
|
246
|
+
doCall(args.args, args.promiseObj);
|
|
247
|
+
}, 0);
|
|
248
|
+
} else {
|
|
249
|
+
setTimeout(runNextCall, nextAllowedCall - time);
|
|
249
250
|
}
|
|
250
251
|
}
|
|
251
252
|
return function (...args: Args): Promise<void> {
|
|
@@ -253,16 +254,9 @@ export function throttleFunction<Args extends any[]>(
|
|
|
253
254
|
pendingArgs.args = args;
|
|
254
255
|
return pendingArgs.promiseObj.promise;
|
|
255
256
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
doCall(args, promise);
|
|
260
|
-
return promise.promise;
|
|
261
|
-
} else {
|
|
262
|
-
pendingArgs = { args, promiseObj: promiseObj() };
|
|
263
|
-
afterCall(undefined, time);
|
|
264
|
-
return pendingArgs.promiseObj.promise;
|
|
265
|
-
}
|
|
257
|
+
pendingArgs = { args, promiseObj: promiseObj() };
|
|
258
|
+
runNextCall();
|
|
259
|
+
return pendingArgs.promiseObj.promise;
|
|
266
260
|
};
|
|
267
261
|
}
|
|
268
262
|
|