-
-
Notifications
You must be signed in to change notification settings - Fork 286
/
AnyToPromise.ts
181 lines (146 loc) · 3.91 KB
/
AnyToPromise.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {isObject} from "../utils/objects/isObject.js";
import {isObservable} from "../utils/objects/isObservable.js";
import {isPromise} from "../utils/objects/isPromise.js";
import {isStream} from "../utils/objects/isStream.js";
/**
* @ignore
*/
function isResponse(obj: any) {
return isObject(obj) && "data" in obj && "headers" in obj && "status" in obj && "statusText" in obj;
}
/**
* @ignore
*/
export enum AnyToPromiseStatus {
PENDING = "PENDING",
CANCELED = "CANCELED",
RESOLVED = "RESOLVED",
REJECTED = "REJECTED"
}
export enum AnyToPromiseResponseTypes {
STREAM = "STREAM",
BUFFER = "BUFFER",
DATA = "DATA"
}
export interface AnyPromiseResult<T = any> {
state: AnyToPromiseStatus;
type: AnyToPromiseResponseTypes;
status?: number;
headers?: Record<string, any>;
data: T;
}
export class AnyToPromise<T = any> {
public status = AnyToPromiseStatus.PENDING;
public args: any[];
#resolves: any;
#rejects: any;
#hasNextFunction = false;
readonly #promise: Promise<AnyPromiseResult<T>>;
constructor({hasNextFunction = false}: {hasNextFunction?: boolean} = {}) {
this.#hasNextFunction = hasNextFunction;
this.#promise = new Promise((resolve: any, reject: any) => {
this.#resolves = resolve;
this.#rejects = reject;
});
}
isDone(): boolean {
return this.status !== AnyToPromiseStatus.PENDING;
}
get next() {
this.#hasNextFunction = true;
return (error?: any) => {
if (this.isDone()) {
return;
}
return error ? this.reject(error) : this.resolve({type: AnyToPromiseResponseTypes.DATA});
};
}
/**
*
*/
call(cb: Function): Promise<AnyPromiseResult<T>> {
if (this.isDone()) {
return Promise.resolve(this as any);
}
try {
this.handle(cb());
} catch (er) {
this.reject(er);
}
return this.#promise;
}
reject(er: any) {
if (this.isDone()) {
return;
}
this.done(AnyToPromiseStatus.REJECTED);
this.#rejects(er);
}
resolve(response: any = {}) {
if (this.isDone()) {
return;
}
this.done(AnyToPromiseStatus.RESOLVED);
this.#resolves({...response, state: AnyToPromiseStatus.RESOLVED});
}
destroy() {}
cancel() {
if (this.isDone()) {
return;
}
this.done(AnyToPromiseStatus.CANCELED);
return this.#resolves({state: AnyToPromiseStatus.CANCELED});
}
done(status: AnyToPromiseStatus) {
this.destroy();
this.status = status;
}
handle(process: any, additionalProps = {}): any {
if (this.isDone()) {
return;
}
if (process) {
if (this.isCanceledResponse(process)) {
// ABANDON
return this.cancel();
}
if (isObservable(process)) {
process = process.toPromise();
}
if (isResponse(process)) {
return this.handle(process.data, {
...additionalProps,
status: process.status,
headers: process.headers
});
}
if (isStream(process)) {
return this.resolve({...additionalProps, type: AnyToPromiseResponseTypes.STREAM, data: process});
}
if (isStream(process) || Buffer.isBuffer(process)) {
return this.resolve({...additionalProps, type: AnyToPromiseResponseTypes.BUFFER, data: process});
}
if (isPromise(process)) {
return process
.then((result: any) => this.handle(result, additionalProps))
.catch((error: any) => {
if (error.response && isResponse(error.response)) {
return this.handle(error.response);
}
return this.reject(error);
});
}
}
if (!this.#hasNextFunction) {
// no next function and empty response
return this.resolve({
...additionalProps,
data: process,
type: AnyToPromiseResponseTypes.DATA
});
}
}
protected isCanceledResponse(process: any) {
return process === AnyToPromiseStatus.CANCELED;
}
}