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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/misc.ts +29 -35
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "note1": "note on node-forge fork, see https://github.com/digitalbazaar/forge/issues/744 for details",
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
- result.finally(() => {
214
- afterCall(Date.now() + delay);
215
- }).catch(e => console.error(e));
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
- afterCall(Date.now() + delay);
219
+ nextAllowedCall = Date.now() + delay;
220
+ runNextCall();
218
221
  }
219
222
  } catch (e: any) {
220
- debugger;
221
223
  promiseObj.reject(e);
222
- afterCall(Date.now() + delay);
224
+ nextAllowedCall = Date.now() + delay;
225
+ runNextCall();
223
226
  }
224
227
  }
225
- function afterCall(setNextAllowedCall: number | undefined, time = Date.now()) {
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
- let args = pendingArgs;
236
- pendingArgs = undefined;
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
- if (pendingArgs) {
244
- let args = pendingArgs;
245
- pendingArgs = undefined;
246
- doCall(args.args, args.promiseObj);
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
- }, nextAllowedCall - time);
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
- let time = Date.now();
257
- if (time > nextAllowedCall) {
258
- let promise = promiseObj();
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