resultar 3.2.1 → 3.3.1
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/README.md +83 -29
- package/dist/index.js +135 -138
- package/dist/index.js.map +1 -1
- package/package.json +6 -4
package/dist/index.js
CHANGED
|
@@ -53,32 +53,28 @@ const createResultarError = (message, result, config = defaultErrorConfig) => {
|
|
|
53
53
|
};
|
|
54
54
|
//#endregion
|
|
55
55
|
//#region src/tagged-match.ts
|
|
56
|
+
const noTaggedHandlerMatch = Symbol("noTaggedHandlerMatch");
|
|
57
|
+
const isTaggedHandlerMatch = (call) => call !== noTaggedHandlerMatch;
|
|
56
58
|
const isTaggedValue = (value) => typeof value === "object" && value !== null && "_tag" in value;
|
|
57
59
|
const hasTag = (value, tag) => isTaggedValue(value) && value._tag === tag;
|
|
58
60
|
const callTaggedHandler = (value, handlers) => {
|
|
59
|
-
if (!isTaggedValue(value)) return
|
|
61
|
+
if (!isTaggedValue(value)) return noTaggedHandlerMatch;
|
|
60
62
|
const handler = handlers[value._tag];
|
|
61
|
-
if (handler === void 0) return
|
|
62
|
-
return {
|
|
63
|
-
matched: true,
|
|
64
|
-
value: handler(value)
|
|
65
|
-
};
|
|
63
|
+
if (handler === void 0) return noTaggedHandlerMatch;
|
|
64
|
+
return { value: handler(value) };
|
|
66
65
|
};
|
|
67
66
|
const callErrorHandler = (error, handlers) => {
|
|
68
|
-
if (handlers.Error === void 0) return
|
|
69
|
-
return {
|
|
70
|
-
matched: true,
|
|
71
|
-
value: handlers.Error(error)
|
|
72
|
-
};
|
|
67
|
+
if (handlers.Error === void 0) return noTaggedHandlerMatch;
|
|
68
|
+
return { value: handlers.Error(error) };
|
|
73
69
|
};
|
|
74
70
|
const callTaggedOrErrorHandler = (error, handlers) => {
|
|
75
71
|
const tagged = callTaggedHandler(error, handlers);
|
|
76
|
-
if (tagged
|
|
72
|
+
if (isTaggedHandlerMatch(tagged)) return tagged;
|
|
77
73
|
return callErrorHandler(error, handlers);
|
|
78
74
|
};
|
|
79
75
|
const matchTaggedOr = (error, handlers, fallback) => {
|
|
80
76
|
const handled = callTaggedOrErrorHandler(error, handlers);
|
|
81
|
-
return handled
|
|
77
|
+
return isTaggedHandlerMatch(handled) ? handled.value : fallback(error);
|
|
82
78
|
};
|
|
83
79
|
//#endregion
|
|
84
80
|
//#region src/result.ts
|
|
@@ -113,38 +109,30 @@ const combineResultRecordWithAllErrors = (resultRecord) => {
|
|
|
113
109
|
if (errors.length > 0) return err(errors);
|
|
114
110
|
return ok(values);
|
|
115
111
|
};
|
|
116
|
-
const isIterable$1 = (value) => typeof value === "object"
|
|
117
|
-
const validateAllResultItems = (items, f) => {
|
|
118
|
-
return combineResultListWithAllErrors(Array.from(items, f));
|
|
119
|
-
};
|
|
112
|
+
const isIterable$1 = (value) => value !== null && (typeof value === "object" || typeof value === "function") && typeof value[Symbol.iterator] === "function";
|
|
120
113
|
const createEmptyResultsCollectionError = () => {
|
|
121
114
|
const error = /* @__PURE__ */ new Error("Received an empty collection of results");
|
|
122
115
|
error.name = "IllegalArgumentException";
|
|
123
116
|
return error;
|
|
124
117
|
};
|
|
125
|
-
const
|
|
126
|
-
let latestError = void 0;
|
|
127
|
-
let hasCandidates = false;
|
|
128
|
-
for (const candidate of candidates) {
|
|
129
|
-
hasCandidates = true;
|
|
130
|
-
const result = candidate();
|
|
131
|
-
if (result.isOk()) return result;
|
|
132
|
-
latestError = result;
|
|
133
|
-
}
|
|
134
|
-
if (!hasCandidates || latestError === void 0) throw createEmptyResultsCollectionError();
|
|
135
|
-
return latestError;
|
|
136
|
-
};
|
|
137
|
-
const getReason$1 = (value) => typeof value === "object" && value !== null && "reason" in value ? value.reason : void 0;
|
|
118
|
+
const getReason$1 = (value) => value.reason;
|
|
138
119
|
const loopResult = (initial, options) => {
|
|
139
120
|
let state = initial;
|
|
121
|
+
if (options.discard === true) {
|
|
122
|
+
while (options.while(state)) {
|
|
123
|
+
const result = options.body(state);
|
|
124
|
+
if (result.isErr()) return err(result.error);
|
|
125
|
+
state = options.step(state);
|
|
126
|
+
}
|
|
127
|
+
return ok(void 0);
|
|
128
|
+
}
|
|
140
129
|
const values = [];
|
|
141
130
|
while (options.while(state)) {
|
|
142
131
|
const result = options.body(state);
|
|
143
132
|
if (result.isErr()) return err(result.error);
|
|
144
|
-
|
|
133
|
+
values.push(result.value);
|
|
145
134
|
state = options.step(state);
|
|
146
135
|
}
|
|
147
|
-
if (options.discard === true) return ok(void 0);
|
|
148
136
|
return ok(values);
|
|
149
137
|
};
|
|
150
138
|
const iterateResult = (initial, options) => {
|
|
@@ -157,34 +145,25 @@ const iterateResult = (initial, options) => {
|
|
|
157
145
|
return ok(state);
|
|
158
146
|
};
|
|
159
147
|
const forEachResult = (items, f, options) => {
|
|
148
|
+
if (options?.discard === true) {
|
|
149
|
+
let index = 0;
|
|
150
|
+
for (const item of items) {
|
|
151
|
+
const result = f(item, index);
|
|
152
|
+
if (result.isErr()) return err(result.error);
|
|
153
|
+
index += 1;
|
|
154
|
+
}
|
|
155
|
+
return ok(void 0);
|
|
156
|
+
}
|
|
160
157
|
const values = [];
|
|
161
158
|
let index = 0;
|
|
162
159
|
for (const item of items) {
|
|
163
160
|
const result = f(item, index);
|
|
164
161
|
if (result.isErr()) return err(result.error);
|
|
165
|
-
|
|
162
|
+
values.push(result.value);
|
|
166
163
|
index += 1;
|
|
167
164
|
}
|
|
168
|
-
if (options?.discard === true) return ok(void 0);
|
|
169
165
|
return ok(values);
|
|
170
166
|
};
|
|
171
|
-
const resolveResultBooleanCondition = (condition) => typeof condition === "function" ? condition() : condition;
|
|
172
|
-
const ifResult = (condition, options) => resolveResultBooleanCondition(condition) ? options.onTrue() : options.onFalse();
|
|
173
|
-
const ifResultWithCondition = (condition, options) => {
|
|
174
|
-
if (condition.isErr()) return err(condition.error);
|
|
175
|
-
return ifResult(condition.value, options);
|
|
176
|
-
};
|
|
177
|
-
const whenResult = (condition, body) => {
|
|
178
|
-
if (resolveResultBooleanCondition(condition)) return body();
|
|
179
|
-
return ok(void 0);
|
|
180
|
-
};
|
|
181
|
-
const whenResultWithConditionByPredicate = (condition, body, predicate) => {
|
|
182
|
-
if (condition.isErr()) return err(condition.error);
|
|
183
|
-
return whenResult(predicate(condition.value), body);
|
|
184
|
-
};
|
|
185
|
-
const whenResultWithCondition = (condition, body) => whenResultWithConditionByPredicate(condition, body, (value) => value);
|
|
186
|
-
const unlessResult = (condition, body) => whenResult(() => !resolveResultBooleanCondition(condition), body);
|
|
187
|
-
const unlessResultWithCondition = (condition, body) => whenResultWithConditionByPredicate(condition, body, (value) => !value);
|
|
188
167
|
const callSyncSideEffect = (effect) => {
|
|
189
168
|
try {
|
|
190
169
|
effect();
|
|
@@ -264,29 +243,43 @@ var ResultNamespace = class {
|
|
|
264
243
|
}
|
|
265
244
|
static validateAll(resultListOrItems, f) {
|
|
266
245
|
if (f === void 0) return combineResultListWithAllErrors(resultListOrItems);
|
|
267
|
-
return
|
|
246
|
+
return combineResultListWithAllErrors(Array.from(resultListOrItems, f));
|
|
268
247
|
}
|
|
269
248
|
static zip(left, right) {
|
|
270
249
|
return combineResultList([left, right]);
|
|
271
250
|
}
|
|
272
251
|
static firstSuccessOf(candidates) {
|
|
273
|
-
|
|
252
|
+
let latestError = void 0;
|
|
253
|
+
for (const candidate of candidates) {
|
|
254
|
+
const result = candidate();
|
|
255
|
+
if (result.isOk()) return result;
|
|
256
|
+
latestError = result;
|
|
257
|
+
}
|
|
258
|
+
if (latestError === void 0) throw createEmptyResultsCollectionError();
|
|
259
|
+
return latestError;
|
|
274
260
|
}
|
|
275
261
|
static if(condition, options) {
|
|
276
|
-
if (typeof condition === "boolean" || typeof condition === "function") return
|
|
277
|
-
return
|
|
262
|
+
if (typeof condition === "boolean" || typeof condition === "function") return (typeof condition === "function" ? condition() : condition) ? options.onTrue() : options.onFalse();
|
|
263
|
+
if (condition.isErr()) return err(condition.error);
|
|
264
|
+
return condition.value ? options.onTrue() : options.onFalse();
|
|
278
265
|
}
|
|
279
266
|
static when(condition, body) {
|
|
280
|
-
|
|
267
|
+
if (typeof condition === "function" ? condition() : condition) return body();
|
|
268
|
+
return ok(void 0);
|
|
281
269
|
}
|
|
282
270
|
static whenResult(condition, body) {
|
|
283
|
-
return
|
|
271
|
+
if (condition.isErr()) return err(condition.error);
|
|
272
|
+
if (condition.value) return body();
|
|
273
|
+
return ok(void 0);
|
|
284
274
|
}
|
|
285
275
|
static unless(condition, body) {
|
|
286
|
-
|
|
276
|
+
if (typeof condition === "function" ? !condition() : !condition) return body();
|
|
277
|
+
return ok(void 0);
|
|
287
278
|
}
|
|
288
279
|
static unlessResult(condition, body) {
|
|
289
|
-
return
|
|
280
|
+
if (condition.isErr()) return err(condition.error);
|
|
281
|
+
if (!condition.value) return body();
|
|
282
|
+
return ok(void 0);
|
|
290
283
|
}
|
|
291
284
|
static loop(initial, options) {
|
|
292
285
|
return loopResult(initial, options);
|
|
@@ -454,7 +447,7 @@ var Err = class extends ResultVariant {
|
|
|
454
447
|
catchTags(handlers) {
|
|
455
448
|
const error = this.error;
|
|
456
449
|
const handled = callTaggedHandler(error, handlers);
|
|
457
|
-
if (handled
|
|
450
|
+
if (isTaggedHandlerMatch(handled)) return handled.value;
|
|
458
451
|
return err(error);
|
|
459
452
|
}
|
|
460
453
|
catchReason(errorTag, reasonTag, f) {
|
|
@@ -639,7 +632,7 @@ const combineResultAsyncListWithAllErrors = (asyncResultList) => ResultAsync.fro
|
|
|
639
632
|
const combineResultAsyncRecordWithAllErrors = (asyncResultRecord) => {
|
|
640
633
|
return new ResultAsync(collectResultAsyncRecord(asyncResultRecord).then((resultRecord) => Result.combineWithAllErrors(resultRecord)));
|
|
641
634
|
};
|
|
642
|
-
const isIterable = (value) => typeof value === "object"
|
|
635
|
+
const isIterable = (value) => value !== null && (typeof value === "object" || typeof value === "function") && typeof value[Symbol.iterator] === "function";
|
|
643
636
|
const createIllegalArgumentException = (message) => {
|
|
644
637
|
const error = new Error(message);
|
|
645
638
|
error.name = "IllegalArgumentException";
|
|
@@ -730,11 +723,6 @@ const runConcurrentResultAsyncItems = (options) => Promise.try(async () => {
|
|
|
730
723
|
schedule();
|
|
731
724
|
});
|
|
732
725
|
});
|
|
733
|
-
const createResultAsyncRetryContext = (attempt, times) => ({
|
|
734
|
-
attempt,
|
|
735
|
-
nextAttempt: attempt + 1,
|
|
736
|
-
retriesRemaining: Math.max(0, times - attempt)
|
|
737
|
-
});
|
|
738
726
|
const createResultAsyncRetryAbortError = (signal) => signal.reason === void 0 ? new AbortError("ResultAsync retry aborted") : new AbortError("ResultAsync retry aborted", { cause: signal.reason });
|
|
739
727
|
const createResultAsyncResourceAbortError = (signal) => signal.reason === void 0 ? new AbortError("ResultAsync resource scope aborted") : new AbortError("ResultAsync resource scope aborted", { cause: signal.reason });
|
|
740
728
|
const validateResultAsyncRetryTimes = (times) => {
|
|
@@ -755,23 +743,6 @@ const validateResultAsyncRetryStaticJittered = (jittered) => {
|
|
|
755
743
|
const validateResultAsyncRetryStaticDelay = (delay) => {
|
|
756
744
|
if (typeof delay === "number") validateResultAsyncRetryDelayValue(delay);
|
|
757
745
|
};
|
|
758
|
-
const getResultAsyncRetryJitterRandom = () => {
|
|
759
|
-
const value = Math.random();
|
|
760
|
-
validateResultAsyncRetryJitterRandomValue(value);
|
|
761
|
-
return value;
|
|
762
|
-
};
|
|
763
|
-
const applyResultAsyncRetryJitter = (delayMs, jittered) => {
|
|
764
|
-
if (jittered === void 0 || jittered === 0 || delayMs === 0) return delayMs;
|
|
765
|
-
const random = getResultAsyncRetryJitterRandom();
|
|
766
|
-
const range = delayMs * jittered;
|
|
767
|
-
const min = Math.max(0, delayMs - range);
|
|
768
|
-
return min + (delayMs + range - min) * random;
|
|
769
|
-
};
|
|
770
|
-
const normalizeResultAsyncRetryDelay = (delay, jittered, context) => {
|
|
771
|
-
const delayMs = typeof delay === "function" ? delay(context) : delay ?? 0;
|
|
772
|
-
validateResultAsyncRetryDelayValue(delayMs);
|
|
773
|
-
return applyResultAsyncRetryJitter(delayMs, jittered);
|
|
774
|
-
};
|
|
775
746
|
const waitResultAsyncRetryDelay = async (delayMs, signal) => {
|
|
776
747
|
if (signal.aborted) return Result.err(createResultAsyncRetryAbortError(signal));
|
|
777
748
|
if (delayMs === 0) return Result.ok(void 0);
|
|
@@ -787,13 +758,6 @@ const waitResultAsyncRetryDelay = async (delayMs, signal) => {
|
|
|
787
758
|
signal.addEventListener("abort", abort, { once: true });
|
|
788
759
|
});
|
|
789
760
|
};
|
|
790
|
-
const callResultAsyncRetryOnRetry = async (options, error, context) => {
|
|
791
|
-
try {
|
|
792
|
-
await options.onRetry?.(error, context);
|
|
793
|
-
} catch {}
|
|
794
|
-
};
|
|
795
|
-
const shouldRetryResultAsyncError = async (options, error, context) => options.while?.(error, context) ?? true;
|
|
796
|
-
const resolveResultAsyncRetryRecovery = async (recovered) => recovered instanceof ResultAsync ? recovered : recovered;
|
|
797
761
|
const runResultAsyncRetryAttempts = async (task, options, recover) => {
|
|
798
762
|
validateResultAsyncRetryTimes(options.times);
|
|
799
763
|
validateResultAsyncRetryStaticDelay(options.delayMs);
|
|
@@ -804,10 +768,31 @@ const runResultAsyncRetryAttempts = async (task, options, recover) => {
|
|
|
804
768
|
if (signal.aborted) return Result.err(createResultAsyncRetryAbortError(signal));
|
|
805
769
|
const result = await task(attempt, signal);
|
|
806
770
|
if (result.isOk()) return Result.ok(result.value);
|
|
807
|
-
const context =
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
771
|
+
const context = {
|
|
772
|
+
attempt,
|
|
773
|
+
nextAttempt: attempt + 1,
|
|
774
|
+
retriesRemaining: Math.max(0, options.times - attempt)
|
|
775
|
+
};
|
|
776
|
+
const shouldRetry = options.while;
|
|
777
|
+
if (!(shouldRetry === void 0 ? true : await shouldRetry(result.error, context)) || attempt >= options.times) {
|
|
778
|
+
const recovered = recover(result.error, context);
|
|
779
|
+
return recovered instanceof ResultAsync ? recovered : recovered;
|
|
780
|
+
}
|
|
781
|
+
const onRetry = options.onRetry;
|
|
782
|
+
if (onRetry !== void 0) try {
|
|
783
|
+
await onRetry(result.error, context);
|
|
784
|
+
} catch {}
|
|
785
|
+
let delayMs = typeof options.delayMs === "function" ? options.delayMs(context) : options.delayMs ?? 0;
|
|
786
|
+
validateResultAsyncRetryDelayValue(delayMs);
|
|
787
|
+
const jittered = options.jittered;
|
|
788
|
+
if (jittered !== void 0 && jittered !== 0 && delayMs !== 0) {
|
|
789
|
+
const random = Math.random();
|
|
790
|
+
validateResultAsyncRetryJitterRandomValue(random);
|
|
791
|
+
const range = delayMs * jittered;
|
|
792
|
+
const min = Math.max(0, delayMs - range);
|
|
793
|
+
delayMs = min + (delayMs + range - min) * random;
|
|
794
|
+
}
|
|
795
|
+
const delayResult = await waitResultAsyncRetryDelay(delayMs, signal);
|
|
811
796
|
if (delayResult.isErr()) return Result.err(delayResult.error);
|
|
812
797
|
attempt += 1;
|
|
813
798
|
}
|
|
@@ -865,18 +850,16 @@ const validateAllResultAsyncItems = (items, f, options) => {
|
|
|
865
850
|
const firstSuccessOfAsyncCandidates = (candidates) => {
|
|
866
851
|
return new ResultAsync(Promise.try(async () => {
|
|
867
852
|
let latestError = void 0;
|
|
868
|
-
let hasCandidates = false;
|
|
869
853
|
for (const candidate of candidates) {
|
|
870
|
-
hasCandidates = true;
|
|
871
854
|
const result = await candidate();
|
|
872
855
|
if (result.isOk()) return result;
|
|
873
856
|
latestError = result;
|
|
874
857
|
}
|
|
875
|
-
if (
|
|
858
|
+
if (latestError === void 0) throw createEmptyResultsCollectionError();
|
|
876
859
|
return latestError;
|
|
877
860
|
}));
|
|
878
861
|
};
|
|
879
|
-
const getReason = (value) =>
|
|
862
|
+
const getReason = (value) => value.reason;
|
|
880
863
|
const createRaceAbortReason = () => new AbortError("ResultAsync race loser interrupted");
|
|
881
864
|
const startRaceTask = (task, index) => {
|
|
882
865
|
const controller = new AbortController();
|
|
@@ -890,33 +873,26 @@ const startRaceTask = (task, index) => {
|
|
|
890
873
|
}))
|
|
891
874
|
};
|
|
892
875
|
};
|
|
893
|
-
const abortStartedRaceTask = (task, reason = createRaceAbortReason()) => {
|
|
894
|
-
if (!task.controller.signal.aborted) task.controller.abort(reason);
|
|
895
|
-
};
|
|
896
876
|
const createRaceHandle = (task) => ({
|
|
897
877
|
get signal() {
|
|
898
878
|
return task.controller.signal;
|
|
899
879
|
},
|
|
900
|
-
abort(reason) {
|
|
901
|
-
|
|
880
|
+
abort(reason = createRaceAbortReason()) {
|
|
881
|
+
task.controller.abort(reason);
|
|
902
882
|
},
|
|
903
883
|
wait() {
|
|
904
884
|
return task.result;
|
|
905
885
|
}
|
|
906
886
|
});
|
|
907
|
-
const abortRaceLosers = (startedTasks, winnerIndex) => {
|
|
908
|
-
const winner = startedTasks[winnerIndex];
|
|
909
|
-
for (const loser of startedTasks) if (loser !== winner) abortStartedRaceTask(loser);
|
|
910
|
-
};
|
|
911
|
-
const abortPendingRaceLosers = (startedTasks, winnerIndex, completedIndexes) => {
|
|
912
|
-
const winner = startedTasks[winnerIndex];
|
|
913
|
-
for (const [index, loser] of startedTasks.entries()) if (loser !== winner && !completedIndexes.has(index)) abortStartedRaceTask(loser);
|
|
914
|
-
};
|
|
915
887
|
const observeRaceFirstTask = (task, context) => {
|
|
916
888
|
task.settled.then(({ index, result }) => {
|
|
917
889
|
if (context.state.settled) return;
|
|
918
890
|
context.state.settled = true;
|
|
919
|
-
|
|
891
|
+
const startedTasks = context.startedTasks;
|
|
892
|
+
for (let loserIndex = 0; loserIndex < startedTasks.length; loserIndex += 1) {
|
|
893
|
+
const loser = startedTasks[loserIndex];
|
|
894
|
+
if (loser === void 0) {} else if (loserIndex !== index) loser.controller.abort(createRaceAbortReason());
|
|
895
|
+
}
|
|
920
896
|
context.resolve(result);
|
|
921
897
|
});
|
|
922
898
|
};
|
|
@@ -926,13 +902,17 @@ const observeRaceSuccessTask = (task, context) => {
|
|
|
926
902
|
context.state.completedIndexes.add(index);
|
|
927
903
|
if (result.isOk()) {
|
|
928
904
|
context.state.settled = true;
|
|
929
|
-
|
|
905
|
+
const startedTasks = context.startedTasks;
|
|
906
|
+
for (let loserIndex = 0; loserIndex < startedTasks.length; loserIndex += 1) {
|
|
907
|
+
const loser = startedTasks[loserIndex];
|
|
908
|
+
if (loser === void 0) {} else if (!context.state.completedIndexes.has(loserIndex)) loser.controller.abort(createRaceAbortReason());
|
|
909
|
+
}
|
|
930
910
|
context.resolve(result);
|
|
931
911
|
return;
|
|
932
912
|
}
|
|
933
913
|
context.state.completedCount += 1;
|
|
934
914
|
context.state.latestError = result;
|
|
935
|
-
if (context.state.completedCount === context.startedTasks.length
|
|
915
|
+
if (context.state.completedCount === context.startedTasks.length) {
|
|
936
916
|
context.state.settled = true;
|
|
937
917
|
context.resolve(context.state.latestError);
|
|
938
918
|
}
|
|
@@ -947,13 +927,6 @@ const observeRaceResultAsyncTasks = (tasks, createContext, observeTask) => {
|
|
|
947
927
|
for (const task of startedTasks) observeTask(task, context);
|
|
948
928
|
}));
|
|
949
929
|
};
|
|
950
|
-
const raceFirstResultAsyncTasks = (tasks) => observeRaceResultAsyncTasks(tasks, (resolve, startedTasks) => ({
|
|
951
|
-
resolve,
|
|
952
|
-
startedTasks,
|
|
953
|
-
state: { settled: false }
|
|
954
|
-
}), (task, context) => {
|
|
955
|
-
observeRaceFirstTask(task, context);
|
|
956
|
-
});
|
|
957
930
|
const raceResultAsyncTasks = (tasks) => observeRaceResultAsyncTasks(tasks, (resolve, startedTasks) => ({
|
|
958
931
|
resolve,
|
|
959
932
|
startedTasks,
|
|
@@ -963,20 +936,25 @@ const raceResultAsyncTasks = (tasks) => observeRaceResultAsyncTasks(tasks, (reso
|
|
|
963
936
|
latestError: void 0,
|
|
964
937
|
settled: false
|
|
965
938
|
}
|
|
966
|
-
}),
|
|
967
|
-
observeRaceSuccessTask(task, context);
|
|
968
|
-
});
|
|
939
|
+
}), observeRaceSuccessTask);
|
|
969
940
|
const loopResultAsync = (initial, options) => {
|
|
970
941
|
return new ResultAsync(Promise.try(async () => {
|
|
971
942
|
let state = initial;
|
|
943
|
+
if (options.discard === true) {
|
|
944
|
+
while (options.while(state)) {
|
|
945
|
+
const result = await options.body(state);
|
|
946
|
+
if (result.isErr()) return Result.err(result.error);
|
|
947
|
+
state = options.step(state);
|
|
948
|
+
}
|
|
949
|
+
return Result.ok(void 0);
|
|
950
|
+
}
|
|
972
951
|
const values = [];
|
|
973
952
|
while (options.while(state)) {
|
|
974
953
|
const result = await options.body(state);
|
|
975
954
|
if (result.isErr()) return Result.err(result.error);
|
|
976
|
-
|
|
955
|
+
values.push(result.value);
|
|
977
956
|
state = options.step(state);
|
|
978
957
|
}
|
|
979
|
-
if (options.discard === true) return Result.ok(void 0);
|
|
980
958
|
return Result.ok(values);
|
|
981
959
|
}));
|
|
982
960
|
};
|
|
@@ -992,13 +970,32 @@ const iterateResultAsync = (initial, options) => {
|
|
|
992
970
|
}));
|
|
993
971
|
};
|
|
994
972
|
const forEachResultAsync = (items, f, options) => {
|
|
973
|
+
if (options?.discard === true) return new ResultAsync(runConcurrentResultAsyncItems({
|
|
974
|
+
concurrency: options.concurrency,
|
|
975
|
+
finish: (state) => {
|
|
976
|
+
if (state.activeCount > 0) return;
|
|
977
|
+
const firstError = sortIndexedResultAsyncErrors(state.errors)[0];
|
|
978
|
+
if (firstError !== void 0) return Result.err(firstError.error);
|
|
979
|
+
return Result.ok(void 0);
|
|
980
|
+
},
|
|
981
|
+
items,
|
|
982
|
+
onResult: (result, currentIndex, state) => {
|
|
983
|
+
if (result.isErr()) {
|
|
984
|
+
state.errors.push({
|
|
985
|
+
error: result.error,
|
|
986
|
+
index: currentIndex
|
|
987
|
+
});
|
|
988
|
+
state.stopScheduling = true;
|
|
989
|
+
}
|
|
990
|
+
},
|
|
991
|
+
run: f
|
|
992
|
+
}));
|
|
995
993
|
return new ResultAsync(runConcurrentResultAsyncItems({
|
|
996
994
|
concurrency: options?.concurrency,
|
|
997
995
|
finish: (state) => {
|
|
998
|
-
if (state.activeCount > 0
|
|
996
|
+
if (state.activeCount > 0) return;
|
|
999
997
|
const firstError = sortIndexedResultAsyncErrors(state.errors)[0];
|
|
1000
998
|
if (firstError !== void 0) return Result.err(firstError.error);
|
|
1001
|
-
if (options?.discard === true) return Result.ok(void 0);
|
|
1002
999
|
return Result.ok(state.values);
|
|
1003
1000
|
},
|
|
1004
1001
|
items,
|
|
@@ -1011,7 +1008,7 @@ const forEachResultAsync = (items, f, options) => {
|
|
|
1011
1008
|
state.stopScheduling = true;
|
|
1012
1009
|
return;
|
|
1013
1010
|
}
|
|
1014
|
-
|
|
1011
|
+
state.values[currentIndex] = result.value;
|
|
1015
1012
|
},
|
|
1016
1013
|
run: f
|
|
1017
1014
|
}));
|
|
@@ -1177,7 +1174,11 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1177
1174
|
return raceResultAsyncTasks(tasks);
|
|
1178
1175
|
}
|
|
1179
1176
|
static raceFirst(left, right) {
|
|
1180
|
-
return
|
|
1177
|
+
return observeRaceResultAsyncTasks([left, right], (resolve, startedTasks) => ({
|
|
1178
|
+
resolve,
|
|
1179
|
+
startedTasks,
|
|
1180
|
+
state: { settled: false }
|
|
1181
|
+
}), observeRaceFirstTask);
|
|
1181
1182
|
}
|
|
1182
1183
|
static raceWith(left, right, handlers) {
|
|
1183
1184
|
return new ResultAsync(new Promise((resolve) => {
|
|
@@ -1212,7 +1213,7 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1212
1213
|
}, options.timeoutMs);
|
|
1213
1214
|
signal.addEventListener("abort", () => {
|
|
1214
1215
|
clearTimeout(timeout);
|
|
1215
|
-
}
|
|
1216
|
+
});
|
|
1216
1217
|
}));
|
|
1217
1218
|
};
|
|
1218
1219
|
return ResultAsync.raceWith(task, timeoutTask, {
|
|
@@ -1222,7 +1223,6 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1222
1223
|
},
|
|
1223
1224
|
onRightDone: (result, taskHandle) => {
|
|
1224
1225
|
if (result.isErr()) taskHandle.abort(result.error);
|
|
1225
|
-
else taskHandle.abort(createRaceAbortReason());
|
|
1226
1226
|
return result;
|
|
1227
1227
|
}
|
|
1228
1228
|
});
|
|
@@ -1342,7 +1342,7 @@ var ResultAsync = class ResultAsync extends Pipeable {
|
|
|
1342
1342
|
if (res.isOk()) return Result.ok(res.value);
|
|
1343
1343
|
const error = res.error;
|
|
1344
1344
|
const handled = callTaggedHandler(error, handlers);
|
|
1345
|
-
if (handled
|
|
1345
|
+
if (isTaggedHandlerMatch(handled)) {
|
|
1346
1346
|
const next = handled.value;
|
|
1347
1347
|
return next instanceof ResultAsync ? next.innerPromise : next;
|
|
1348
1348
|
}
|
|
@@ -1533,10 +1533,7 @@ const stringifySymbolTemplateValue = (value) => {
|
|
|
1533
1533
|
return symbol.description ?? symbol.toString();
|
|
1534
1534
|
};
|
|
1535
1535
|
const stringifyFunctionTemplateValue = (value) => value.name;
|
|
1536
|
-
const stringifyObjectTemplateValue = (value) =>
|
|
1537
|
-
if (value === null) return "null";
|
|
1538
|
-
return JSON.stringify(value) ?? Object.prototype.toString.call(value);
|
|
1539
|
-
};
|
|
1536
|
+
const stringifyObjectTemplateValue = (value) => JSON.stringify(value) ?? Object.prototype.toString.call(value);
|
|
1540
1537
|
const templateValueStringifiers = {
|
|
1541
1538
|
bigint: stringifyDirectTemplateValue,
|
|
1542
1539
|
boolean: stringifyDirectTemplateValue,
|
|
@@ -1546,7 +1543,7 @@ const templateValueStringifiers = {
|
|
|
1546
1543
|
string: stringifyDirectTemplateValue,
|
|
1547
1544
|
symbol: stringifySymbolTemplateValue
|
|
1548
1545
|
};
|
|
1549
|
-
const stringifyTemplateValue = (value) => isRedacted(value) ? stringifyRedacted(value) : templateValueStringifiers[typeof value]
|
|
1546
|
+
const stringifyTemplateValue = (value) => isRedacted(value) ? stringifyRedacted(value) : templateValueStringifiers[typeof value](value);
|
|
1550
1547
|
const compileMessageInterpolator = (template) => (values) => template.replaceAll(templateVariableMatcher, (variable, key) => {
|
|
1551
1548
|
const value = values?.[key];
|
|
1552
1549
|
return value === void 0 ? variable : stringifyTemplateValue(value);
|