-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PseudoPromise.ts
211 lines (188 loc) · 5.85 KB
/
PseudoPromise.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { INVALID } from "./helpers/util.ts";
import { ZodError } from "./ZodError.ts";
type Func = (arg: any, ctx: { async: boolean }) => any;
type FuncItem = { type: "function"; function: Func };
type Catcher = (error: Error, ctx: { async: boolean }) => any;
type CatcherItem = { type: "catcher"; catcher: Catcher };
type Items = (FuncItem | CatcherItem)[];
export const NOSET = Symbol("no_set");
export class PseudoPromise<PayloadType = undefined> {
readonly _return: PayloadType | undefined;
items: Items;
constructor(funcs: Items = []) {
this.items = funcs;
}
static all = <T extends [PseudoPromise<any>, ...PseudoPromise<any>[]]>(
pps: T
) => {
return new PseudoPromise().all(() => pps);
};
all = <P extends PseudoPromise<any>, T extends [P, ...P[]]>(
func: (arg: PayloadType, ctx: { async: boolean }) => T
): PseudoPromise<
{
[k in keyof T]: T[k] extends PseudoPromise<infer U> ? U : never;
}
> => {
return this.then((arg, ctx) => {
const pps = func(arg, ctx);
if (ctx.async) {
const allValues = Promise.all(
pps.map(async (pp) => {
try {
return await pp.getValueAsync();
} catch (err) {
return INVALID;
}
})
).then((vals) => {
return vals;
});
return allValues;
} else {
return pps.map((pp) => pp.getValueSync()) as any;
}
});
};
static object = (pps: { [k: string]: PseudoPromise<any> }) => {
return new PseudoPromise().then((_arg, ctx) => {
const value: any = {};
const zerr = new ZodError([]);
if (ctx.async) {
const getAsyncObject = async () => {
const items = await Promise.all(
Object.keys(pps).map(async (k) => {
try {
const v = await pps[k].getValueAsync();
return [k, v] as [string, any];
} catch (err) {
if (err instanceof ZodError) {
zerr.addIssues(err.issues);
return [k, INVALID] as [string, any];
}
throw err;
}
})
);
if (!zerr.isEmpty) throw zerr;
for (const item of items) {
if (item[1] !== NOSET) value[item[0]] = item[1];
}
return value;
};
return getAsyncObject();
} else {
const items = Object.keys(pps).map((k) => {
try {
const v = pps[k].getValueSync();
return [k, v] as [string, any];
} catch (err) {
if (err instanceof ZodError) {
zerr.addIssues(err.issues);
return [k, INVALID] as [string, any];
}
throw err;
}
});
if (!zerr.isEmpty) throw zerr;
for (const item of items) {
if (item[1] !== NOSET) value[item[0]] = item[1];
}
return value;
}
});
};
static resolve = <T>(value: T): PseudoPromise<T> => {
if (value instanceof PseudoPromise) {
throw new Error("Do not pass PseudoPromise into PseudoPromise.resolve");
}
return new PseudoPromise().then(() => value) as any;
};
then = <NewPayload>(
func: (arg: PayloadType, ctx: { async: boolean }) => NewPayload
): PseudoPromise<NewPayload extends Promise<infer U> ? U : NewPayload> => {
return new PseudoPromise([
...this.items,
{ type: "function", function: func },
]);
};
// parallel = <
// NewFunc extends (arg: PayloadType, ctx: { async: boolean }) => any,
// ParallelArgs extends [NewFunc, ...NewFunc[]]
// >(
// ...funcs: ParallelArgs
// ): PseudoPromise<
// {
// [k in keyof ParallelArgs]: ParallelArgs[k] extends (
// ...args: any
// ) => infer R
// ? R extends Promise<infer U>
// ? U
// : R
// : never;
// }
// > => {
// return new PseudoPromise([
// ...this.items,
// { type: "function", function: func },
// ]);
// };
catch = (catcher: (err: Error, ctx: { async: boolean }) => unknown): this => {
return new PseudoPromise([
...this.items,
{ type: "catcher", catcher },
]) as this;
};
getValueSync = (): PayloadType => {
let val: any = undefined;
for (let index = 0; index < this.items.length; index++) {
try {
const item = this.items[index];
if (item.type === "function") {
val = item.function(val, { async: false });
}
} catch (err) {
const catcherIndex = this.items.findIndex(
(x, i) => x.type === "catcher" && i > index
);
const catcherItem = this.items[catcherIndex];
if (!catcherItem || catcherItem.type !== "catcher") {
throw err;
} else {
index = catcherIndex;
val = catcherItem.catcher(err, { async: false });
}
}
}
return val;
};
getValueAsync = async (): Promise<PayloadType> => {
let val: any = undefined;
for (let index = 0; index < this.items.length; index++) {
const item = this.items[index];
try {
if (item.type === "function") {
val = await item.function(val, { async: true });
}
} catch (err) {
const catcherIndex = this.items.findIndex(
(x, i) => x.type === "catcher" && i > index
);
const catcherItem = this.items[catcherIndex];
if (!catcherItem || catcherItem.type !== "catcher") {
throw err;
} else {
index = catcherIndex;
val = await catcherItem.catcher(err, { async: true });
}
}
if (val instanceof PseudoPromise) {
throw new Error("ASYNC: DO NOT RETURN PSEUDOPROMISE FROM FUNCTIONS");
}
if (val instanceof Promise) {
throw new Error("ASYNC: DO NOT RETURN PROMISE FROM FUNCTIONS");
}
}
return val;
};
}