@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 +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 +16 -8
- 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/dist/index.d.ts
CHANGED
|
@@ -4,52 +4,592 @@ import * as Option from "effect/Option";
|
|
|
4
4
|
import * as Result from "effect/Result";
|
|
5
5
|
import * as Schema from "effect/Schema";
|
|
6
6
|
import type { Unify } from "effect/Unify";
|
|
7
|
+
/**
|
|
8
|
+
* Reports completed work and, when known, the total amount of work.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* ## Why
|
|
12
|
+
* Progress belongs to the value so loading and refreshing states can carry the same transportable measurement.
|
|
13
|
+
*
|
|
14
|
+
* ## Ownership and lifetime
|
|
15
|
+
* This plain object acquires no resources. Its `readonly` fields are a TypeScript constraint; the runtime value is not frozen.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import type { Progress } from "@typed/async-data"
|
|
20
|
+
* const progress: Progress = { loaded: 4, total: 10 }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @category Models
|
|
24
|
+
* @since 1.0.0
|
|
25
|
+
*/
|
|
7
26
|
export interface Progress {
|
|
27
|
+
/**
|
|
28
|
+
* The finite amount of work completed so far.
|
|
29
|
+
* @remarks
|
|
30
|
+
* ## Why
|
|
31
|
+
* A required counter lets consumers render useful progress even when the total is unknown.
|
|
32
|
+
* ## Ownership and lifetime
|
|
33
|
+
* Inherits the resource-free lifetime of its enclosing `Progress` value.
|
|
34
|
+
* @category Models
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
*/
|
|
8
37
|
readonly loaded: number;
|
|
38
|
+
/**
|
|
39
|
+
* The finite total amount of work, when the producer knows it.
|
|
40
|
+
* @remarks
|
|
41
|
+
* ## Why
|
|
42
|
+
* Optionality distinguishes indeterminate work from a known total without inventing a sentinel value.
|
|
43
|
+
* ## Ownership and lifetime
|
|
44
|
+
* Inherits the resource-free lifetime of its enclosing `Progress` value.
|
|
45
|
+
* @category Models
|
|
46
|
+
* @since 1.0.0
|
|
47
|
+
*/
|
|
9
48
|
readonly total?: number | undefined;
|
|
10
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Represents an asynchronous value before work or a result exists.
|
|
52
|
+
* @remarks
|
|
53
|
+
* ## Why
|
|
54
|
+
* A distinct initial state prevents absence from being confused with loading, failure, or a successful `undefined` value.
|
|
55
|
+
* ## Ownership and lifetime
|
|
56
|
+
* This shape acquires no resources. `readonly` is compile-time only; values matching the interface need not be frozen.
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { NoData } from "@typed/async-data"
|
|
60
|
+
* const initial = NoData
|
|
61
|
+
* ```
|
|
62
|
+
* @category Models
|
|
63
|
+
* @since 1.0.0
|
|
64
|
+
*/
|
|
11
65
|
export interface NoData {
|
|
66
|
+
/**
|
|
67
|
+
* The discriminant for the initial state.
|
|
68
|
+
* @remarks
|
|
69
|
+
* ## Why
|
|
70
|
+
* A literal tag enables exhaustive matching without runtime class identity.
|
|
71
|
+
* ## Ownership and lifetime
|
|
72
|
+
* Inherits the resource-free lifetime of its enclosing state.
|
|
73
|
+
* @category Models
|
|
74
|
+
* @since 1.0.0
|
|
75
|
+
*/
|
|
12
76
|
readonly _tag: "NoData";
|
|
13
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Represents active work that has not produced a prior value or failure.
|
|
80
|
+
* @remarks
|
|
81
|
+
* ## Why
|
|
82
|
+
* Loading is separate from refreshing so consumers can decide whether stale content remains available.
|
|
83
|
+
* ## Ownership and lifetime
|
|
84
|
+
* This plain object acquires no resources; the operation it describes is owned elsewhere and the object is not frozen.
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* import { loading } from "@typed/async-data"
|
|
88
|
+
* const state = loading({ loaded: 0 })
|
|
89
|
+
* ```
|
|
90
|
+
* @category Models
|
|
91
|
+
* @since 1.0.0
|
|
92
|
+
*/
|
|
14
93
|
export interface Loading {
|
|
94
|
+
/**
|
|
95
|
+
* The discriminant for loading without a prior result.
|
|
96
|
+
* @remarks
|
|
97
|
+
* ## Why
|
|
98
|
+
* The literal tag makes loading branches explicit and exhaustively checkable.
|
|
99
|
+
* ## Ownership and lifetime
|
|
100
|
+
* Inherits the resource-free lifetime of its enclosing state.
|
|
101
|
+
* @category Models
|
|
102
|
+
* @since 1.0.0
|
|
103
|
+
*/
|
|
15
104
|
readonly _tag: "Loading";
|
|
105
|
+
/**
|
|
106
|
+
* Optional progress reported by the active producer.
|
|
107
|
+
* @remarks
|
|
108
|
+
* ## Why
|
|
109
|
+
* Keeping progress optional supports both determinate and uninstrumented operations.
|
|
110
|
+
* ## Ownership and lifetime
|
|
111
|
+
* Inherits the resource-free lifetime of its enclosing state.
|
|
112
|
+
* @category Models
|
|
113
|
+
* @since 1.0.0
|
|
114
|
+
*/
|
|
16
115
|
readonly progress?: Progress | undefined;
|
|
17
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Represents a successfully produced value, optionally while it refreshes.
|
|
119
|
+
* @remarks
|
|
120
|
+
* ## Why
|
|
121
|
+
* Success keeps the last usable value available while progress can describe newer work.
|
|
122
|
+
* ## Ownership and lifetime
|
|
123
|
+
* This plain wrapper acquires no resources; ownership of `value` remains with the caller and `readonly` does not freeze either object.
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* import { success } from "@typed/async-data"
|
|
127
|
+
* const state = success({ id: 1 })
|
|
128
|
+
* ```
|
|
129
|
+
* @category Models
|
|
130
|
+
* @since 1.0.0
|
|
131
|
+
*/
|
|
18
132
|
export interface Success<A> {
|
|
133
|
+
/**
|
|
134
|
+
* The discriminant for a successful value.
|
|
135
|
+
* @remarks
|
|
136
|
+
* ## Why
|
|
137
|
+
* The literal tag enables exhaustive success handling without inspecting the payload.
|
|
138
|
+
* ## Ownership and lifetime
|
|
139
|
+
* Inherits the resource-free lifetime of its enclosing state.
|
|
140
|
+
* @category Models
|
|
141
|
+
* @since 1.0.0
|
|
142
|
+
*/
|
|
19
143
|
readonly _tag: "Success";
|
|
144
|
+
/**
|
|
145
|
+
* The most recently successful value.
|
|
146
|
+
* @remarks
|
|
147
|
+
* ## Why
|
|
148
|
+
* The payload remains directly accessible even when `progress` marks a refresh.
|
|
149
|
+
* ## Ownership and lifetime
|
|
150
|
+
* Inherits the enclosing state's lifetime; the caller retains ownership of the referenced value.
|
|
151
|
+
* @category Models
|
|
152
|
+
* @since 1.0.0
|
|
153
|
+
*/
|
|
20
154
|
readonly value: A;
|
|
155
|
+
/**
|
|
156
|
+
* Optional progress for a refresh of the successful value.
|
|
157
|
+
* @remarks
|
|
158
|
+
* ## Why
|
|
159
|
+
* Its presence distinguishes refreshing success from a settled success.
|
|
160
|
+
* ## Ownership and lifetime
|
|
161
|
+
* Inherits the resource-free lifetime of its enclosing state.
|
|
162
|
+
* @category Models
|
|
163
|
+
* @since 1.0.0
|
|
164
|
+
*/
|
|
21
165
|
readonly progress?: Progress | undefined;
|
|
22
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Represents an Effect failure, optionally while a retry refreshes it.
|
|
169
|
+
* @remarks
|
|
170
|
+
* ## Why
|
|
171
|
+
* Retaining the complete Effect `Cause` preserves typed errors, defects, and interruption instead of flattening failure detail.
|
|
172
|
+
* ## Ownership and lifetime
|
|
173
|
+
* This plain wrapper acquires no resources; its `Cause` is persistent, but the wrapper itself is not frozen.
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* import { failure } from "@typed/async-data"
|
|
177
|
+
* import { Cause } from "effect"
|
|
178
|
+
* const state = failure(Cause.fail("offline"))
|
|
179
|
+
* ```
|
|
180
|
+
* See [Effect Cause](https://effect.website/docs/error-management/cause/).
|
|
181
|
+
* @category Models
|
|
182
|
+
* @since 1.0.0
|
|
183
|
+
*/
|
|
23
184
|
export interface Failure<E> {
|
|
185
|
+
/**
|
|
186
|
+
* The discriminant for a failed computation.
|
|
187
|
+
* @remarks
|
|
188
|
+
* ## Why
|
|
189
|
+
* The literal tag separates failure from absence and loading.
|
|
190
|
+
* ## Ownership and lifetime
|
|
191
|
+
* Inherits the resource-free lifetime of its enclosing state.
|
|
192
|
+
* @category Models
|
|
193
|
+
* @since 1.0.0
|
|
194
|
+
*/
|
|
24
195
|
readonly _tag: "Failure";
|
|
196
|
+
/**
|
|
197
|
+
* The complete Effect cause of failure.
|
|
198
|
+
* @remarks
|
|
199
|
+
* ## Why
|
|
200
|
+
* Cause preservation keeps defects and interruption observable alongside typed failures.
|
|
201
|
+
* ## Ownership and lifetime
|
|
202
|
+
* Inherits the enclosing state's lifetime; Effect Cause is persistent.
|
|
203
|
+
* @category Models
|
|
204
|
+
* @since 1.0.0
|
|
205
|
+
*/
|
|
25
206
|
readonly cause: Cause.Cause<E>;
|
|
207
|
+
/**
|
|
208
|
+
* Optional progress for a retry of the failed operation.
|
|
209
|
+
* @remarks
|
|
210
|
+
* ## Why
|
|
211
|
+
* Its presence distinguishes a refreshing failure from a settled failure.
|
|
212
|
+
* ## Ownership and lifetime
|
|
213
|
+
* Inherits the resource-free lifetime of its enclosing state.
|
|
214
|
+
* @category Models
|
|
215
|
+
* @since 1.0.0
|
|
216
|
+
*/
|
|
26
217
|
readonly progress?: Progress | undefined;
|
|
27
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Represents an optimistic value together with the exact state it replaced.
|
|
221
|
+
* @remarks
|
|
222
|
+
* ## Why
|
|
223
|
+
* Preserving history makes rollback and nested optimistic transformations explicit rather than hiding them in renderer state.
|
|
224
|
+
* ## Ownership and lifetime
|
|
225
|
+
* This plain wrapper acquires no resources and retains its previous state; `readonly` is compile-time only.
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* import { optimistic, success } from "@typed/async-data"
|
|
229
|
+
* const state = optimistic(success(1), 2)
|
|
230
|
+
* ```
|
|
231
|
+
* @category Models
|
|
232
|
+
* @since 1.0.0
|
|
233
|
+
*/
|
|
28
234
|
export interface Optimistic<A, E> {
|
|
235
|
+
/**
|
|
236
|
+
* The discriminant for optimistic state.
|
|
237
|
+
* @remarks
|
|
238
|
+
* ## Why
|
|
239
|
+
* The literal tag enables explicit optimistic handling and cycle-safe traversal.
|
|
240
|
+
* ## Ownership and lifetime
|
|
241
|
+
* Inherits the resource-free lifetime of its enclosing state.
|
|
242
|
+
* @category Models
|
|
243
|
+
* @since 1.0.0
|
|
244
|
+
*/
|
|
29
245
|
readonly _tag: "Optimistic";
|
|
246
|
+
/**
|
|
247
|
+
* The value currently presented optimistically.
|
|
248
|
+
* @remarks
|
|
249
|
+
* ## Why
|
|
250
|
+
* Separating the provisional value from history makes pending intent directly observable.
|
|
251
|
+
* ## Ownership and lifetime
|
|
252
|
+
* Inherits the enclosing state's lifetime; the caller retains ownership of the referenced value.
|
|
253
|
+
* @category Models
|
|
254
|
+
* @since 1.0.0
|
|
255
|
+
*/
|
|
30
256
|
readonly value: A;
|
|
257
|
+
/**
|
|
258
|
+
* The state to restore or inspect beneath this optimistic layer.
|
|
259
|
+
* @remarks
|
|
260
|
+
* ## Why
|
|
261
|
+
* Explicit history supports deterministic rollback and transformation of nested optimistic states.
|
|
262
|
+
* ## Ownership and lifetime
|
|
263
|
+
* Inherits the enclosing optimistic wrapper's resource-free lifetime.
|
|
264
|
+
* @category Models
|
|
265
|
+
* @since 1.0.0
|
|
266
|
+
*/
|
|
31
267
|
readonly previous: AsyncData<A, E>;
|
|
32
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* The complete renderer-independent state machine for asynchronous data.
|
|
271
|
+
* @remarks
|
|
272
|
+
* ## Why
|
|
273
|
+
* A closed union makes absence, work, value, failure, and optimistic history compositional and exhaustively matchable.
|
|
274
|
+
* ## Ownership and lifetime
|
|
275
|
+
* AsyncData values acquire no resources; they describe operation state whose execution is owned elsewhere.
|
|
276
|
+
* @example
|
|
277
|
+
* ```ts
|
|
278
|
+
* import type { AsyncData } from "@typed/async-data"
|
|
279
|
+
* import { success } from "@typed/async-data"
|
|
280
|
+
* const state: AsyncData<number, string> = success(1)
|
|
281
|
+
* ```
|
|
282
|
+
* @category Models
|
|
283
|
+
* @since 1.0.0
|
|
284
|
+
*/
|
|
33
285
|
export type AsyncData<A, E> = NoData | Loading | Success<A> | Failure<E> | Optimistic<A, E>;
|
|
286
|
+
/**
|
|
287
|
+
* A success or failure that retains progress for an active refresh.
|
|
288
|
+
* @remarks
|
|
289
|
+
* ## Why
|
|
290
|
+
* This refinement lets consumers distinguish refreshes without losing the previous result.
|
|
291
|
+
* ## Ownership and lifetime
|
|
292
|
+
* This structural view acquires no resources and has the lifetime of the underlying state value.
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* import type { Refreshing } from "@typed/async-data"
|
|
296
|
+
* const state: Refreshing<number, never> = { _tag: "Success", value: 1, progress: { loaded: 0 } }
|
|
297
|
+
* ```
|
|
298
|
+
* @category Models
|
|
299
|
+
* @since 1.0.0
|
|
300
|
+
*/
|
|
34
301
|
export type Refreshing<A, E> = (Success<A> | Failure<E>) & {
|
|
302
|
+
/** Progress whose presence marks the retained result as actively refreshing. @since 1.0.0 */
|
|
35
303
|
readonly progress: Progress;
|
|
36
304
|
};
|
|
37
|
-
|
|
305
|
+
type EncodedFailure = {
|
|
306
|
+
readonly _tag: "Failure";
|
|
307
|
+
readonly cause: Schema.Json;
|
|
308
|
+
readonly progress?: Progress | undefined;
|
|
309
|
+
};
|
|
310
|
+
type EncodedOptimistic<A, E> = {
|
|
311
|
+
readonly _tag: "Optimistic";
|
|
312
|
+
readonly value: A;
|
|
313
|
+
readonly previous: EncodedAsyncData<A, E>;
|
|
314
|
+
};
|
|
315
|
+
type EncodedAsyncData<A, E> = NoData | Loading | Success<A> | EncodedFailure | EncodedOptimistic<A, E>;
|
|
316
|
+
/**
|
|
317
|
+
* Builds an Effect Schema codec for recursive AsyncData values.
|
|
318
|
+
* @remarks
|
|
319
|
+
* ## Why
|
|
320
|
+
* A codec gives transport boundaries the same state model as application code while encoding `Cause` as JSON and preserving schema service requirements.
|
|
321
|
+
* ## Ownership and lifetime
|
|
322
|
+
* Codec construction is pure and acquires no resources; decoding and encoding use the services declared by the supplied schemas.
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* import { AsyncData } from "@typed/async-data"
|
|
326
|
+
* import { Schema } from "effect"
|
|
327
|
+
* const codec = AsyncData(Schema.String, Schema.String)
|
|
328
|
+
* ```
|
|
329
|
+
* See [Effect Schema](https://effect.website/docs/schema/introduction/).
|
|
330
|
+
* @category Schemas
|
|
331
|
+
* @since 1.0.0
|
|
332
|
+
*/
|
|
333
|
+
export declare const AsyncData: <const A extends Schema.Top, E extends Schema.Top>(A: A, E: E) => Schema.Codec<AsyncData<A["Type"], E["Type"]>, EncodedAsyncData<A["Encoded"], E["Encoded"]>, A["DecodingServices"] | E["DecodingServices"], A["EncodingServices"] | E["EncodingServices"]>;
|
|
334
|
+
/**
|
|
335
|
+
* Tests whether an AsyncData value is `NoData`.
|
|
336
|
+
* @remarks
|
|
337
|
+
* ## Why
|
|
338
|
+
* A named refinement narrows the union without duplicating tag checks.
|
|
339
|
+
* ## Ownership and lifetime
|
|
340
|
+
* This pure predicate acquires no resources and does not retain its argument.
|
|
341
|
+
* @example
|
|
342
|
+
* ```ts
|
|
343
|
+
* import { isNoData, NoData } from "@typed/async-data"
|
|
344
|
+
* isNoData(NoData)
|
|
345
|
+
* ```
|
|
346
|
+
* @category Refinements
|
|
347
|
+
* @since 1.0.0
|
|
348
|
+
*/
|
|
38
349
|
export declare const isNoData: <A, E>(asyncData: AsyncData<A, E>) => asyncData is NoData;
|
|
350
|
+
/**
|
|
351
|
+
* Tests whether an AsyncData value is loading without a prior result.
|
|
352
|
+
* @remarks
|
|
353
|
+
* ## Why
|
|
354
|
+
* This refinement deliberately excludes refreshing success and failure states.
|
|
355
|
+
* ## Ownership and lifetime
|
|
356
|
+
* This pure predicate acquires no resources and does not retain its argument.
|
|
357
|
+
* @example
|
|
358
|
+
* ```ts
|
|
359
|
+
* import { isLoading, loading } from "@typed/async-data"
|
|
360
|
+
* isLoading(loading())
|
|
361
|
+
* ```
|
|
362
|
+
* @category Refinements
|
|
363
|
+
* @since 1.0.0
|
|
364
|
+
*/
|
|
39
365
|
export declare const isLoading: <A, E>(asyncData: AsyncData<A, E>) => asyncData is Loading;
|
|
366
|
+
/**
|
|
367
|
+
* Tests whether an AsyncData value contains a successful value.
|
|
368
|
+
* @remarks
|
|
369
|
+
* ## Why
|
|
370
|
+
* This refinement separates settled or refreshing success from optimistic state.
|
|
371
|
+
* ## Ownership and lifetime
|
|
372
|
+
* This pure predicate acquires no resources and does not retain its argument.
|
|
373
|
+
* @example
|
|
374
|
+
* ```ts
|
|
375
|
+
* import { isSuccess, success } from "@typed/async-data"
|
|
376
|
+
* isSuccess(success(1))
|
|
377
|
+
* ```
|
|
378
|
+
* @category Refinements
|
|
379
|
+
* @since 1.0.0
|
|
380
|
+
*/
|
|
40
381
|
export declare const isSuccess: <A, E>(asyncData: AsyncData<A, E>) => asyncData is Success<A>;
|
|
382
|
+
/**
|
|
383
|
+
* Tests whether an AsyncData value contains an Effect Cause.
|
|
384
|
+
* @remarks
|
|
385
|
+
* ## Why
|
|
386
|
+
* The refinement exposes complete failure information without treating optimistic history as a current failure.
|
|
387
|
+
* ## Ownership and lifetime
|
|
388
|
+
* This pure predicate acquires no resources and does not retain its argument.
|
|
389
|
+
* @example
|
|
390
|
+
* ```ts
|
|
391
|
+
* import { failure, isFailure } from "@typed/async-data"
|
|
392
|
+
* import { Cause } from "effect"
|
|
393
|
+
* isFailure(failure(Cause.fail("offline")))
|
|
394
|
+
* ```
|
|
395
|
+
* @category Refinements
|
|
396
|
+
* @since 1.0.0
|
|
397
|
+
*/
|
|
41
398
|
export declare const isFailure: <A, E>(asyncData: AsyncData<A, E>) => asyncData is Failure<E>;
|
|
399
|
+
/**
|
|
400
|
+
* Tests whether an AsyncData value is an optimistic history node.
|
|
401
|
+
* @remarks
|
|
402
|
+
* ## Why
|
|
403
|
+
* A dedicated refinement makes history traversal explicit and type safe.
|
|
404
|
+
* ## Ownership and lifetime
|
|
405
|
+
* This pure predicate acquires no resources and does not retain its argument.
|
|
406
|
+
* @example
|
|
407
|
+
* ```ts
|
|
408
|
+
* import { isOptimistic, optimistic, success } from "@typed/async-data"
|
|
409
|
+
* isOptimistic(optimistic(success(1), 2))
|
|
410
|
+
* ```
|
|
411
|
+
* @category Refinements
|
|
412
|
+
* @since 1.0.0
|
|
413
|
+
*/
|
|
42
414
|
export declare const isOptimistic: <A, E>(asyncData: AsyncData<A, E>) => asyncData is Optimistic<A, E>;
|
|
415
|
+
/**
|
|
416
|
+
* Validates the runtime structure of an unknown AsyncData value.
|
|
417
|
+
* @remarks
|
|
418
|
+
* ## Why
|
|
419
|
+
* Boundary validation rejects malformed progress, failure causes, and cyclic optimistic histories before application logic depends on them.
|
|
420
|
+
* ## Ownership and lifetime
|
|
421
|
+
* Validation is iterative, acquires no resources, and retains no visited objects after returning.
|
|
422
|
+
* @example
|
|
423
|
+
* ```ts
|
|
424
|
+
* import { isAsyncData } from "@typed/async-data"
|
|
425
|
+
* isAsyncData({ _tag: "Loading", progress: { loaded: 1 } })
|
|
426
|
+
* ```
|
|
427
|
+
* @category Refinements
|
|
428
|
+
* @since 1.0.0
|
|
429
|
+
*/
|
|
43
430
|
export declare const isAsyncData: <A, E>(u: unknown) => u is AsyncData<A, E>;
|
|
431
|
+
/**
|
|
432
|
+
* Tests whether a success or failure carries active refresh progress.
|
|
433
|
+
* @remarks
|
|
434
|
+
* ## Why
|
|
435
|
+
* Refreshing keeps an existing result visible while making new work observable.
|
|
436
|
+
* ## Ownership and lifetime
|
|
437
|
+
* This pure predicate acquires no resources and does not retain its argument.
|
|
438
|
+
* @example
|
|
439
|
+
* ```ts
|
|
440
|
+
* import { isRefreshing, success } from "@typed/async-data"
|
|
441
|
+
* isRefreshing(success("cached", { loaded: 0 }))
|
|
442
|
+
* ```
|
|
443
|
+
* @category Refinements
|
|
444
|
+
* @since 1.0.0
|
|
445
|
+
*/
|
|
44
446
|
export declare const isRefreshing: <A, E>(asyncData: AsyncData<A, E>) => asyncData is Refreshing<A, E>;
|
|
447
|
+
/**
|
|
448
|
+
* Tests whether the base state is loading or refreshing through optimistic history.
|
|
449
|
+
* @remarks
|
|
450
|
+
* ## Why
|
|
451
|
+
* Pending status follows the underlying operation rather than disappearing when optimistic values are layered above it; cycles return `false`.
|
|
452
|
+
*
|
|
453
|
+
* **Known type/runtime discrepancy:** an `Optimistic` wrapper over a pending base returns `true` at runtime, but the existing type predicate narrows to `Loading | Refreshing<A, E>` and excludes `Optimistic`. Do not use this function to narrow before reading `_tag`; use it only as a pending-status boolean and refine the original value separately. The predicate is retained for compatibility and is not an accurate description of every `true` result.
|
|
454
|
+
* ## Ownership and lifetime
|
|
455
|
+
* This iterative predicate acquires no resources and retains no visited objects after returning.
|
|
456
|
+
* @example
|
|
457
|
+
* ```ts
|
|
458
|
+
* import { isPending, loading, optimistic } from "@typed/async-data"
|
|
459
|
+
* isPending(optimistic(loading(), "draft"))
|
|
460
|
+
* ```
|
|
461
|
+
* @category Refinements
|
|
462
|
+
* @since 1.0.0
|
|
463
|
+
*/
|
|
45
464
|
export declare const isPending: <A, E>(asyncData: AsyncData<A, E>) => asyncData is Loading | Refreshing<A, E>;
|
|
465
|
+
/**
|
|
466
|
+
* The canonical shared initial AsyncData object.
|
|
467
|
+
* @remarks
|
|
468
|
+
* ## Why
|
|
469
|
+
* A shared singleton avoids allocating an equivalent empty state and gives callers an exact constructor value.
|
|
470
|
+
* ## Ownership and lifetime
|
|
471
|
+
* The module owns this resource-free singleton for the process lifetime. It is not frozen: mutation through an unsafe cast changes the shared value for every caller.
|
|
472
|
+
* @example
|
|
473
|
+
* ```ts
|
|
474
|
+
* import { NoData } from "@typed/async-data"
|
|
475
|
+
* const initial = NoData
|
|
476
|
+
* ```
|
|
477
|
+
* @category Constructors
|
|
478
|
+
* @since 1.0.0
|
|
479
|
+
*/
|
|
46
480
|
export declare const NoData: NoData;
|
|
481
|
+
/**
|
|
482
|
+
* Creates a loading state with optional progress.
|
|
483
|
+
* @remarks
|
|
484
|
+
* ## Why
|
|
485
|
+
* The constructor keeps state creation consistent with the discriminated union.
|
|
486
|
+
* ## Ownership and lifetime
|
|
487
|
+
* This pure function acquires no resources and returns a new wrapper.
|
|
488
|
+
* @example
|
|
489
|
+
* ```ts
|
|
490
|
+
* import { loading } from "@typed/async-data"
|
|
491
|
+
* const state = loading({ loaded: 2, total: 5 })
|
|
492
|
+
* ```
|
|
493
|
+
* @category Constructors
|
|
494
|
+
* @since 1.0.0
|
|
495
|
+
*/
|
|
47
496
|
export declare const loading: (progress?: Progress) => Loading;
|
|
497
|
+
/**
|
|
498
|
+
* Creates a successful state with optional refresh progress.
|
|
499
|
+
* @remarks
|
|
500
|
+
* ## Why
|
|
501
|
+
* The constructor preserves the payload type while making refresh state explicit.
|
|
502
|
+
* ## Ownership and lifetime
|
|
503
|
+
* This pure function acquires no resources; the returned wrapper retains the supplied value.
|
|
504
|
+
* @example
|
|
505
|
+
* ```ts
|
|
506
|
+
* import { success } from "@typed/async-data"
|
|
507
|
+
* const state = success("ready")
|
|
508
|
+
* ```
|
|
509
|
+
* @category Constructors
|
|
510
|
+
* @since 1.0.0
|
|
511
|
+
*/
|
|
48
512
|
export declare const success: <A>(value: A, progress?: Progress) => Success<A>;
|
|
513
|
+
/**
|
|
514
|
+
* Creates a failed state while preserving the complete Effect Cause.
|
|
515
|
+
* @remarks
|
|
516
|
+
* ## Why
|
|
517
|
+
* Accepting `Cause` prevents typed failures, defects, and interruption from being collapsed into one error value.
|
|
518
|
+
* ## Ownership and lifetime
|
|
519
|
+
* This pure function acquires no resources; the returned wrapper retains the supplied persistent Cause.
|
|
520
|
+
* @example
|
|
521
|
+
* ```ts
|
|
522
|
+
* import { failure } from "@typed/async-data"
|
|
523
|
+
* import { Cause } from "effect"
|
|
524
|
+
* const state = failure(Cause.fail("offline"))
|
|
525
|
+
* ```
|
|
526
|
+
* @category Constructors
|
|
527
|
+
* @since 1.0.0
|
|
528
|
+
*/
|
|
49
529
|
export declare const failure: <E>(cause: Cause.Cause<E>, progress?: Progress) => Failure<E>;
|
|
530
|
+
/**
|
|
531
|
+
* Adds an optimistic value above an existing AsyncData history.
|
|
532
|
+
* @remarks
|
|
533
|
+
* ## Why
|
|
534
|
+
* Keeping `previous` makes rollback and reconciliation explicit rather than mutating or discarding earlier state.
|
|
535
|
+
* ## Ownership and lifetime
|
|
536
|
+
* This pure function acquires no resources; the wrapper retains both supplied values.
|
|
537
|
+
* @example
|
|
538
|
+
* ```ts
|
|
539
|
+
* import { optimistic, success } from "@typed/async-data"
|
|
540
|
+
* const state = optimistic(success("saved"), "saving")
|
|
541
|
+
* ```
|
|
542
|
+
* @category Constructors
|
|
543
|
+
* @since 1.0.0
|
|
544
|
+
*/
|
|
50
545
|
export declare const optimistic: <A, E>(previous: AsyncData<A, E>, value: A) => Optimistic<A, E>;
|
|
546
|
+
/**
|
|
547
|
+
* Starts loading while preserving successful, failed, and optimistic history.
|
|
548
|
+
* @remarks
|
|
549
|
+
* ## Why
|
|
550
|
+
* Refreshes should keep usable values or causes visible, and optimistic layers must remain in their original order. Cyclic history throws `TypeError`.
|
|
551
|
+
* ## Ownership and lifetime
|
|
552
|
+
* This pure transformation acquires no resources and returns new plain wrappers where rebuilding is needed.
|
|
553
|
+
* @example
|
|
554
|
+
* ```ts
|
|
555
|
+
* import { startLoading, success } from "@typed/async-data"
|
|
556
|
+
* const refreshing = startLoading(success("cached"))
|
|
557
|
+
* ```
|
|
558
|
+
* @category Transformations
|
|
559
|
+
* @since 1.0.0
|
|
560
|
+
*/
|
|
51
561
|
export declare const startLoading: <A, E>(data: AsyncData<A, E>, progress?: Progress) => AsyncData<A, E>;
|
|
562
|
+
/**
|
|
563
|
+
* Stops loading or refreshing without discarding optimistic history.
|
|
564
|
+
* @remarks
|
|
565
|
+
* ## Why
|
|
566
|
+
* Removing progress settles success and failure while leaving initial or loading state semantics predictable. Cyclic history throws `TypeError`.
|
|
567
|
+
* ## Ownership and lifetime
|
|
568
|
+
* This pure transformation acquires no resources and returns new plain wrappers where rebuilding is needed.
|
|
569
|
+
* @example
|
|
570
|
+
* ```ts
|
|
571
|
+
* import { startLoading, stopLoading, success } from "@typed/async-data"
|
|
572
|
+
* const settled = stopLoading(startLoading(success("cached")))
|
|
573
|
+
* ```
|
|
574
|
+
* @category Transformations
|
|
575
|
+
* @since 1.0.0
|
|
576
|
+
*/
|
|
52
577
|
export declare const stopLoading: <A, E>(data: AsyncData<A, E>) => AsyncData<A, E>;
|
|
578
|
+
/**
|
|
579
|
+
* Exhaustively folds every AsyncData variant, in data-first or data-last form.
|
|
580
|
+
* @remarks
|
|
581
|
+
* ## Why
|
|
582
|
+
* Centralized exhaustive dispatch exposes values and Causes with their full state while TypeScript unifies branch result types.
|
|
583
|
+
* ## Ownership and lifetime
|
|
584
|
+
* Matching is synchronous, acquires no resources, and retains nothing beyond callback behavior.
|
|
585
|
+
* @example
|
|
586
|
+
* ```ts
|
|
587
|
+
* import { match } from "@typed/async-data"
|
|
588
|
+
* const label = match({ NoData: () => "empty", Loading: () => "loading", Failure: () => "failed", Success: String, Optimistic: String })
|
|
589
|
+
* ```
|
|
590
|
+
* @category Folding
|
|
591
|
+
* @since 1.0.0
|
|
592
|
+
*/
|
|
53
593
|
export declare const match: {
|
|
54
594
|
<A, E, R1, R2, R3, R4, R5>(matchers: {
|
|
55
595
|
NoData: (data: NoData) => R1;
|
|
@@ -66,27 +606,148 @@ export declare const match: {
|
|
|
66
606
|
Optimistic: (value: A, data: Optimistic<A, E>) => R5;
|
|
67
607
|
}): Unify<R1 | R2 | R3 | R4 | R5>;
|
|
68
608
|
};
|
|
609
|
+
/**
|
|
610
|
+
* Returns the current successful or optimistic value as an Effect `Option`.
|
|
611
|
+
* @remarks
|
|
612
|
+
* ## Why
|
|
613
|
+
* `Option` distinguishes an absent value from a present `undefined` payload and composes with Effect's data APIs.
|
|
614
|
+
* ## Ownership and lifetime
|
|
615
|
+
* This pure lookup acquires no resources and does not retain the state.
|
|
616
|
+
* @example
|
|
617
|
+
* ```ts
|
|
618
|
+
* import { getSuccess, success } from "@typed/async-data"
|
|
619
|
+
* const value = getSuccess(success(1))
|
|
620
|
+
* ```
|
|
621
|
+
* See [Effect Option](https://effect.website/docs/data-types/option/).
|
|
622
|
+
* @category Accessors
|
|
623
|
+
* @since 1.0.0
|
|
624
|
+
*/
|
|
69
625
|
export declare function getSuccess<A, E>(data: AsyncData<A, E>): Option.Option<A>;
|
|
70
626
|
/**
|
|
627
|
+
* Returns the complete Cause only when the current state is a Failure.
|
|
628
|
+
* @remarks
|
|
629
|
+
* ## Why
|
|
630
|
+
* The accessor keeps typed errors, defects, and interruption intact for callers that need full failure diagnostics.
|
|
631
|
+
* ## Ownership and lifetime
|
|
632
|
+
* This pure lookup acquires no resources and returns a reference to the persistent Cause.
|
|
633
|
+
* @example
|
|
634
|
+
* ```ts
|
|
635
|
+
* import { failure, getCause } from "@typed/async-data"
|
|
636
|
+
* import { Cause } from "effect"
|
|
637
|
+
* const cause = getCause(failure(Cause.fail("offline")))
|
|
638
|
+
* ```
|
|
639
|
+
* @category Accessors
|
|
71
640
|
* @since 1.0.0
|
|
72
641
|
*/
|
|
73
642
|
export declare function getCause<A, E>(data: AsyncData<A, E>): Option.Option<Cause.Cause<E>>;
|
|
74
643
|
/**
|
|
644
|
+
* Returns the first typed failure found in a Failure Cause.
|
|
645
|
+
* @remarks
|
|
646
|
+
* ## Why
|
|
647
|
+
* This convenience accessor intentionally excludes defects and interruption; use `getCause` when those distinctions matter.
|
|
648
|
+
* ## Ownership and lifetime
|
|
649
|
+
* This pure lookup acquires no resources and does not retain the state.
|
|
650
|
+
* @example
|
|
651
|
+
* ```ts
|
|
652
|
+
* import { failure, getError } from "@typed/async-data"
|
|
653
|
+
* import { Cause } from "effect"
|
|
654
|
+
* const error = getError(failure(Cause.fail("offline")))
|
|
655
|
+
* ```
|
|
656
|
+
* @category Accessors
|
|
75
657
|
* @since 1.0.0
|
|
76
658
|
*/
|
|
77
659
|
export declare function getError<A, E>(data: AsyncData<A, E>): Option.Option<E>;
|
|
660
|
+
/**
|
|
661
|
+
* Maps successful and every optimistic value while preserving state structure.
|
|
662
|
+
* @remarks
|
|
663
|
+
* ## Why
|
|
664
|
+
* Value transformations should not erase progress, Causes, or optimistic rollback history. Cyclic history throws `TypeError`.
|
|
665
|
+
* ## Ownership and lifetime
|
|
666
|
+
* This pure transformation acquires no resources and returns new plain wrappers.
|
|
667
|
+
* @example
|
|
668
|
+
* ```ts
|
|
669
|
+
* import { map, success } from "@typed/async-data"
|
|
670
|
+
* const state = map(success(2), (n) => n * 2)
|
|
671
|
+
* ```
|
|
672
|
+
* @category Transformations
|
|
673
|
+
* @since 1.0.0
|
|
674
|
+
*/
|
|
78
675
|
export declare const map: {
|
|
79
676
|
<A, B>(f: (a: A) => B): <E>(data: AsyncData<A, E>) => AsyncData<B, E>;
|
|
80
677
|
<A, E, B>(data: AsyncData<A, E>, f: (a: A) => B): AsyncData<B, E>;
|
|
81
678
|
};
|
|
679
|
+
/**
|
|
680
|
+
* Replaces a successful or outer optimistic value with another AsyncData value.
|
|
681
|
+
* @remarks
|
|
682
|
+
* ## Why
|
|
683
|
+
* State-producing transformations can change both value and error types while non-value states pass through unchanged.
|
|
684
|
+
* ## Ownership and lifetime
|
|
685
|
+
* This pure transformation acquires no resources; ownership follows the AsyncData returned by the callback.
|
|
686
|
+
* @example
|
|
687
|
+
* ```ts
|
|
688
|
+
* import { flatMap, success } from "@typed/async-data"
|
|
689
|
+
* const parsed = flatMap(success("2"), (text) => success(Number(text)))
|
|
690
|
+
* ```
|
|
691
|
+
* @category Transformations
|
|
692
|
+
* @since 1.0.0
|
|
693
|
+
*/
|
|
82
694
|
export declare const flatMap: {
|
|
83
|
-
<A,
|
|
84
|
-
<A, E, B, E2>(data: AsyncData<A, E>, f: (a: A, data: Success<A> | Optimistic<A, E>) => AsyncData<B,
|
|
695
|
+
<A, B, E2>(f: (a: A, data: Success<A> | Optimistic<A, unknown>) => AsyncData<B, E2>): <E>(data: AsyncData<A, E>) => AsyncData<B, E | E2>;
|
|
696
|
+
<A, E, B, E2>(data: AsyncData<A, E>, f: (a: A, data: Success<A> | Optimistic<A, E>) => AsyncData<B, E2>): AsyncData<B, E | E2>;
|
|
85
697
|
};
|
|
698
|
+
/**
|
|
699
|
+
* Maps typed failures inside the base Cause while preserving defects, interruption, progress, and optimistic history.
|
|
700
|
+
* @remarks
|
|
701
|
+
* ## Why
|
|
702
|
+
* Error adaptation should use Effect Cause semantics instead of flattening the failure channel. Cyclic history throws `TypeError`.
|
|
703
|
+
* ## Ownership and lifetime
|
|
704
|
+
* This pure transformation acquires no resources and returns new plain wrappers.
|
|
705
|
+
* @example
|
|
706
|
+
* ```ts
|
|
707
|
+
* import { failure, mapError } from "@typed/async-data"
|
|
708
|
+
* import { Cause } from "effect"
|
|
709
|
+
* const state = mapError(failure(Cause.fail(404)), String)
|
|
710
|
+
* ```
|
|
711
|
+
* @category Transformations
|
|
712
|
+
* @since 1.0.0
|
|
713
|
+
*/
|
|
86
714
|
export declare const mapError: {
|
|
87
715
|
<A, E, E2>(f: (e: E) => E2): (data: AsyncData<A, E>) => AsyncData<A, E2>;
|
|
88
716
|
<A, E, E2>(data: AsyncData<A, E>, f: (e: E) => E2): AsyncData<A, E2>;
|
|
89
717
|
};
|
|
718
|
+
/**
|
|
719
|
+
* Converts an Effect Exit to Success or Failure without losing its Cause.
|
|
720
|
+
* @remarks
|
|
721
|
+
* ## Why
|
|
722
|
+
* Exit is Effect's complete computation result, so preserving its Cause keeps typed failures, defects, and interruption available.
|
|
723
|
+
* ## Ownership and lifetime
|
|
724
|
+
* This pure conversion acquires no resources and retains the Exit payload or Cause.
|
|
725
|
+
* @example
|
|
726
|
+
* ```ts
|
|
727
|
+
* import { fromExit } from "@typed/async-data"
|
|
728
|
+
* import { Exit } from "effect"
|
|
729
|
+
* const state = fromExit(Exit.succeed(1))
|
|
730
|
+
* ```
|
|
731
|
+
* @category Conversions
|
|
732
|
+
* @since 1.0.0
|
|
733
|
+
*/
|
|
90
734
|
export declare const fromExit: <A, E>(exit: Exit.Exit<A, E>) => AsyncData<A, E>;
|
|
735
|
+
/**
|
|
736
|
+
* Converts an Effect Result to Success or a typed Failure Cause.
|
|
737
|
+
* @remarks
|
|
738
|
+
* ## Why
|
|
739
|
+
* Result has only typed success/failure, so a failed result becomes `Cause.fail` without inventing defects or interruption.
|
|
740
|
+
* ## Ownership and lifetime
|
|
741
|
+
* This pure conversion acquires no resources and retains the Result payload.
|
|
742
|
+
* @example
|
|
743
|
+
* ```ts
|
|
744
|
+
* import { fromResult } from "@typed/async-data"
|
|
745
|
+
* import { Result } from "effect"
|
|
746
|
+
* const state = fromResult(Result.succeed(1))
|
|
747
|
+
* ```
|
|
748
|
+
* @category Conversions
|
|
749
|
+
* @since 1.0.0
|
|
750
|
+
*/
|
|
91
751
|
export declare const fromResult: <A, E>(result: Result.Result<A, E>) => AsyncData<A, E>;
|
|
752
|
+
export {};
|
|
92
753
|
//# sourceMappingURL=index.d.ts.map
|