@httptoolkit/util 0.1.0 → 0.1.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.
@@ -1,5 +1,12 @@
1
1
  export type MaybePromise<T> = T | Promise<T>;
2
- export declare const delay: (ms: number) => Promise<unknown>;
2
+ export declare const delay: (ms: number, options?: {
3
+ /**
4
+ * If set, if the code is running in a compatible runtime (i.e. Node.js)
5
+ * then the .unref() will be used to ensure that this delay does not
6
+ * block the process shutdown.
7
+ */
8
+ unref?: boolean;
9
+ }) => Promise<unknown>;
3
10
  export declare function doWhile<T>(doFn: () => Promise<T>, whileFn: () => Promise<boolean> | boolean): Promise<void>;
4
11
  export interface Deferred<T> {
5
12
  resolve: (arg: T) => void;
package/dist/promises.js CHANGED
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getDeferred = exports.doWhile = exports.delay = void 0;
4
- const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
4
+ const delay = (ms, options = {}) => new Promise((resolve) => {
5
+ const timer = setTimeout(resolve, ms);
6
+ if (options.unref && timer.unref) {
7
+ timer.unref();
8
+ }
9
+ });
5
10
  exports.delay = delay;
6
11
  async function doWhile(doFn, whileFn) {
7
12
  do {
@@ -1 +1 @@
1
- {"version":3,"file":"promises.js","sourceRoot":"","sources":["../src/promises.ts"],"names":[],"mappings":";;;AAEO,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,EAAE,CAChC,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CACnD,CAAC;AAFW,QAAA,KAAK,SAEhB;AAEK,KAAK,UAAU,OAAO,CACzB,IAAsB,EACtB,OAAyC;IAEzC,GAAG;QACC,MAAM,IAAI,EAAE,CAAC;KAChB,QAAQ,MAAM,OAAO,EAAE,EAAE;AAC9B,CAAC;AAPD,0BAOC;AAQD,SAAg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O,EAAE,MAAM,EAAE,OAAO,EAAS,CAAC;AAC/C,CAAC;AAZD,kCAYC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@httptoolkit/util",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "A tiny utility package, sharing JS code widely used across HTTP Toolkit projects",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -27,7 +27,7 @@
27
27
  "url": "https://github.com/httptoolkit/httptoolkit-js-util/issues"
28
28
  },
29
29
  "homepage": "https://github.com/httptoolkit/httptoolkit-js-util#readme",
30
- "dependencies": {
30
+ "devDependencies": {
31
31
  "typescript": "5.2.2"
32
32
  }
33
33
  }
package/src/promises.ts CHANGED
@@ -1,8 +1,20 @@
1
1
  export type MaybePromise<T> = T | Promise<T>;
2
2
 
3
- export const delay = (ms: number) =>
4
- new Promise((resolve) => setTimeout(resolve, ms)
5
- );
3
+ export const delay = (ms: number, options: {
4
+ /**
5
+ * If set, if the code is running in a compatible runtime (i.e. Node.js)
6
+ * then the .unref() will be used to ensure that this delay does not
7
+ * block the process shutdown.
8
+ */
9
+ unref?: boolean
10
+ } = {}) =>
11
+ new Promise((resolve) => {
12
+ const timer = setTimeout(resolve, ms);
13
+
14
+ if (options.unref && (timer as any).unref) {
15
+ (timer as any).unref();
16
+ }
17
+ });
6
18
 
7
19
  export async function doWhile<T>(
8
20
  doFn: () => Promise<T>,