@typed/async-data 1.0.0-beta.4 → 1.0.0-beta.5
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 +32 -21
- package/dist/index.d.ts +664 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +501 -35
- package/package.json +22 -14
- package/src/__tests__/assert.type-test.ts +4 -0
- package/src/__tests__/flat-map.type-test.ts +29 -0
- package/src/index.ts +807 -41
- package/src/AsyncData.test.ts +0 -444
- package/src/index.test.ts +0 -497
package/README.md
CHANGED
|
@@ -39,6 +39,14 @@ AsyncData(A, E): Schema.Codec<AsyncData<A, E>, ...>
|
|
|
39
39
|
|
|
40
40
|
Builds an Effect Schema codec for `AsyncData` given value schema `A` and error schema `E`.
|
|
41
41
|
|
|
42
|
+
`Failure.cause` uses the full Effect `Cause` model. Its encoded form is an ordered JSON array of
|
|
43
|
+
`Fail`, `Die`, and `Interrupt` reasons; defects are normalized with `Schema.Defect()`. For example,
|
|
44
|
+
`failure(Cause.fail("offline"))` encodes its cause as
|
|
45
|
+
`[{ _tag: "Fail", error: "offline" }]` and decodes back to a `Cause.Cause<string>`.
|
|
46
|
+
|
|
47
|
+
Progress codecs accept finite numbers, including negative and fractional values. They reject
|
|
48
|
+
`NaN`, `Infinity`, and `-Infinity` without imposing count or percentage semantics.
|
|
49
|
+
|
|
42
50
|
### Constructors
|
|
43
51
|
|
|
44
52
|
```ts
|
|
@@ -65,15 +73,13 @@ isPending<A, E>(asyncData: AsyncData<A, E>): asyncData is Loading | Refreshing<A
|
|
|
65
73
|
### Pattern matching
|
|
66
74
|
|
|
67
75
|
```ts
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}),
|
|
76
|
-
);
|
|
76
|
+
const label = AsyncData.match(AsyncData.success(1), {
|
|
77
|
+
NoData: () => "no data",
|
|
78
|
+
Loading: () => "loading",
|
|
79
|
+
Failure: () => "failure",
|
|
80
|
+
Success: (value) => `success: ${value}`,
|
|
81
|
+
Optimistic: (value) => `optimistic: ${value}`,
|
|
82
|
+
});
|
|
77
83
|
```
|
|
78
84
|
|
|
79
85
|
Returns a unified result type from the matching branch. Callbacks receive the variant payload (and full variant for `Failure`/`Success`/`Optimistic`).
|
|
@@ -103,19 +109,26 @@ Extract the success value, failure cause, or first error from the variant (or `O
|
|
|
103
109
|
### Transformers
|
|
104
110
|
|
|
105
111
|
```ts
|
|
106
|
-
|
|
112
|
+
const mapped = AsyncData.map(AsyncData.success(1), (value) => value + 1);
|
|
107
113
|
```
|
|
108
114
|
|
|
109
115
|
Maps the success/optimistic value; NoData, Loading, and Failure are unchanged.
|
|
110
116
|
|
|
111
117
|
```ts
|
|
112
|
-
|
|
118
|
+
const chained = AsyncData.flatMap(AsyncData.success(1), (value) =>
|
|
119
|
+
AsyncData.success(String(value)),
|
|
120
|
+
);
|
|
113
121
|
```
|
|
114
122
|
|
|
115
123
|
Chains on success or optimistic; NoData, Loading, and Failure are unchanged.
|
|
116
124
|
|
|
117
125
|
```ts
|
|
118
|
-
|
|
126
|
+
import * as Cause from "effect/Cause";
|
|
127
|
+
|
|
128
|
+
const mappedFailure = AsyncData.mapError(
|
|
129
|
+
AsyncData.failure(Cause.fail("offline")),
|
|
130
|
+
(error) => new Error(error),
|
|
131
|
+
);
|
|
119
132
|
```
|
|
120
133
|
|
|
121
134
|
Maps the failure error type; Success, NoData, Loading unchanged; Optimistic recurs on `previous`.
|
|
@@ -135,15 +148,13 @@ import * as AsyncData from "@typed/async-data";
|
|
|
135
148
|
|
|
136
149
|
// Success and match
|
|
137
150
|
const data = AsyncData.success({ id: 1, name: "Alice" });
|
|
138
|
-
const message =
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}),
|
|
146
|
-
);
|
|
151
|
+
const message = AsyncData.match(data, {
|
|
152
|
+
NoData: () => "no data",
|
|
153
|
+
Loading: () => "loading...",
|
|
154
|
+
Failure: (cause) => `error: ${cause}`,
|
|
155
|
+
Success: (user) => `user: ${user.name}`,
|
|
156
|
+
Optimistic: (user) => `optimistic: ${user.name}`,
|
|
157
|
+
});
|
|
147
158
|
// message === "user: Alice"
|
|
148
159
|
|
|
149
160
|
// From Result and getSuccess
|