@typed/async-data 1.0.0-beta.4 → 1.0.0-beta.6

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 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
- data.pipe(
69
- AsyncData.match({
70
- NoData: (data) => R1,
71
- Loading: (data) => R2,
72
- Failure: (cause, data) => R3,
73
- Success: (value, data) => R4,
74
- Optimistic: (value, data) => R5,
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
- data.pipe(AsyncData.map((a: A) => B));
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
- data.pipe(AsyncData.flatMap((value: A, data: Success<A> | Optimistic<A, E>) => AsyncData<B, E2>));
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
- data.pipe(AsyncData.mapError((e: E) => E2));
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 = data.pipe(
139
- AsyncData.match({
140
- NoData: () => "no data",
141
- Loading: () => "loading...",
142
- Failure: (cause) => `error: ${cause}`,
143
- Success: (user) => `user: ${user.name}`,
144
- Optimistic: (user) => `optimistic: ${user.name}`,
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