-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise-chain.ts
68 lines (57 loc) · 1.89 KB
/
promise-chain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { PromiseChainable, PromiseChainableConstructor } from "./types.ts";
/**
* Utility class to wrap a composition class with the intended purpose of chaining methods, specifically useful for
* functions that return Promises. Note: Promise functions and non-promise functions can be mixed.
*
* ### Example
*
* ```ts
* const testClass = new TestClassWithSyncAndAsyncMethods();
* const result = await PromiseChain(testClass)
.asyncIncrement("propertyOne", 3)
.asyncIncrementTwo()
.asyncIncrementOne()
.increment("propertyTwo", 5)
.increment("propertyOne", 2)
.asyncIncrementOne();
* ```
*
*/
const PromiseChain = function <T extends Record<string, unknown>>(
this: PromiseChainable<T> | void,
obj: T,
) {
if (!(this instanceof PromiseChain)) {
return new PromiseChain(obj);
} else {
const self = this as unknown as { _valuePromise: Promise<T> } & Promise<T>;
self._valuePromise = Promise.resolve(obj);
this.then = (...args) => self._valuePromise.then(...args);
this.catch = (...args) => self._valuePromise.catch(...args);
this.finally = (...args) => self._valuePromise.finally(...args);
keysOfObject(obj).forEach((key) => {
const callableFunc = obj[key];
if (!(callableFunc instanceof Function)) {
return;
}
Object.defineProperty(this, key, {
value: (...args: unknown[]) => {
self._valuePromise = self._valuePromise.then((val: T) =>
callableFunc.apply(val, args)
);
return this;
},
});
});
}
} as PromiseChainableConstructor;
function keysOfObject<T extends Record<string, unknown>>(
obj: T,
): Array<keyof T> {
const proto = Object.getPrototypeOf(obj);
const keys = Object.keys(obj).concat(
Object.getOwnPropertyNames(proto).filter((name) => name !== "constructor"),
);
return keys as Array<keyof T>;
}
export default PromiseChain;