@pistonite/pure 0.26.7 → 0.26.8
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/sync/index.ts +2 -1
- package/src/sync/util.ts +18 -13
package/package.json
CHANGED
package/src/sync/index.ts
CHANGED
|
@@ -14,8 +14,9 @@ export { latest, type LatestConstructor, type UpdateArgsFn } from "./latest.ts";
|
|
|
14
14
|
export { debounce, type DebounceConstructor } from "./debounce.ts";
|
|
15
15
|
export { batch, type BatchConstructor } from "./batch.ts";
|
|
16
16
|
export { once, type OnceConstructor } from "./once.ts";
|
|
17
|
+
export { makePromise, type PromiseHandle } from "./util.ts";
|
|
17
18
|
|
|
18
|
-
// types
|
|
19
|
+
// helper types
|
|
19
20
|
export type { AnyFn, AwaitRet } from "./util.ts";
|
|
20
21
|
|
|
21
22
|
// unstable
|
package/src/sync/util.ts
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
let
|
|
1
|
+
/**
|
|
2
|
+
* Make a {@link PromiseHandle} with the promise object separate from
|
|
3
|
+
* its resolve and reject methods
|
|
4
|
+
*/
|
|
5
|
+
export const makePromise = <T>(): PromiseHandle<T> => {
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
let resolve: any;
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
let reject: any;
|
|
8
10
|
const promise = new Promise<T>((res, rej) => {
|
|
9
11
|
resolve = res;
|
|
10
12
|
reject = rej;
|
|
11
13
|
});
|
|
12
|
-
if (!resolve || !reject) {
|
|
13
|
-
throw new Error(
|
|
14
|
-
"Promise callbacks not set. This is a bug in the JS engine!",
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
14
|
return { promise, resolve, reject };
|
|
18
15
|
};
|
|
19
16
|
|
|
20
|
-
|
|
17
|
+
/**
|
|
18
|
+
* A handle of the promise that breaks down the promise object
|
|
19
|
+
* and its resolve and reject functions
|
|
20
|
+
*/
|
|
21
|
+
export type PromiseHandle<T> = {
|
|
22
|
+
promise: Promise<T>;
|
|
23
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
24
|
+
reject: (reason?: unknown) => void;
|
|
25
|
+
};
|
|
21
26
|
|
|
22
27
|
/** Shorthand for Awaited<ReturnType<T>> */
|
|
23
28
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|