@everyonesoftware/common 2.0.0 → 3.0.0
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/.github/workflows/publish.yml +54 -36
- package/package.json +6 -2
- package/sources/asyncIterator.ts +436 -436
- package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +47 -47
- package/sources/asyncResult.ts +95 -95
- package/sources/byteList.ts +201 -201
- package/sources/byteListStream.ts +120 -120
- package/sources/byteReadStream.ts +23 -23
- package/sources/byteWriteStream.ts +15 -15
- package/sources/characterList.ts +194 -194
- package/sources/characterListStream.ts +150 -150
- package/sources/characterReadStream.ts +80 -80
- package/sources/characterReadStreamIterator.ts +127 -127
- package/sources/concatenateIterable.ts +118 -118
- package/sources/concatenateIterator.ts +164 -164
- package/sources/currentProcess.ts +157 -157
- package/sources/dateTime.ts +129 -129
- package/sources/depthFirstSearch.ts +229 -229
- package/sources/flatMapIterable.ts +103 -103
- package/sources/flatMapIterator.ts +151 -151
- package/sources/generator.ts +250 -250
- package/sources/index.ts +1 -1
- package/sources/iterator.ts +480 -480
- package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +123 -123
- package/sources/javascriptSetSet.ts +133 -133
- package/sources/listQueue.ts +61 -61
- package/sources/listStack.ts +61 -61
- package/sources/luxonDateTime.ts +108 -108
- package/sources/mapAsyncIterator.ts +140 -140
- package/sources/mutableMap.ts +291 -291
- package/sources/node.ts +36 -36
- package/sources/promiseAsyncResult.ts +173 -173
- package/sources/queue.ts +48 -48
- package/sources/recreationDotGovClient.ts +258 -258
- package/sources/searchControl.ts +41 -41
- package/sources/set.ts +243 -243
- package/sources/skipAsyncIterator.ts +144 -144
- package/sources/stack.ts +47 -47
- package/sources/syncResult.ts +299 -299
- package/sources/takeAsyncIterator.ts +140 -140
- package/sources/whereAsyncIterator.ts +142 -142
- package/sources/wonderlandTrailClient.ts +1502 -1502
- package/tests/assertTestTests.ts +74 -74
- package/tests/byteListStreamTests.ts +389 -389
- package/tests/byteListTests.ts +26 -26
- package/tests/characterListStreamTests.ts +390 -390
- package/tests/characterListTests.ts +249 -249
- package/tests/dateTimeTests.ts +29 -29
- package/tests/depthFirstSearchTests.ts +105 -105
- package/tests/generatorTests.ts +85 -85
- package/tests/mutableMapTests.ts +153 -153
- package/tests/promiseAsyncResultTests.ts +687 -687
- package/tests/queueTests.ts +28 -28
- package/tests/recreationDotGovClientTests.ts +190 -190
- package/tests/setTests.ts +139 -139
- package/tests/stackTests.ts +65 -65
- package/tests/syncResultTests.ts +1250 -1250
- package/tests/wonderlandTrailClientTests.ts +451 -451
- package/tsup.config.ts +12 -12
package/sources/syncResult.ts
CHANGED
|
@@ -1,300 +1,300 @@
|
|
|
1
|
-
import { PreCondition } from "./preCondition";
|
|
2
|
-
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
3
|
-
import { instanceOfType, isPromise, isPromiseLike, isUndefinedOrNull, Type } from "./types";
|
|
4
|
-
import { AsyncResult } from "./asyncResult";
|
|
5
|
-
|
|
6
|
-
export class SyncResult<T> implements AsyncResult<T>
|
|
7
|
-
{
|
|
8
|
-
private value: T | undefined;
|
|
9
|
-
private error: unknown | undefined;
|
|
10
|
-
|
|
11
|
-
private constructor(value: T | undefined, error: unknown | undefined)
|
|
12
|
-
{
|
|
13
|
-
PreCondition.assertTrue(value === undefined || error === undefined, "value === undefined || error === undefined", "Either value or error must be undefined.");
|
|
14
|
-
|
|
15
|
-
this.value = value;
|
|
16
|
-
this.error = error;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public static create<T>(action: () => T): SyncResult<T>
|
|
20
|
-
{
|
|
21
|
-
PreCondition.assertNotUndefinedAndNotNull(action, "action");
|
|
22
|
-
|
|
23
|
-
let value: T | undefined;
|
|
24
|
-
let error: unknown | undefined;
|
|
25
|
-
try
|
|
26
|
-
{
|
|
27
|
-
value = action();
|
|
28
|
-
}
|
|
29
|
-
catch (e)
|
|
30
|
-
{
|
|
31
|
-
error = e;
|
|
32
|
-
}
|
|
33
|
-
return new SyncResult(value, error);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public static value<T>(value: T): SyncResult<T>
|
|
37
|
-
{
|
|
38
|
-
return new SyncResult(value, undefined);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public static error<T>(error: unknown): SyncResult<T>
|
|
42
|
-
{
|
|
43
|
-
return new SyncResult<T>(undefined, error);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public await(): T
|
|
47
|
-
{
|
|
48
|
-
if (this.error)
|
|
49
|
-
{
|
|
50
|
-
throw this.error;
|
|
51
|
-
}
|
|
52
|
-
return this.value!;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public then<TResult1 = T, TResult2 = never>(onfullfilled?: ((value: T) => TResult1) | null, onrejected?: ((reason: unknown) => TResult2) | null): SyncResult<TResult1 | TResult2>;
|
|
56
|
-
public then<TResult1 = T, TResult2 = never>(onfullfilled?: ((value: T) => (TResult1 | PromiseLike<TResult1>)) | null, onrejected?: ((reason: unknown) => (TResult2 | PromiseLike<TResult2>)) | null): SyncResult<TResult1 | TResult2>;
|
|
57
|
-
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => (TResult1 | PromiseLike<TResult1>)) | null, onrejected?: ((reason: unknown) => (TResult2 | PromiseLike<TResult2>)) | null): SyncResult<TResult1 | TResult2> | PromiseAsyncResult<TResult1 | TResult2>
|
|
58
|
-
{
|
|
59
|
-
let result: SyncResult<TResult1 | TResult2> | PromiseAsyncResult<TResult1 | TResult2>;
|
|
60
|
-
if (this.error)
|
|
61
|
-
{
|
|
62
|
-
if (onrejected)
|
|
63
|
-
{
|
|
64
|
-
try
|
|
65
|
-
{
|
|
66
|
-
const onRejectedResult: TResult2 | PromiseLike<TResult2> = onrejected(this.error);
|
|
67
|
-
if (onRejectedResult instanceof SyncResult || onRejectedResult instanceof PromiseAsyncResult)
|
|
68
|
-
{
|
|
69
|
-
result = onRejectedResult;
|
|
70
|
-
}
|
|
71
|
-
else if (isPromise<TResult2>(onRejectedResult))
|
|
72
|
-
{
|
|
73
|
-
result = PromiseAsyncResult.create(onRejectedResult);
|
|
74
|
-
}
|
|
75
|
-
else
|
|
76
|
-
{
|
|
77
|
-
result = SyncResult.value(onRejectedResult as TResult2);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
catch (error)
|
|
81
|
-
{
|
|
82
|
-
result = SyncResult.error(error);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
else
|
|
86
|
-
{
|
|
87
|
-
result = SyncResult.error<TResult2>(this.error);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else
|
|
91
|
-
{
|
|
92
|
-
if (onfulfilled)
|
|
93
|
-
{
|
|
94
|
-
try
|
|
95
|
-
{
|
|
96
|
-
const onFullfilledResult: TResult1 | PromiseLike<TResult1> = onfulfilled(this.value!);
|
|
97
|
-
if (onFullfilledResult instanceof SyncResult || onFullfilledResult instanceof PromiseAsyncResult)
|
|
98
|
-
{
|
|
99
|
-
result = onFullfilledResult;
|
|
100
|
-
}
|
|
101
|
-
else if (isPromise<TResult1>(onFullfilledResult))
|
|
102
|
-
{
|
|
103
|
-
result = PromiseAsyncResult.create(onFullfilledResult);
|
|
104
|
-
}
|
|
105
|
-
else
|
|
106
|
-
{
|
|
107
|
-
result = SyncResult.value(onFullfilledResult as TResult1);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
catch (error)
|
|
111
|
-
{
|
|
112
|
-
result = SyncResult.error(error);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
else
|
|
116
|
-
{
|
|
117
|
-
result = SyncResult.value<TResult1>(this.value as TResult1);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return result;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
public onValue(onValueFunction: (value: T) => void): SyncResult<T>;
|
|
124
|
-
public onValue(onValueFunction: (value: T) => Promise<void>): PromiseAsyncResult<T>;
|
|
125
|
-
onValue(onValueFunction: (value: T) => (void | Promise<void>)): SyncResult<T> | PromiseAsyncResult<T>
|
|
126
|
-
{
|
|
127
|
-
return this.then<T>((value: T) =>
|
|
128
|
-
{
|
|
129
|
-
let result: SyncResult<T> | PromiseAsyncResult<T>;
|
|
130
|
-
try
|
|
131
|
-
{
|
|
132
|
-
const onValueFunctionResult: (void | Promise<void>) = onValueFunction(value);
|
|
133
|
-
if (isPromise(onValueFunctionResult))
|
|
134
|
-
{
|
|
135
|
-
result = PromiseAsyncResult.create(onValueFunctionResult.then(() => value));
|
|
136
|
-
}
|
|
137
|
-
else
|
|
138
|
-
{
|
|
139
|
-
result = this;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
catch (error)
|
|
143
|
-
{
|
|
144
|
-
result = SyncResult.error(error);
|
|
145
|
-
}
|
|
146
|
-
return result;
|
|
147
|
-
})
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
public catch<TResult = never>(onrejected: (reason: unknown) => TResult): SyncResult<T | TResult>;
|
|
151
|
-
public catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): SyncResult<T | TResult> | PromiseAsyncResult<T | TResult>
|
|
152
|
-
public catch<TError, TResult = never>(errorType: Type<TError>, onrejected: (reason: TError) => TResult): SyncResult<T | TResult>;
|
|
153
|
-
public catch<TError, TResult = never>(errorType: Type<TError>, onrejected: (reason: TError) => PromiseLike<TResult>): PromiseAsyncResult<T | TResult>;
|
|
154
|
-
catch<TResult = never>(errorTypeOrOnRejected?: Type<unknown> | ((reason: unknown) => TResult | PromiseLike<TResult>) | null, onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): SyncResult<T | TResult> | PromiseAsyncResult<T | TResult>
|
|
155
|
-
{
|
|
156
|
-
let errorType: Type<TypeError> | undefined;
|
|
157
|
-
if (!isUndefinedOrNull(onrejected))
|
|
158
|
-
{
|
|
159
|
-
errorType = errorTypeOrOnRejected as Type<unknown>;
|
|
160
|
-
|
|
161
|
-
PreCondition.assertNotUndefinedAndNotNull(errorType, "errorType");
|
|
162
|
-
}
|
|
163
|
-
else
|
|
164
|
-
{
|
|
165
|
-
onrejected = errorTypeOrOnRejected as ((reason: any) => TResult | PromiseLike<TResult>) | null;
|
|
166
|
-
}
|
|
167
|
-
PreCondition.assertNotUndefinedAndNotNull(onrejected, "onrejected");
|
|
168
|
-
|
|
169
|
-
let result: SyncResult<T | TResult> | PromiseAsyncResult<T | TResult> = this;
|
|
170
|
-
if (this.error && (!errorType || instanceOfType(this.error, errorType)))
|
|
171
|
-
{
|
|
172
|
-
try
|
|
173
|
-
{
|
|
174
|
-
const onRejectedResult: TResult | PromiseLike<TResult> = onrejected(this.error);
|
|
175
|
-
if (onRejectedResult instanceof SyncResult || onRejectedResult instanceof PromiseAsyncResult)
|
|
176
|
-
{
|
|
177
|
-
result = onRejectedResult;
|
|
178
|
-
}
|
|
179
|
-
else if (isPromise<TResult>(onRejectedResult))
|
|
180
|
-
{
|
|
181
|
-
result = PromiseAsyncResult.create(onRejectedResult);
|
|
182
|
-
}
|
|
183
|
-
else
|
|
184
|
-
{
|
|
185
|
-
result = SyncResult.value(onRejectedResult as TResult);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
catch (error)
|
|
189
|
-
{
|
|
190
|
-
result = SyncResult.error(error);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return result;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
public onError(onErrorFunction: (reason: unknown) => void): SyncResult<T>;
|
|
197
|
-
public onError(onErrorFunction: (reason: unknown) => PromiseLike<void>): PromiseAsyncResult<T>;
|
|
198
|
-
public onError<TError>(errorType: Type<TError>, onErrorFunction: (reason: TError) => void): SyncResult<T>;
|
|
199
|
-
public onError<TError>(errorType: Type<TError>, onErrorFunction: (reason: TError) => PromiseLike<void>): PromiseAsyncResult<T>;
|
|
200
|
-
onError(errorTypeOrOnErrorFunction: Type<unknown> | ((reason: unknown) => (void | PromiseLike<void>)), onErrorFunction?: (reason: unknown) => (void | PromiseLike<void>)): SyncResult<T> | PromiseAsyncResult<T>
|
|
201
|
-
{
|
|
202
|
-
let errorType: Type<TypeError> | undefined;
|
|
203
|
-
if (!isUndefinedOrNull(onErrorFunction))
|
|
204
|
-
{
|
|
205
|
-
errorType = errorTypeOrOnErrorFunction as Type<unknown>;
|
|
206
|
-
|
|
207
|
-
PreCondition.assertNotUndefinedAndNotNull(errorType, "errorType");
|
|
208
|
-
}
|
|
209
|
-
else
|
|
210
|
-
{
|
|
211
|
-
onErrorFunction = errorTypeOrOnErrorFunction as ((reason: any) => void | PromiseLike<void>);
|
|
212
|
-
}
|
|
213
|
-
PreCondition.assertNotUndefinedAndNotNull(onErrorFunction, "onErrorFunction");
|
|
214
|
-
|
|
215
|
-
let result: SyncResult<T> | PromiseAsyncResult<T> = this;
|
|
216
|
-
if (this.error && (!errorType || instanceOfType(this.error, errorType)))
|
|
217
|
-
{
|
|
218
|
-
try
|
|
219
|
-
{
|
|
220
|
-
const onErrorResult: void | PromiseLike<void> = onErrorFunction(this.error);
|
|
221
|
-
if (isPromise(onErrorResult))
|
|
222
|
-
{
|
|
223
|
-
result = PromiseAsyncResult.create(onErrorResult).then(() => this);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
catch (error)
|
|
227
|
-
{
|
|
228
|
-
result = SyncResult.error(error);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
return result;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
public convertError(convertErrorFunction: (reason: unknown) => unknown): SyncResult<T>;
|
|
235
|
-
public convertError(onErrorFunction: (reason: unknown) => PromiseLike<unknown>): PromiseAsyncResult<T>;
|
|
236
|
-
public convertError<TError>(errorType: Type<TError>, convertErrorFunction: (reason: TError) => unknown): SyncResult<T>;
|
|
237
|
-
public convertError<TError>(errorType: Type<TError>, convertErrorFunction: (reason: TError) => PromiseLike<unknown>): PromiseAsyncResult<T>;
|
|
238
|
-
convertError(errorTypeOrConvertErrorFunction: Type<unknown> | ((reason: unknown) => (unknown | PromiseLike<unknown>)), convertErrorFunction?: (reason: unknown) => (unknown | PromiseLike<unknown>)): SyncResult<T> | PromiseAsyncResult<T>
|
|
239
|
-
{
|
|
240
|
-
let errorType: Type<TypeError> | undefined;
|
|
241
|
-
if (!isUndefinedOrNull(convertErrorFunction))
|
|
242
|
-
{
|
|
243
|
-
errorType = errorTypeOrConvertErrorFunction as Type<unknown>;
|
|
244
|
-
|
|
245
|
-
PreCondition.assertNotUndefinedAndNotNull(errorType, "errorType");
|
|
246
|
-
}
|
|
247
|
-
else
|
|
248
|
-
{
|
|
249
|
-
convertErrorFunction = errorTypeOrConvertErrorFunction as ((reason: any) => void | PromiseLike<void>);
|
|
250
|
-
}
|
|
251
|
-
PreCondition.assertNotUndefinedAndNotNull(convertErrorFunction, "convertErrorFunction");
|
|
252
|
-
|
|
253
|
-
let result: SyncResult<T> | PromiseAsyncResult<T> = this;
|
|
254
|
-
if (this.error && (!errorType || instanceOfType(this.error, errorType)))
|
|
255
|
-
{
|
|
256
|
-
try
|
|
257
|
-
{
|
|
258
|
-
const convertErrorResult: unknown | PromiseLike<unknown> = convertErrorFunction(this.error);
|
|
259
|
-
if (isPromise(convertErrorResult))
|
|
260
|
-
{
|
|
261
|
-
result = PromiseAsyncResult.error(convertErrorResult);
|
|
262
|
-
}
|
|
263
|
-
else
|
|
264
|
-
{
|
|
265
|
-
result = SyncResult.error(convertErrorResult);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
catch (error)
|
|
269
|
-
{
|
|
270
|
-
result = SyncResult.error(error);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
return result;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
public finally(onfinally?: (() => void) | null): SyncResult<T>
|
|
277
|
-
public finally(onfinally?: (() => Promise<void>) | null): PromiseAsyncResult<T>
|
|
278
|
-
finally(onfinally?: (() => (void | Promise<void>)) | null | undefined): PromiseAsyncResult<T> | SyncResult<T>
|
|
279
|
-
{
|
|
280
|
-
let result: PromiseAsyncResult<T> | SyncResult<T> = this;
|
|
281
|
-
if (onfinally)
|
|
282
|
-
{
|
|
283
|
-
try
|
|
284
|
-
{
|
|
285
|
-
const onfinallyResult: void | Promise<void> = onfinally();
|
|
286
|
-
if (isPromiseLike(onfinallyResult))
|
|
287
|
-
{
|
|
288
|
-
result = PromiseAsyncResult.create(onfinallyResult).then(() => this);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
catch (error)
|
|
292
|
-
{
|
|
293
|
-
result = SyncResult.error(error);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
return result;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
readonly [Symbol.toStringTag]: string = "SyncResult2";
|
|
1
|
+
import { PreCondition } from "./preCondition";
|
|
2
|
+
import { PromiseAsyncResult } from "./promiseAsyncResult";
|
|
3
|
+
import { instanceOfType, isPromise, isPromiseLike, isUndefinedOrNull, Type } from "./types";
|
|
4
|
+
import { AsyncResult } from "./asyncResult";
|
|
5
|
+
|
|
6
|
+
export class SyncResult<T> implements AsyncResult<T>
|
|
7
|
+
{
|
|
8
|
+
private value: T | undefined;
|
|
9
|
+
private error: unknown | undefined;
|
|
10
|
+
|
|
11
|
+
private constructor(value: T | undefined, error: unknown | undefined)
|
|
12
|
+
{
|
|
13
|
+
PreCondition.assertTrue(value === undefined || error === undefined, "value === undefined || error === undefined", "Either value or error must be undefined.");
|
|
14
|
+
|
|
15
|
+
this.value = value;
|
|
16
|
+
this.error = error;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public static create<T>(action: () => T): SyncResult<T>
|
|
20
|
+
{
|
|
21
|
+
PreCondition.assertNotUndefinedAndNotNull(action, "action");
|
|
22
|
+
|
|
23
|
+
let value: T | undefined;
|
|
24
|
+
let error: unknown | undefined;
|
|
25
|
+
try
|
|
26
|
+
{
|
|
27
|
+
value = action();
|
|
28
|
+
}
|
|
29
|
+
catch (e)
|
|
30
|
+
{
|
|
31
|
+
error = e;
|
|
32
|
+
}
|
|
33
|
+
return new SyncResult(value, error);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public static value<T>(value: T): SyncResult<T>
|
|
37
|
+
{
|
|
38
|
+
return new SyncResult(value, undefined);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public static error<T>(error: unknown): SyncResult<T>
|
|
42
|
+
{
|
|
43
|
+
return new SyncResult<T>(undefined, error);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public await(): T
|
|
47
|
+
{
|
|
48
|
+
if (this.error)
|
|
49
|
+
{
|
|
50
|
+
throw this.error;
|
|
51
|
+
}
|
|
52
|
+
return this.value!;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public then<TResult1 = T, TResult2 = never>(onfullfilled?: ((value: T) => TResult1) | null, onrejected?: ((reason: unknown) => TResult2) | null): SyncResult<TResult1 | TResult2>;
|
|
56
|
+
public then<TResult1 = T, TResult2 = never>(onfullfilled?: ((value: T) => (TResult1 | PromiseLike<TResult1>)) | null, onrejected?: ((reason: unknown) => (TResult2 | PromiseLike<TResult2>)) | null): SyncResult<TResult1 | TResult2>;
|
|
57
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => (TResult1 | PromiseLike<TResult1>)) | null, onrejected?: ((reason: unknown) => (TResult2 | PromiseLike<TResult2>)) | null): SyncResult<TResult1 | TResult2> | PromiseAsyncResult<TResult1 | TResult2>
|
|
58
|
+
{
|
|
59
|
+
let result: SyncResult<TResult1 | TResult2> | PromiseAsyncResult<TResult1 | TResult2>;
|
|
60
|
+
if (this.error)
|
|
61
|
+
{
|
|
62
|
+
if (onrejected)
|
|
63
|
+
{
|
|
64
|
+
try
|
|
65
|
+
{
|
|
66
|
+
const onRejectedResult: TResult2 | PromiseLike<TResult2> = onrejected(this.error);
|
|
67
|
+
if (onRejectedResult instanceof SyncResult || onRejectedResult instanceof PromiseAsyncResult)
|
|
68
|
+
{
|
|
69
|
+
result = onRejectedResult;
|
|
70
|
+
}
|
|
71
|
+
else if (isPromise<TResult2>(onRejectedResult))
|
|
72
|
+
{
|
|
73
|
+
result = PromiseAsyncResult.create(onRejectedResult);
|
|
74
|
+
}
|
|
75
|
+
else
|
|
76
|
+
{
|
|
77
|
+
result = SyncResult.value(onRejectedResult as TResult2);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (error)
|
|
81
|
+
{
|
|
82
|
+
result = SyncResult.error(error);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else
|
|
86
|
+
{
|
|
87
|
+
result = SyncResult.error<TResult2>(this.error);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else
|
|
91
|
+
{
|
|
92
|
+
if (onfulfilled)
|
|
93
|
+
{
|
|
94
|
+
try
|
|
95
|
+
{
|
|
96
|
+
const onFullfilledResult: TResult1 | PromiseLike<TResult1> = onfulfilled(this.value!);
|
|
97
|
+
if (onFullfilledResult instanceof SyncResult || onFullfilledResult instanceof PromiseAsyncResult)
|
|
98
|
+
{
|
|
99
|
+
result = onFullfilledResult;
|
|
100
|
+
}
|
|
101
|
+
else if (isPromise<TResult1>(onFullfilledResult))
|
|
102
|
+
{
|
|
103
|
+
result = PromiseAsyncResult.create(onFullfilledResult);
|
|
104
|
+
}
|
|
105
|
+
else
|
|
106
|
+
{
|
|
107
|
+
result = SyncResult.value(onFullfilledResult as TResult1);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch (error)
|
|
111
|
+
{
|
|
112
|
+
result = SyncResult.error(error);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else
|
|
116
|
+
{
|
|
117
|
+
result = SyncResult.value<TResult1>(this.value as TResult1);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public onValue(onValueFunction: (value: T) => void): SyncResult<T>;
|
|
124
|
+
public onValue(onValueFunction: (value: T) => Promise<void>): PromiseAsyncResult<T>;
|
|
125
|
+
onValue(onValueFunction: (value: T) => (void | Promise<void>)): SyncResult<T> | PromiseAsyncResult<T>
|
|
126
|
+
{
|
|
127
|
+
return this.then<T>((value: T) =>
|
|
128
|
+
{
|
|
129
|
+
let result: SyncResult<T> | PromiseAsyncResult<T>;
|
|
130
|
+
try
|
|
131
|
+
{
|
|
132
|
+
const onValueFunctionResult: (void | Promise<void>) = onValueFunction(value);
|
|
133
|
+
if (isPromise(onValueFunctionResult))
|
|
134
|
+
{
|
|
135
|
+
result = PromiseAsyncResult.create(onValueFunctionResult.then(() => value));
|
|
136
|
+
}
|
|
137
|
+
else
|
|
138
|
+
{
|
|
139
|
+
result = this;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (error)
|
|
143
|
+
{
|
|
144
|
+
result = SyncResult.error(error);
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public catch<TResult = never>(onrejected: (reason: unknown) => TResult): SyncResult<T | TResult>;
|
|
151
|
+
public catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): SyncResult<T | TResult> | PromiseAsyncResult<T | TResult>
|
|
152
|
+
public catch<TError, TResult = never>(errorType: Type<TError>, onrejected: (reason: TError) => TResult): SyncResult<T | TResult>;
|
|
153
|
+
public catch<TError, TResult = never>(errorType: Type<TError>, onrejected: (reason: TError) => PromiseLike<TResult>): PromiseAsyncResult<T | TResult>;
|
|
154
|
+
catch<TResult = never>(errorTypeOrOnRejected?: Type<unknown> | ((reason: unknown) => TResult | PromiseLike<TResult>) | null, onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): SyncResult<T | TResult> | PromiseAsyncResult<T | TResult>
|
|
155
|
+
{
|
|
156
|
+
let errorType: Type<TypeError> | undefined;
|
|
157
|
+
if (!isUndefinedOrNull(onrejected))
|
|
158
|
+
{
|
|
159
|
+
errorType = errorTypeOrOnRejected as Type<unknown>;
|
|
160
|
+
|
|
161
|
+
PreCondition.assertNotUndefinedAndNotNull(errorType, "errorType");
|
|
162
|
+
}
|
|
163
|
+
else
|
|
164
|
+
{
|
|
165
|
+
onrejected = errorTypeOrOnRejected as ((reason: any) => TResult | PromiseLike<TResult>) | null;
|
|
166
|
+
}
|
|
167
|
+
PreCondition.assertNotUndefinedAndNotNull(onrejected, "onrejected");
|
|
168
|
+
|
|
169
|
+
let result: SyncResult<T | TResult> | PromiseAsyncResult<T | TResult> = this;
|
|
170
|
+
if (this.error && (!errorType || instanceOfType(this.error, errorType)))
|
|
171
|
+
{
|
|
172
|
+
try
|
|
173
|
+
{
|
|
174
|
+
const onRejectedResult: TResult | PromiseLike<TResult> = onrejected(this.error);
|
|
175
|
+
if (onRejectedResult instanceof SyncResult || onRejectedResult instanceof PromiseAsyncResult)
|
|
176
|
+
{
|
|
177
|
+
result = onRejectedResult;
|
|
178
|
+
}
|
|
179
|
+
else if (isPromise<TResult>(onRejectedResult))
|
|
180
|
+
{
|
|
181
|
+
result = PromiseAsyncResult.create(onRejectedResult);
|
|
182
|
+
}
|
|
183
|
+
else
|
|
184
|
+
{
|
|
185
|
+
result = SyncResult.value(onRejectedResult as TResult);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch (error)
|
|
189
|
+
{
|
|
190
|
+
result = SyncResult.error(error);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public onError(onErrorFunction: (reason: unknown) => void): SyncResult<T>;
|
|
197
|
+
public onError(onErrorFunction: (reason: unknown) => PromiseLike<void>): PromiseAsyncResult<T>;
|
|
198
|
+
public onError<TError>(errorType: Type<TError>, onErrorFunction: (reason: TError) => void): SyncResult<T>;
|
|
199
|
+
public onError<TError>(errorType: Type<TError>, onErrorFunction: (reason: TError) => PromiseLike<void>): PromiseAsyncResult<T>;
|
|
200
|
+
onError(errorTypeOrOnErrorFunction: Type<unknown> | ((reason: unknown) => (void | PromiseLike<void>)), onErrorFunction?: (reason: unknown) => (void | PromiseLike<void>)): SyncResult<T> | PromiseAsyncResult<T>
|
|
201
|
+
{
|
|
202
|
+
let errorType: Type<TypeError> | undefined;
|
|
203
|
+
if (!isUndefinedOrNull(onErrorFunction))
|
|
204
|
+
{
|
|
205
|
+
errorType = errorTypeOrOnErrorFunction as Type<unknown>;
|
|
206
|
+
|
|
207
|
+
PreCondition.assertNotUndefinedAndNotNull(errorType, "errorType");
|
|
208
|
+
}
|
|
209
|
+
else
|
|
210
|
+
{
|
|
211
|
+
onErrorFunction = errorTypeOrOnErrorFunction as ((reason: any) => void | PromiseLike<void>);
|
|
212
|
+
}
|
|
213
|
+
PreCondition.assertNotUndefinedAndNotNull(onErrorFunction, "onErrorFunction");
|
|
214
|
+
|
|
215
|
+
let result: SyncResult<T> | PromiseAsyncResult<T> = this;
|
|
216
|
+
if (this.error && (!errorType || instanceOfType(this.error, errorType)))
|
|
217
|
+
{
|
|
218
|
+
try
|
|
219
|
+
{
|
|
220
|
+
const onErrorResult: void | PromiseLike<void> = onErrorFunction(this.error);
|
|
221
|
+
if (isPromise(onErrorResult))
|
|
222
|
+
{
|
|
223
|
+
result = PromiseAsyncResult.create(onErrorResult).then(() => this);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch (error)
|
|
227
|
+
{
|
|
228
|
+
result = SyncResult.error(error);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
public convertError(convertErrorFunction: (reason: unknown) => unknown): SyncResult<T>;
|
|
235
|
+
public convertError(onErrorFunction: (reason: unknown) => PromiseLike<unknown>): PromiseAsyncResult<T>;
|
|
236
|
+
public convertError<TError>(errorType: Type<TError>, convertErrorFunction: (reason: TError) => unknown): SyncResult<T>;
|
|
237
|
+
public convertError<TError>(errorType: Type<TError>, convertErrorFunction: (reason: TError) => PromiseLike<unknown>): PromiseAsyncResult<T>;
|
|
238
|
+
convertError(errorTypeOrConvertErrorFunction: Type<unknown> | ((reason: unknown) => (unknown | PromiseLike<unknown>)), convertErrorFunction?: (reason: unknown) => (unknown | PromiseLike<unknown>)): SyncResult<T> | PromiseAsyncResult<T>
|
|
239
|
+
{
|
|
240
|
+
let errorType: Type<TypeError> | undefined;
|
|
241
|
+
if (!isUndefinedOrNull(convertErrorFunction))
|
|
242
|
+
{
|
|
243
|
+
errorType = errorTypeOrConvertErrorFunction as Type<unknown>;
|
|
244
|
+
|
|
245
|
+
PreCondition.assertNotUndefinedAndNotNull(errorType, "errorType");
|
|
246
|
+
}
|
|
247
|
+
else
|
|
248
|
+
{
|
|
249
|
+
convertErrorFunction = errorTypeOrConvertErrorFunction as ((reason: any) => void | PromiseLike<void>);
|
|
250
|
+
}
|
|
251
|
+
PreCondition.assertNotUndefinedAndNotNull(convertErrorFunction, "convertErrorFunction");
|
|
252
|
+
|
|
253
|
+
let result: SyncResult<T> | PromiseAsyncResult<T> = this;
|
|
254
|
+
if (this.error && (!errorType || instanceOfType(this.error, errorType)))
|
|
255
|
+
{
|
|
256
|
+
try
|
|
257
|
+
{
|
|
258
|
+
const convertErrorResult: unknown | PromiseLike<unknown> = convertErrorFunction(this.error);
|
|
259
|
+
if (isPromise(convertErrorResult))
|
|
260
|
+
{
|
|
261
|
+
result = PromiseAsyncResult.error(convertErrorResult);
|
|
262
|
+
}
|
|
263
|
+
else
|
|
264
|
+
{
|
|
265
|
+
result = SyncResult.error(convertErrorResult);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
catch (error)
|
|
269
|
+
{
|
|
270
|
+
result = SyncResult.error(error);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return result;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
public finally(onfinally?: (() => void) | null): SyncResult<T>
|
|
277
|
+
public finally(onfinally?: (() => Promise<void>) | null): PromiseAsyncResult<T>
|
|
278
|
+
finally(onfinally?: (() => (void | Promise<void>)) | null | undefined): PromiseAsyncResult<T> | SyncResult<T>
|
|
279
|
+
{
|
|
280
|
+
let result: PromiseAsyncResult<T> | SyncResult<T> = this;
|
|
281
|
+
if (onfinally)
|
|
282
|
+
{
|
|
283
|
+
try
|
|
284
|
+
{
|
|
285
|
+
const onfinallyResult: void | Promise<void> = onfinally();
|
|
286
|
+
if (isPromiseLike(onfinallyResult))
|
|
287
|
+
{
|
|
288
|
+
result = PromiseAsyncResult.create(onfinallyResult).then(() => this);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
catch (error)
|
|
292
|
+
{
|
|
293
|
+
result = SyncResult.error(error);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return result;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
readonly [Symbol.toStringTag]: string = "SyncResult2";
|
|
300
300
|
}
|