socket-function 0.20.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.20.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
@@ -158,7 +158,7 @@ export class PromiseObj<T = void> {
158
158
  /** Resolve called does not mean the value is ready, as it may be resolved with a promise. */
159
159
  public resolveCalled?: boolean;
160
160
 
161
- public resolve(value: T | Promise<T>) {
161
+ public resolve = (value: T | Promise<T>) => {
162
162
  this.resolveCalled = true;
163
163
  if (typeof value === "object" && value !== null && value instanceof Promise) {
164
164
  value.then(
@@ -169,10 +169,10 @@ export class PromiseObj<T = void> {
169
169
  this.value = { value };
170
170
  }
171
171
  this.baseResolve(value);
172
- }
173
- public reject(error: any) {
172
+ };
173
+ public reject = (error: any) => {
174
174
  this.baseReject(error);
175
- }
175
+ };
176
176
 
177
177
  private baseResolve!: (value: T | Promise<T>) => void;
178
178
  private baseReject!: (error: any) => void;
@@ -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
 
@@ -19,7 +19,7 @@ export function getOpenTimesBase(): OwnTimeObjInternal[] {
19
19
  return openTimes;
20
20
  }
21
21
 
22
- (global as any).pendingOwnCallTime = openTimes;
22
+ (globalThis as any).pendingOwnCallTime = openTimes;
23
23
 
24
24
  // NOTE: This overhead time is actually mostly for aggregate time, but it is needed,
25
25
  // otherwise we consistently underestimate the time spent.