@carbonteq/fp 0.10.0 → 0.11.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 +18 -3
- package/dist/errors.cjs +1 -3
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +2 -5
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.mts +2 -5
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +1 -2
- package/dist/errors.mjs.map +1 -1
- package/dist/flow-experimental.cjs +197 -151
- package/dist/flow-experimental.cjs.map +1 -1
- package/dist/flow-experimental.d.cts +11 -11
- package/dist/flow-experimental.d.cts.map +1 -1
- package/dist/flow-experimental.d.mts +11 -11
- package/dist/flow-experimental.d.mts.map +1 -1
- package/dist/flow-experimental.mjs +196 -150
- package/dist/flow-experimental.mjs.map +1 -1
- package/dist/flow.cjs +194 -150
- package/dist/flow.cjs.map +1 -1
- package/dist/flow.d.cts +8 -10
- package/dist/flow.d.cts.map +1 -1
- package/dist/flow.d.mts +8 -10
- package/dist/flow.d.mts.map +1 -1
- package/dist/flow.mjs +193 -149
- package/dist/flow.mjs.map +1 -1
- package/dist/index.cjs +10 -10
- package/dist/index.d.cts +7 -7
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +7 -7
- package/dist/match.cjs +100 -9
- package/dist/match.cjs.map +1 -1
- package/dist/match.d.cts +14 -21
- package/dist/match.d.cts.map +1 -1
- package/dist/match.d.mts +14 -21
- package/dist/match.d.mts.map +1 -1
- package/dist/match.mjs +100 -8
- package/dist/match.mjs.map +1 -1
- package/dist/option-experimental.cjs +101 -57
- package/dist/option-experimental.cjs.map +1 -1
- package/dist/option-experimental.d.cts +7 -10
- package/dist/option-experimental.d.cts.map +1 -1
- package/dist/option-experimental.d.mts +7 -10
- package/dist/option-experimental.d.mts.map +1 -1
- package/dist/option-experimental.mjs +100 -56
- package/dist/option-experimental.mjs.map +1 -1
- package/dist/option.cjs +170 -118
- package/dist/option.cjs.map +1 -1
- package/dist/option.d.cts +27 -19
- package/dist/option.d.cts.map +1 -1
- package/dist/option.d.mts +27 -19
- package/dist/option.d.mts.map +1 -1
- package/dist/option.mjs +169 -117
- package/dist/option.mjs.map +1 -1
- package/dist/result-experimental.cjs +197 -117
- package/dist/result-experimental.cjs.map +1 -1
- package/dist/result-experimental.d.cts +26 -26
- package/dist/result-experimental.d.cts.map +1 -1
- package/dist/result-experimental.d.mts +26 -26
- package/dist/result-experimental.d.mts.map +1 -1
- package/dist/result-experimental.mjs +195 -115
- package/dist/result-experimental.mjs.map +1 -1
- package/dist/result.cjs +245 -173
- package/dist/result.cjs.map +1 -1
- package/dist/result.d.cts +26 -22
- package/dist/result.d.cts.map +1 -1
- package/dist/result.d.mts +26 -22
- package/dist/result.d.mts.map +1 -1
- package/dist/result.mjs +243 -171
- package/dist/result.mjs.map +1 -1
- package/dist/tuple.cjs +3 -5
- package/dist/tuple.cjs.map +1 -1
- package/dist/tuple.d.cts +6 -8
- package/dist/tuple.d.cts.map +1 -1
- package/dist/tuple.d.mts +6 -8
- package/dist/tuple.d.mts.map +1 -1
- package/dist/tuple.mjs +3 -4
- package/dist/tuple.mjs.map +1 -1
- package/dist/unit.cjs +1 -3
- package/dist/unit.cjs.map +1 -1
- package/dist/unit.d.cts +2 -5
- package/dist/unit.d.cts.map +1 -1
- package/dist/unit.d.mts +2 -5
- package/dist/unit.d.mts.map +1 -1
- package/dist/unit.mjs +1 -2
- package/dist/unit.mjs.map +1 -1
- package/dist/utils.cjs +35 -3
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +7 -6
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +7 -6
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +34 -3
- package/dist/utils.mjs.map +1 -1
- package/examples/README.md +14 -0
- package/examples/flow/04-complex-backend.ts +7 -4
- package/examples/flow-experimental/04-complex-backend.ts +7 -4
- package/examples/match/01-stable-match.ts +7 -0
- package/examples/match-experimental/01-experimental-match.ts +7 -0
- package/package.json +22 -31
package/README.md
CHANGED
|
@@ -182,7 +182,7 @@ Use `match()` for fluent, exhaustive pattern matching across stable and experime
|
|
|
182
182
|
import { match, P, Result } from "@carbonteq/fp";
|
|
183
183
|
|
|
184
184
|
const message = match(Result.Ok(42) as Result<number, string>)
|
|
185
|
-
.with(P.Ok(P.eq(42)), () =>
|
|
185
|
+
.with(P.Ok(P.eq(42)), (value) => `exactly ${value}`)
|
|
186
186
|
.with(P.Ok(P.not(P.oneOf(42, 43))), () => "ok but not 42 or 43")
|
|
187
187
|
.with(P.Ok(), (value) => `other ok: ${value}`)
|
|
188
188
|
.with(P.Err(), (error) => `error: ${error}`)
|
|
@@ -200,6 +200,13 @@ const combined = match(Result.Ok(42) as Result<number, string>)
|
|
|
200
200
|
.exhaustive();
|
|
201
201
|
```
|
|
202
202
|
|
|
203
|
+
Handler inputs:
|
|
204
|
+
|
|
205
|
+
- `.with(P.Ok(...), handler)` receives the unwrapped `Ok` value.
|
|
206
|
+
- `.with(P.Err(...), handler)` receives the unwrapped `Err` value.
|
|
207
|
+
- `.with(P.Some(...), handler)` receives the unwrapped `Some` value.
|
|
208
|
+
- `.with(P._, handler)` and `.when(...)` handlers receive the whole matched value.
|
|
209
|
+
|
|
203
210
|
`match()` is intentionally synchronous. If you have async-inner stable values, settle first:
|
|
204
211
|
|
|
205
212
|
```typescript
|
|
@@ -218,7 +225,9 @@ const label = match(settled)
|
|
|
218
225
|
|
|
219
226
|
## The `ExperimentalResult` type (Experimental)
|
|
220
227
|
|
|
221
|
-
`ExperimentalResult` differs from the stable `Result` by providing explicit async methods like `mapAsync` and `flatMapAsync` that return `Promise<ExperimentalResult<T,
|
|
228
|
+
`ExperimentalResult` differs from the stable `Result` by providing explicit async methods like `mapAsync` and `flatMapAsync` that return `Promise<ExperimentalResult<T, unknown>>` directly, rather than returning `Result<Promise<T>, E>` and requiring `toPromise()`. Returned-promise rejections become `Err` with the original rejection value, which need not be an `Error`. Narrow errors before accessing properties, for example `error instanceof Error ? error.message : String(error)`. A promise-typed mapper that throws synchronously still rejects the method's promise. Both methods skip the mapper for an existing `Err`. It also includes generator-based APIs (`gen`, `asyncGen`, and adapter variants) used by `Flow`.
|
|
229
|
+
|
|
230
|
+
`ExperimentalResult.validate` now accepts synchronous validators only. Migrate `await result.validate([asyncValidator])` to `await result.validateAsync([asyncValidator])`. `validateAsync` always returns a Promise, including when the input is already Err. Stable `Result.validate` is unchanged.
|
|
222
231
|
|
|
223
232
|
```typescript
|
|
224
233
|
import { ExperimentalResult } from "@carbonteq/fp";
|
|
@@ -238,6 +247,8 @@ import { ExperimentalOption } from "@carbonteq/fp";
|
|
|
238
247
|
|
|
239
248
|
`Flow` is the stable generator namespace and works with stable `Option` + `Result`, returning stable `Result`.
|
|
240
249
|
|
|
250
|
+
Generator composition in all six namespaces (`Option`, `Result`, `Flow`, and their experimental counterparts) runs suspended `finally` cleanup when a yielded effect fails. Cleanup may yield further effects and use their resumed values; async generators wait for cleanup to settle before returning. Once a runner has observed a primary failure or exception, subsequent cleanup failures do not replace it. JavaScript's ordinary `finally` control-flow rules still apply before the runner observes a failure.
|
|
251
|
+
|
|
241
252
|
```typescript
|
|
242
253
|
import { Flow, Option, Result, UnwrappedNone } from "@carbonteq/fp";
|
|
243
254
|
|
|
@@ -701,7 +712,11 @@ console.log(validatedErrs.unwrapErr()); // [Error: New password cannot be the sa
|
|
|
701
712
|
|
|
702
713
|
#### `unwrap`, `unwrapOr`, `unwrapOrElse`, `safeUnwrap`, and `unwrapErr`
|
|
703
714
|
|
|
704
|
-
These functions
|
|
715
|
+
These functions extract values from a `Result`. `Option` also supports `unwrap`, `unwrapOr`, `unwrapOrElse`, and `safeUnwrap`.
|
|
716
|
+
|
|
717
|
+
For stable `Option`, `unwrapOr` supplies a default and `unwrapOrElse` computes it only for absence. Promise-backed options wait for settlement and always return a promise, including after settling to None. Rejected payloads still reject without invoking the factory.
|
|
718
|
+
|
|
719
|
+
For `Option<Promise<T>>`, a resolved fallback or factory has return type `Awaited<T> | Promise<Awaited<T>>`: `Option.None` can be widened to that receiver type but still returns a resolved fallback synchronously. A promised fallback or promise-returning factory guarantees `Promise<Awaited<T>>`. Fallbacks may also be a union of resolved and promised values. Ordinary synchronous options stay synchronous, and direct `Option.None` calls infer the fallback's type.
|
|
705
720
|
|
|
706
721
|
```typescript
|
|
707
722
|
import { Result } from "@carbonteq/fp";
|
package/dist/errors.cjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
//#region src/errors.ts
|
|
2
|
+
// src/errors.ts
|
|
4
3
|
var UnwrappedErrWithOk = class extends Error {
|
|
5
4
|
constructor(result) {
|
|
6
5
|
super(`Attempted to call unwrapErr on an Ok result: ${result}`);
|
|
@@ -14,7 +13,6 @@ var UnwrappedOkWithErr = class extends Error {
|
|
|
14
13
|
}
|
|
15
14
|
};
|
|
16
15
|
|
|
17
|
-
//#endregion
|
|
18
16
|
exports.UnwrappedErrWithOk = UnwrappedErrWithOk;
|
|
19
17
|
exports.UnwrappedOkWithErr = UnwrappedOkWithErr;
|
|
20
18
|
//# sourceMappingURL=errors.cjs.map
|
package/dist/errors.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.cjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["// Error classes\nexport class UnwrappedErrWithOk extends Error {\n constructor(result: string) {\n super(`Attempted to call unwrapErr on an Ok result: ${result}`)\n this.name = \"UnwrappedErrWithOk\"\n }\n}\n\nexport class UnwrappedOkWithErr extends Error {\n constructor(result: string) {\n super(`Attempted to call unwrap on an Err result: ${result}`)\n this.name = \"UnwrappedOkWithErr\"\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.cjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["// Error classes\nexport class UnwrappedErrWithOk extends Error {\n constructor(result: string) {\n super(`Attempted to call unwrapErr on an Ok result: ${result}`)\n this.name = \"UnwrappedErrWithOk\"\n }\n}\n\nexport class UnwrappedOkWithErr extends Error {\n constructor(result: string) {\n super(`Attempted to call unwrap on an Err result: ${result}`)\n this.name = \"UnwrappedOkWithErr\"\n }\n}\n"],"mappings":";;AACA,IAAa,qBAAb,cAAwC,MAAM;CAC5C,YAAY,QAAgB;EAC1B,MAAM,gDAAgD,QAAQ;EAC9D,KAAK,OAAO;CACd;AACF;AAEA,IAAa,qBAAb,cAAwC,MAAM;CAC5C,YAAY,QAAgB;EAC1B,MAAM,8CAA8C,QAAQ;EAC5D,KAAK,OAAO;CACd;AACF"}
|
package/dist/errors.d.cts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
declare class UnwrappedErrWithOk extends Error {
|
|
1
|
+
export declare class UnwrappedErrWithOk extends Error {
|
|
3
2
|
constructor(result: string);
|
|
4
3
|
}
|
|
5
|
-
declare class UnwrappedOkWithErr extends Error {
|
|
4
|
+
export declare class UnwrappedOkWithErr extends Error {
|
|
6
5
|
constructor(result: string);
|
|
7
6
|
}
|
|
8
|
-
//#endregion
|
|
9
|
-
export { UnwrappedErrWithOk, UnwrappedOkWithErr };
|
|
10
7
|
//# sourceMappingURL=errors.d.cts.map
|
package/dist/errors.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.cts","names":[],"sources":["../src/errors.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.d.cts","names":[],"sources":["../src/errors.ts"],"mappings":"A,qBACa,2BAA2B;EACtC,YAAY;;qBAMD,2BAA2B;EACtC,YAAY"}
|
package/dist/errors.d.mts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
declare class UnwrappedErrWithOk extends Error {
|
|
1
|
+
export declare class UnwrappedErrWithOk extends Error {
|
|
3
2
|
constructor(result: string);
|
|
4
3
|
}
|
|
5
|
-
declare class UnwrappedOkWithErr extends Error {
|
|
4
|
+
export declare class UnwrappedOkWithErr extends Error {
|
|
6
5
|
constructor(result: string);
|
|
7
6
|
}
|
|
8
|
-
//#endregion
|
|
9
|
-
export { UnwrappedErrWithOk, UnwrappedOkWithErr };
|
|
10
7
|
//# sourceMappingURL=errors.d.mts.map
|
package/dist/errors.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.mts","names":[],"sources":["../src/errors.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.d.mts","names":[],"sources":["../src/errors.ts"],"mappings":"A,qBACa,2BAA2B;EACtC,YAAY;;qBAMD,2BAA2B;EACtC,YAAY"}
|
package/dist/errors.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/errors.ts
|
|
2
2
|
var UnwrappedErrWithOk = class extends Error {
|
|
3
3
|
constructor(result) {
|
|
4
4
|
super(`Attempted to call unwrapErr on an Ok result: ${result}`);
|
|
@@ -12,6 +12,5 @@ var UnwrappedOkWithErr = class extends Error {
|
|
|
12
12
|
}
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
//#endregion
|
|
16
15
|
export { UnwrappedErrWithOk, UnwrappedOkWithErr };
|
|
17
16
|
//# sourceMappingURL=errors.mjs.map
|
package/dist/errors.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["// Error classes\nexport class UnwrappedErrWithOk extends Error {\n constructor(result: string) {\n super(`Attempted to call unwrapErr on an Ok result: ${result}`)\n this.name = \"UnwrappedErrWithOk\"\n }\n}\n\nexport class UnwrappedOkWithErr extends Error {\n constructor(result: string) {\n super(`Attempted to call unwrap on an Err result: ${result}`)\n this.name = \"UnwrappedOkWithErr\"\n }\n}\n"],"mappings":";AACA,IAAa,qBAAb,cAAwC,MAAM;CAC5C,YAAY,QAAgB;
|
|
1
|
+
{"version":3,"file":"errors.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["// Error classes\nexport class UnwrappedErrWithOk extends Error {\n constructor(result: string) {\n super(`Attempted to call unwrapErr on an Ok result: ${result}`)\n this.name = \"UnwrappedErrWithOk\"\n }\n}\n\nexport class UnwrappedOkWithErr extends Error {\n constructor(result: string) {\n super(`Attempted to call unwrap on an Err result: ${result}`)\n this.name = \"UnwrappedOkWithErr\"\n }\n}\n"],"mappings":";AACA,IAAa,qBAAb,cAAwC,MAAM;CAC5C,YAAY,QAAgB;EAC1B,MAAM,gDAAgD,QAAQ;EAC9D,KAAK,OAAO;CACd;AACF;AAEA,IAAa,qBAAb,cAAwC,MAAM;CAC5C,YAAY,QAAgB;EAC1B,MAAM,8CAA8C,QAAQ;EAC5D,KAAK,OAAO;CACd;AACF"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const
|
|
3
|
-
const require_result_experimental = require(
|
|
4
|
-
const
|
|
2
|
+
const require_option_experimental = require("./option-experimental.cjs");
|
|
3
|
+
const require_result_experimental = require("./result-experimental.cjs");
|
|
4
|
+
const require_utils = require("./utils.cjs");
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
// src/flow-experimental.ts
|
|
7
7
|
function isOption(value) {
|
|
8
8
|
if (value instanceof require_option_experimental.ExperimentalOption) return true;
|
|
9
9
|
if (typeof value !== "object" || value === null) return false;
|
|
@@ -52,7 +52,14 @@ var ExperimentalFlowError = class extends Error {
|
|
|
52
52
|
function isFlowError(value) {
|
|
53
53
|
return value instanceof ExperimentalFlowError;
|
|
54
54
|
}
|
|
55
|
+
function resolveCleanupYield(value) {
|
|
56
|
+
if (require_utils.isCapturedTrace(value)) value = value.value;
|
|
57
|
+
if (isFlowError(value)) return void 0;
|
|
58
|
+
if (isOption(value)) return value.isNone() ? void 0 : value.unwrap();
|
|
59
|
+
if (isResult(value)) return value.isErr() ? void 0 : value.unwrap();
|
|
60
|
+
}
|
|
55
61
|
var XFlowYieldWrap = class {
|
|
62
|
+
value;
|
|
56
63
|
constructor(value) {
|
|
57
64
|
this.value = value;
|
|
58
65
|
}
|
|
@@ -80,51 +87,59 @@ var ExperimentalFlow = class {
|
|
|
80
87
|
static gen(genFn) {
|
|
81
88
|
const iterator = genFn();
|
|
82
89
|
let nextArg;
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const userStack = stackLines.slice(2).join("\n");
|
|
97
|
-
value.stack = `${value.name}: ${value.message}\n${userStack}`;
|
|
98
|
-
}
|
|
90
|
+
let naturalCompleted = false;
|
|
91
|
+
try {
|
|
92
|
+
while (true) {
|
|
93
|
+
const next = iterator.next(nextArg);
|
|
94
|
+
if (next.done) {
|
|
95
|
+
naturalCompleted = true;
|
|
96
|
+
return require_result_experimental.ExperimentalResult.Ok(next.value);
|
|
97
|
+
}
|
|
98
|
+
let value = next.value;
|
|
99
|
+
let stack;
|
|
100
|
+
if (require_utils.isCapturedTrace(value)) {
|
|
101
|
+
stack = value.stack;
|
|
102
|
+
value = value.value;
|
|
99
103
|
}
|
|
100
|
-
|
|
101
|
-
} else if (isOption(value)) {
|
|
102
|
-
if (value.isNone()) {
|
|
103
|
-
const err = new require_option_experimental.UnwrappedNone();
|
|
104
|
+
if (isFlowError(value)) {
|
|
104
105
|
if (stack) {
|
|
105
106
|
const stackLines = stack.split("\n");
|
|
106
107
|
if (stackLines.length > 2) {
|
|
107
108
|
const userStack = stackLines.slice(2).join("\n");
|
|
108
|
-
|
|
109
|
+
value.stack = `${value.name}: ${value.message}\n${userStack}`;
|
|
109
110
|
}
|
|
110
111
|
}
|
|
111
|
-
return require_result_experimental.ExperimentalResult.Err(
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
112
|
+
return require_result_experimental.ExperimentalResult.Err(value);
|
|
113
|
+
} else if (isOption(value)) {
|
|
114
|
+
if (value.isNone()) {
|
|
115
|
+
const err = new require_option_experimental.UnwrappedNone();
|
|
116
|
+
if (stack) {
|
|
117
|
+
const stackLines = stack.split("\n");
|
|
118
|
+
if (stackLines.length > 2) {
|
|
119
|
+
const userStack = stackLines.slice(2).join("\n");
|
|
120
|
+
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
121
|
+
}
|
|
122
122
|
}
|
|
123
|
+
return require_result_experimental.ExperimentalResult.Err(err);
|
|
123
124
|
}
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
125
|
+
nextArg = value.unwrap();
|
|
126
|
+
} else if (isResult(value)) {
|
|
127
|
+
if (value.isErr()) {
|
|
128
|
+
const err = value.unwrapErr();
|
|
129
|
+
if (stack && err instanceof Error) {
|
|
130
|
+
const stackLines = stack.split("\n");
|
|
131
|
+
if (stackLines.length > 2) {
|
|
132
|
+
const userStack = stackLines.slice(2).join("\n");
|
|
133
|
+
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
nextArg = value.unwrap();
|
|
139
|
+
} else throw new Error("Flow.gen yielded unknown type");
|
|
140
|
+
}
|
|
141
|
+
} finally {
|
|
142
|
+
if (!naturalCompleted) require_utils.closeGenerator(iterator, resolveCleanupYield);
|
|
128
143
|
}
|
|
129
144
|
}
|
|
130
145
|
/** Adapter variant of `gen` for improved type inference. */
|
|
@@ -132,155 +147,186 @@ var ExperimentalFlow = class {
|
|
|
132
147
|
const baseAdapter = (val) => new XFlowYieldWrap(val);
|
|
133
148
|
const iterator = genFn(Object.assign(baseAdapter, { fail: (error) => new XFlowYieldWrap(require_result_experimental.ExperimentalResult.Err(error)) }));
|
|
134
149
|
let nextArg;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
150
|
+
let naturalCompleted = false;
|
|
151
|
+
try {
|
|
152
|
+
while (true) {
|
|
153
|
+
const next = iterator.next(nextArg);
|
|
154
|
+
if (next.done) {
|
|
155
|
+
naturalCompleted = true;
|
|
156
|
+
return require_result_experimental.ExperimentalResult.Ok(next.value);
|
|
157
|
+
}
|
|
158
|
+
let wrapped = next.value;
|
|
159
|
+
let stack;
|
|
160
|
+
if (require_utils.isCapturedTrace(wrapped)) {
|
|
161
|
+
stack = wrapped.stack;
|
|
162
|
+
wrapped = wrapped.value;
|
|
163
|
+
} else wrapped = wrapped;
|
|
164
|
+
const value = wrapped.value;
|
|
165
|
+
if (isOption(value)) {
|
|
166
|
+
if (value.isNone()) {
|
|
167
|
+
const err = new require_option_experimental.UnwrappedNone();
|
|
168
|
+
if (stack) {
|
|
169
|
+
const stackLines = stack.split("\n");
|
|
170
|
+
if (stackLines.length > 2) {
|
|
171
|
+
const userStack = stackLines.slice(2).join("\n");
|
|
172
|
+
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
173
|
+
}
|
|
153
174
|
}
|
|
175
|
+
return require_result_experimental.ExperimentalResult.Err(err);
|
|
154
176
|
}
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
177
|
+
nextArg = value.unwrap();
|
|
178
|
+
} else if (isResult(value)) {
|
|
179
|
+
if (value.isErr()) {
|
|
180
|
+
const err = value.unwrapErr();
|
|
181
|
+
if (stack && err instanceof Error) {
|
|
182
|
+
const stackLines = stack.split("\n");
|
|
183
|
+
if (stackLines.length > 2) {
|
|
184
|
+
const userStack = stackLines.slice(2).join("\n");
|
|
185
|
+
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
186
|
+
}
|
|
166
187
|
}
|
|
188
|
+
return value;
|
|
167
189
|
}
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
190
|
+
nextArg = value.unwrap();
|
|
191
|
+
} else throw new Error("ExperimentalFlow.genAdapter yielded unknown type");
|
|
192
|
+
}
|
|
193
|
+
} finally {
|
|
194
|
+
if (!naturalCompleted) require_utils.closeGenerator(iterator, (yielded) => {
|
|
195
|
+
return resolveCleanupYield((require_utils.isCapturedTrace(yielded) ? yielded.value : yielded).value);
|
|
196
|
+
});
|
|
172
197
|
}
|
|
173
198
|
}
|
|
174
199
|
/** Runs an async experimental flow generator and short-circuits on failure. */
|
|
175
200
|
static async asyncGen(genFn) {
|
|
176
201
|
const iterator = genFn();
|
|
177
202
|
let nextArg;
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
value = value.value;
|
|
186
|
-
}
|
|
187
|
-
if (isFlowError(value)) {
|
|
188
|
-
if (stack) {
|
|
189
|
-
const stackLines = stack.split("\n");
|
|
190
|
-
if (stackLines.length > 2) {
|
|
191
|
-
const userStack = stackLines.slice(2).join("\n");
|
|
192
|
-
value.stack = `${value.name}: ${value.message}\n${userStack}`;
|
|
193
|
-
}
|
|
203
|
+
let naturalCompleted = false;
|
|
204
|
+
try {
|
|
205
|
+
while (true) {
|
|
206
|
+
const next = await iterator.next(nextArg);
|
|
207
|
+
if (next.done) {
|
|
208
|
+
naturalCompleted = true;
|
|
209
|
+
return require_result_experimental.ExperimentalResult.Ok(next.value);
|
|
194
210
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (
|
|
198
|
-
|
|
211
|
+
let value = next.value;
|
|
212
|
+
let stack;
|
|
213
|
+
if (require_utils.isCapturedTrace(value)) {
|
|
214
|
+
stack = value.stack;
|
|
215
|
+
value = value.value;
|
|
216
|
+
}
|
|
217
|
+
if (isFlowError(value)) {
|
|
199
218
|
if (stack) {
|
|
200
219
|
const stackLines = stack.split("\n");
|
|
201
220
|
if (stackLines.length > 2) {
|
|
202
221
|
const userStack = stackLines.slice(2).join("\n");
|
|
203
|
-
|
|
222
|
+
value.stack = `${value.name}: ${value.message}\n${userStack}`;
|
|
204
223
|
}
|
|
205
224
|
}
|
|
206
|
-
return require_result_experimental.ExperimentalResult.Err(
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
225
|
+
return require_result_experimental.ExperimentalResult.Err(value);
|
|
226
|
+
} else if (isOption(value)) {
|
|
227
|
+
if (value.isNone()) {
|
|
228
|
+
const err = new require_option_experimental.UnwrappedNone();
|
|
229
|
+
if (stack) {
|
|
230
|
+
const stackLines = stack.split("\n");
|
|
231
|
+
if (stackLines.length > 2) {
|
|
232
|
+
const userStack = stackLines.slice(2).join("\n");
|
|
233
|
+
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
234
|
+
}
|
|
217
235
|
}
|
|
236
|
+
return require_result_experimental.ExperimentalResult.Err(err);
|
|
218
237
|
}
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
238
|
+
nextArg = value.unwrap();
|
|
239
|
+
} else if (isResult(value)) {
|
|
240
|
+
if (value.isErr()) {
|
|
241
|
+
const err = value.unwrapErr();
|
|
242
|
+
if (stack && err instanceof Error) {
|
|
243
|
+
const stackLines = stack.split("\n");
|
|
244
|
+
if (stackLines.length > 2) {
|
|
245
|
+
const userStack = stackLines.slice(2).join("\n");
|
|
246
|
+
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return value;
|
|
250
|
+
}
|
|
251
|
+
nextArg = value.unwrap();
|
|
252
|
+
} else throw new Error("ExperimentalFlow.asyncGen yielded unknown type");
|
|
253
|
+
}
|
|
254
|
+
} finally {
|
|
255
|
+
if (!naturalCompleted) await require_utils.closeAsyncGenerator(iterator, resolveCleanupYield);
|
|
223
256
|
}
|
|
224
257
|
}
|
|
225
|
-
/**
|
|
258
|
+
/**
|
|
259
|
+
* Adapter variant of `asyncGen` for ergonomic mixed sync/async yielding.
|
|
260
|
+
* Promise rejections become Err values with an unknown error type.
|
|
261
|
+
* Delegating a promised payload yields its awaited value.
|
|
262
|
+
*/
|
|
226
263
|
static async asyncGenAdapter(genFn) {
|
|
227
264
|
const baseAdapter = (val) => new AsyncXFlowYieldWrap(val);
|
|
228
265
|
const iterator = genFn(Object.assign(baseAdapter, { fail: (error) => new AsyncXFlowYieldWrap(require_result_experimental.ExperimentalResult.Err(error)) }));
|
|
229
266
|
let nextArg;
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
wrapped = wrapped.value;
|
|
238
|
-
} else wrapped = wrapped;
|
|
239
|
-
let value;
|
|
240
|
-
try {
|
|
241
|
-
value = await wrapped.value;
|
|
242
|
-
} catch (error) {
|
|
243
|
-
if (stack && error instanceof Error) {
|
|
244
|
-
const stackLines = stack.split("\n");
|
|
245
|
-
if (stackLines.length > 2) {
|
|
246
|
-
const userStack = stackLines.slice(2).join("\n");
|
|
247
|
-
error.stack = `${error.name}: ${error.message}\n${userStack}`;
|
|
248
|
-
}
|
|
267
|
+
let naturalCompleted = false;
|
|
268
|
+
try {
|
|
269
|
+
while (true) {
|
|
270
|
+
const next = await iterator.next(nextArg);
|
|
271
|
+
if (next.done) {
|
|
272
|
+
naturalCompleted = true;
|
|
273
|
+
return require_result_experimental.ExperimentalResult.Ok(next.value);
|
|
249
274
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
275
|
+
let wrapped = next.value;
|
|
276
|
+
let stack;
|
|
277
|
+
if (require_utils.isCapturedTrace(wrapped)) {
|
|
278
|
+
stack = wrapped.stack;
|
|
279
|
+
wrapped = wrapped.value;
|
|
280
|
+
} else wrapped = wrapped;
|
|
281
|
+
let value;
|
|
282
|
+
try {
|
|
283
|
+
value = await wrapped.value;
|
|
284
|
+
} catch (error) {
|
|
285
|
+
if (stack && error instanceof Error) {
|
|
256
286
|
const stackLines = stack.split("\n");
|
|
257
287
|
if (stackLines.length > 2) {
|
|
258
288
|
const userStack = stackLines.slice(2).join("\n");
|
|
259
|
-
|
|
289
|
+
error.stack = `${error.name}: ${error.message}\n${userStack}`;
|
|
260
290
|
}
|
|
261
291
|
}
|
|
262
|
-
return require_result_experimental.ExperimentalResult.Err(
|
|
292
|
+
return require_result_experimental.ExperimentalResult.Err(error);
|
|
263
293
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
294
|
+
if (isOption(value)) {
|
|
295
|
+
if (value.isNone()) {
|
|
296
|
+
const err = new require_option_experimental.UnwrappedNone();
|
|
297
|
+
if (stack) {
|
|
298
|
+
const stackLines = stack.split("\n");
|
|
299
|
+
if (stackLines.length > 2) {
|
|
300
|
+
const userStack = stackLines.slice(2).join("\n");
|
|
301
|
+
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
302
|
+
}
|
|
273
303
|
}
|
|
304
|
+
return require_result_experimental.ExperimentalResult.Err(err);
|
|
274
305
|
}
|
|
275
|
-
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
|
|
306
|
+
nextArg = value.unwrap();
|
|
307
|
+
} else if (isResult(value)) {
|
|
308
|
+
if (value.isErr()) {
|
|
309
|
+
const err = value.unwrapErr();
|
|
310
|
+
if (stack && err instanceof Error) {
|
|
311
|
+
const stackLines = stack.split("\n");
|
|
312
|
+
if (stackLines.length > 2) {
|
|
313
|
+
const userStack = stackLines.slice(2).join("\n");
|
|
314
|
+
err.stack = `${err.name}: ${err.message}\n${userStack}`;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return value;
|
|
318
|
+
}
|
|
319
|
+
nextArg = value.unwrap();
|
|
320
|
+
} else throw new Error("ExperimentalFlow.asyncGenAdapter yielded unknown type");
|
|
321
|
+
}
|
|
322
|
+
} finally {
|
|
323
|
+
if (!naturalCompleted) await require_utils.closeAsyncGenerator(iterator, async (yielded) => {
|
|
324
|
+
return resolveCleanupYield(await (require_utils.isCapturedTrace(yielded) ? yielded.value : yielded).value);
|
|
325
|
+
});
|
|
279
326
|
}
|
|
280
327
|
}
|
|
281
328
|
};
|
|
282
329
|
|
|
283
|
-
//#endregion
|
|
284
330
|
exports.ExperimentalFlow = ExperimentalFlow;
|
|
285
331
|
exports.ExperimentalFlowError = ExperimentalFlowError;
|
|
286
332
|
//# sourceMappingURL=flow-experimental.cjs.map
|