@typed/async-data 0.7.1 → 0.8.0
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/dist/cjs/AsyncData.js +51 -8
- package/dist/cjs/AsyncData.js.map +1 -1
- package/dist/cjs/Progress.js +3 -3
- package/dist/cjs/Progress.js.map +1 -1
- package/dist/cjs/Schema.js +212 -199
- package/dist/cjs/Schema.js.map +1 -1
- package/dist/cjs/internal/async-data.js +1 -1
- package/dist/cjs/internal/async-data.js.map +1 -1
- package/dist/dts/AsyncData.d.ts +16 -4
- package/dist/dts/AsyncData.d.ts.map +1 -1
- package/dist/dts/Schema.d.ts +4 -4
- package/dist/dts/Schema.d.ts.map +1 -1
- package/dist/esm/AsyncData.js +43 -7
- package/dist/esm/AsyncData.js.map +1 -1
- package/dist/esm/Progress.js +1 -1
- package/dist/esm/Progress.js.map +1 -1
- package/dist/esm/Schema.js +171 -158
- package/dist/esm/Schema.js.map +1 -1
- package/package.json +4 -4
- package/src/AsyncData.ts +52 -10
- package/src/Progress.ts +1 -1
- package/src/Schema.ts +244 -235
package/src/Schema.ts
CHANGED
|
@@ -5,30 +5,31 @@
|
|
|
5
5
|
import * as Arbitrary from "@effect/schema/Arbitrary"
|
|
6
6
|
import * as AST from "@effect/schema/AST"
|
|
7
7
|
import * as Eq from "@effect/schema/Equivalence"
|
|
8
|
-
import * as Parser from "@effect/schema/Parser"
|
|
9
8
|
import * as ParseResult from "@effect/schema/ParseResult"
|
|
10
9
|
import * as Pretty from "@effect/schema/Pretty"
|
|
11
10
|
import * as Schema from "@effect/schema/Schema"
|
|
12
|
-
import * as AsyncData from "@typed/async-data/AsyncData"
|
|
13
|
-
import { Data, Equal, FiberId } from "effect"
|
|
14
11
|
import * as Cause from "effect/Cause"
|
|
12
|
+
import * as Data from "effect/Data"
|
|
15
13
|
import * as Effect from "effect/Effect"
|
|
14
|
+
import * as Equal from "effect/Equal"
|
|
15
|
+
import * as FiberId from "effect/FiberId"
|
|
16
16
|
import * as Option from "effect/Option"
|
|
17
17
|
import { hasProperty } from "effect/Predicate"
|
|
18
|
+
import * as _AsyncData from "./AsyncData.js"
|
|
18
19
|
import * as P from "./Progress.js"
|
|
19
20
|
|
|
20
21
|
const NO_DATA_PRETTY = "AsyncData.NoData"
|
|
21
|
-
const LOADING_PRETTY = (loading:
|
|
22
|
+
const LOADING_PRETTY = (loading: _AsyncData.Loading) =>
|
|
22
23
|
Option.match(loading.progress, {
|
|
23
24
|
onNone: () => `AsyncData.Loading(timestamp=${loading.timestamp})`,
|
|
24
25
|
onSome: (progress) => `AsyncData.Loading(timestamp=${loading.timestamp}, progress=${P.pretty(progress)})`
|
|
25
26
|
})
|
|
26
|
-
const FAILURE_PRETTY = <E>(print: Pretty.Pretty<Cause.Cause<E>>) => (failure:
|
|
27
|
+
const FAILURE_PRETTY = <E>(print: Pretty.Pretty<Cause.Cause<E>>) => (failure: _AsyncData.Failure<E>) =>
|
|
27
28
|
Option.match(failure.refreshing, {
|
|
28
29
|
onNone: () => `AsyncData.Failure(timestamp=${failure.timestamp}, cause=${print(failure.cause)})`,
|
|
29
30
|
onSome: () => `AsyncData.Failure(timestamp=${failure.timestamp}, refreshing=true, cause=${print(failure.cause)})`
|
|
30
31
|
})
|
|
31
|
-
const SUCCESS_PRETTY = <A>(print: Pretty.Pretty<A>) => (success:
|
|
32
|
+
const SUCCESS_PRETTY = <A>(print: Pretty.Pretty<A>) => (success: _AsyncData.Success<A>) =>
|
|
32
33
|
Option.match(success.refreshing, {
|
|
33
34
|
onNone: () => `AsyncData.Success(timestamp=${success.timestamp}, value=${print(success.value)})`,
|
|
34
35
|
onSome: () => `AsyncData.Success(timestamp=${success.timestamp}, refreshing=true, value=${print(success.value)})`
|
|
@@ -36,7 +37,7 @@ const SUCCESS_PRETTY = <A>(print: Pretty.Pretty<A>) => (success: AsyncData.Succe
|
|
|
36
37
|
|
|
37
38
|
const OPTIMISTIC_PRETTY =
|
|
38
39
|
<E, A>(printValue: Pretty.Pretty<A>, printError: Pretty.Pretty<Cause.Cause<E>>) =>
|
|
39
|
-
(optimistic:
|
|
40
|
+
(optimistic: _AsyncData.Optimistic<A, E>) =>
|
|
40
41
|
`AsyncData.Optimistic(timestamp=${optimistic.timestamp}, value=${printValue(optimistic.value)}, previous=${
|
|
41
42
|
asyncDataPretty(printValue, printError)(optimistic.previous)
|
|
42
43
|
})`
|
|
@@ -46,9 +47,9 @@ const OPTIMISTIC_PRETTY =
|
|
|
46
47
|
*/
|
|
47
48
|
export type NoDataFrom = { readonly _tag: "NoData" }
|
|
48
49
|
|
|
49
|
-
const ProgressSchemaJson = Schema.
|
|
50
|
-
loaded: Schema.
|
|
51
|
-
total: Schema.optional(Schema.
|
|
50
|
+
const ProgressSchemaJson = Schema.Struct({
|
|
51
|
+
loaded: Schema.BigInt,
|
|
52
|
+
total: Schema.optional(Schema.BigInt)
|
|
52
53
|
})
|
|
53
54
|
|
|
54
55
|
const ProgressSchema: Schema.Schema<
|
|
@@ -57,12 +58,12 @@ const ProgressSchema: Schema.Schema<
|
|
|
57
58
|
readonly loaded: bigint
|
|
58
59
|
readonly total: Option.Option<bigint>
|
|
59
60
|
}
|
|
60
|
-
> = Schema.
|
|
61
|
-
loaded: Schema.
|
|
62
|
-
total: Schema.
|
|
61
|
+
> = Schema.Data(Schema.Struct({
|
|
62
|
+
loaded: Schema.BigIntFromSelf,
|
|
63
|
+
total: Schema.OptionFromSelf(Schema.BigIntFromSelf)
|
|
63
64
|
}))
|
|
64
65
|
|
|
65
|
-
const progressArbitrary: Arbitrary.
|
|
66
|
+
const progressArbitrary: Arbitrary.LazyArbitrary<P.Progress> = (fc) =>
|
|
66
67
|
fc.bigInt().chain((loaded) => fc.option(fc.bigInt({ min: loaded })).map((total) => P.make(loaded, total)))
|
|
67
68
|
|
|
68
69
|
/**
|
|
@@ -74,16 +75,18 @@ export const Progress: Schema.Schema<
|
|
|
74
75
|
> = ProgressSchemaJson.pipe(
|
|
75
76
|
Schema.transform(
|
|
76
77
|
ProgressSchema,
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
{
|
|
79
|
+
decode: (json): P.Progress => P.make(json.loaded, json.total),
|
|
80
|
+
encode: (progress) => ({
|
|
81
|
+
loaded: progress.loaded,
|
|
82
|
+
total: Option.getOrUndefined(progress.total)
|
|
83
|
+
})
|
|
84
|
+
}
|
|
82
85
|
),
|
|
83
86
|
Schema.annotations({
|
|
84
87
|
[AST.IdentifierAnnotationId]: "Progress",
|
|
85
88
|
[Pretty.PrettyHookId]: () => "Progress",
|
|
86
|
-
[Arbitrary.ArbitraryHookId]: (): Arbitrary.
|
|
89
|
+
[Arbitrary.ArbitraryHookId]: (): Arbitrary.LazyArbitrary<P.Progress> => progressArbitrary,
|
|
87
90
|
[Eq.EquivalenceHookId]: () => Equal.equals
|
|
88
91
|
})
|
|
89
92
|
)
|
|
@@ -96,9 +99,9 @@ export type ProgressFrom = {
|
|
|
96
99
|
readonly total?: string | undefined
|
|
97
100
|
}
|
|
98
101
|
|
|
99
|
-
const loadingArbitrary: Arbitrary.
|
|
102
|
+
const loadingArbitrary: Arbitrary.LazyArbitrary<_AsyncData.Loading> = (fc) =>
|
|
100
103
|
fc.option(progressArbitrary(fc)).map((progress) =>
|
|
101
|
-
|
|
104
|
+
_AsyncData.loading({
|
|
102
105
|
timestamp: Date.now(),
|
|
103
106
|
progress: progress || undefined
|
|
104
107
|
})
|
|
@@ -114,13 +117,13 @@ export type LoadingFrom = {
|
|
|
114
117
|
}
|
|
115
118
|
|
|
116
119
|
const failureArbitrary = <E>(
|
|
117
|
-
cause: Arbitrary.
|
|
118
|
-
): Arbitrary.
|
|
120
|
+
cause: Arbitrary.LazyArbitrary<Cause.Cause<E>>
|
|
121
|
+
): Arbitrary.LazyArbitrary<_AsyncData.Failure<E>> =>
|
|
119
122
|
(fc) =>
|
|
120
123
|
fc.option(loadingArbitrary(fc)).chain((refreshing) =>
|
|
121
124
|
cause(fc).chain((cause) =>
|
|
122
125
|
fc.date().map((date) =>
|
|
123
|
-
|
|
126
|
+
_AsyncData.failCause(cause, {
|
|
124
127
|
timestamp: date.getTime(),
|
|
125
128
|
refreshing: refreshing || undefined
|
|
126
129
|
})
|
|
@@ -133,12 +136,12 @@ const failureArbitrary = <E>(
|
|
|
133
136
|
*/
|
|
134
137
|
export type FailureFrom<E> = {
|
|
135
138
|
readonly _tag: "Failure"
|
|
136
|
-
readonly cause: Schema.
|
|
139
|
+
readonly cause: Schema.CauseEncoded<E>
|
|
137
140
|
readonly timestamp: number
|
|
138
141
|
readonly refreshing?: LoadingFrom | undefined
|
|
139
142
|
}
|
|
140
143
|
|
|
141
|
-
const FailureFrom = <E>(cause: Schema.
|
|
144
|
+
const FailureFrom = <E>(cause: Schema.CauseEncoded<E>, timestamp: number, refreshing?: LoadingFrom): FailureFrom<E> => {
|
|
142
145
|
const base = {
|
|
143
146
|
_tag: "Failure",
|
|
144
147
|
cause,
|
|
@@ -153,13 +156,13 @@ const FailureFrom = <E>(cause: Schema.CauseFrom<E>, timestamp: number, refreshin
|
|
|
153
156
|
}
|
|
154
157
|
|
|
155
158
|
const successArbitrary = <A>(
|
|
156
|
-
value: Arbitrary.
|
|
157
|
-
): Arbitrary.
|
|
159
|
+
value: Arbitrary.LazyArbitrary<A>
|
|
160
|
+
): Arbitrary.LazyArbitrary<_AsyncData.Success<A>> =>
|
|
158
161
|
(fc) =>
|
|
159
162
|
fc.option(loadingArbitrary(fc)).chain((refreshing) =>
|
|
160
163
|
value(fc).chain((a) =>
|
|
161
164
|
fc.date().map((date) =>
|
|
162
|
-
|
|
165
|
+
_AsyncData.success(a, {
|
|
163
166
|
timestamp: date.getTime(),
|
|
164
167
|
refreshing: refreshing || undefined
|
|
165
168
|
})
|
|
@@ -209,13 +212,13 @@ const OptimisticFrom = <A, E>(value: A, timestamp: number, previous: AsyncDataFr
|
|
|
209
212
|
})
|
|
210
213
|
|
|
211
214
|
const optimisticArbitrary = <A, E>(
|
|
212
|
-
valueArb: Arbitrary.
|
|
213
|
-
causeArb: Arbitrary.
|
|
214
|
-
): Arbitrary.
|
|
215
|
+
valueArb: Arbitrary.LazyArbitrary<A>,
|
|
216
|
+
causeArb: Arbitrary.LazyArbitrary<Cause.Cause<E>>
|
|
217
|
+
): Arbitrary.LazyArbitrary<_AsyncData.Optimistic<A, E>> =>
|
|
215
218
|
(fc) =>
|
|
216
219
|
asyncDataArbitrary(valueArb, causeArb)(fc).chain((previous) =>
|
|
217
220
|
valueArb(fc).chain((value) =>
|
|
218
|
-
fc.date().map((date) =>
|
|
221
|
+
fc.date().map((date) => _AsyncData.optimistic(previous, value, { timestamp: date.getTime() }))
|
|
219
222
|
)
|
|
220
223
|
)
|
|
221
224
|
|
|
@@ -291,13 +294,13 @@ function isLoadingFrom(value: unknown): value is LoadingFrom {
|
|
|
291
294
|
&& (hasProperty(value, "progress") ? isProgressFrom(value.progress) : true)
|
|
292
295
|
}
|
|
293
296
|
|
|
294
|
-
const
|
|
297
|
+
const isCauseEncoded = Schema.is(Schema.encodedSchema(Schema.Cause({ defect: Schema.Unknown, error: Schema.Unknown })))
|
|
295
298
|
|
|
296
299
|
function isFailureFrom(value: unknown): value is FailureFrom<any> {
|
|
297
300
|
return hasProperty(value, "_tag")
|
|
298
301
|
&& value._tag === "Failure"
|
|
299
302
|
&& hasProperty(value, "cause")
|
|
300
|
-
&&
|
|
303
|
+
&& isCauseEncoded(value.cause)
|
|
301
304
|
&& hasProperty(value, "timestamp")
|
|
302
305
|
&& typeof value.timestamp === "number"
|
|
303
306
|
&& (hasProperty(value, "refreshing") ? value.refreshing === undefined || isLoadingFrom(value.refreshing) : true)
|
|
@@ -338,86 +341,88 @@ export const asyncDataFromJson = <A, AI, R1, E, EI, R2>(
|
|
|
338
341
|
error: Schema.Schema<E, EI, R2>
|
|
339
342
|
): Schema.Schema<AsyncDataFrom<A, E>, AsyncDataFrom<AI, EI>, R1 | R2> => {
|
|
340
343
|
const schema = Schema.declare(
|
|
341
|
-
[value, Schema.
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
switch (input._tag) {
|
|
360
|
-
case "NoData":
|
|
361
|
-
case "Loading":
|
|
362
|
-
return input
|
|
363
|
-
case "Failure": {
|
|
364
|
-
const cause = yield* _(parseCause(input.cause, options))
|
|
365
|
-
return FailureFrom(cause, input.timestamp, input.refreshing)
|
|
366
|
-
}
|
|
367
|
-
case "Success": {
|
|
368
|
-
const a = yield* _(parseValue(input.value, options))
|
|
369
|
-
return SuccessFrom(a, input.timestamp, input.refreshing)
|
|
370
|
-
}
|
|
371
|
-
case "Optimistic": {
|
|
372
|
-
const previous = yield* _(parseAsyncData(input.previous, options))
|
|
373
|
-
const value = yield* _(parseValue(input.value, options))
|
|
374
|
-
return OptimisticFrom(value, input.timestamp, previous)
|
|
344
|
+
[value, Schema.Cause({ error, defect: Schema.Unknown })],
|
|
345
|
+
{
|
|
346
|
+
decode: (valueSchema, causeSchema) => {
|
|
347
|
+
const parseCause = ParseResult.decode(causeSchema)
|
|
348
|
+
const parseValue = ParseResult.decode(valueSchema)
|
|
349
|
+
|
|
350
|
+
const parseAsyncData = (
|
|
351
|
+
input: unknown,
|
|
352
|
+
options?: AST.ParseOptions | undefined
|
|
353
|
+
): Effect.Effect<
|
|
354
|
+
AsyncDataFrom<A, E>,
|
|
355
|
+
ParseResult.ParseIssue,
|
|
356
|
+
never
|
|
357
|
+
> => {
|
|
358
|
+
return Effect.gen(function*() {
|
|
359
|
+
if (!isAsyncDataFrom<AI, EI>(input)) {
|
|
360
|
+
return yield* Effect.fail<ParseResult.ParseIssue>(new ParseResult.Forbidden(schema.ast, input))
|
|
375
361
|
}
|
|
376
|
-
}
|
|
377
|
-
})
|
|
378
|
-
}
|
|
379
362
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
switch (input._tag) {
|
|
400
|
-
case "NoData":
|
|
401
|
-
case "Loading":
|
|
402
|
-
return input
|
|
403
|
-
case "Failure": {
|
|
404
|
-
const cause = yield* _(parseCause(causeFromToCause(input.cause), options))
|
|
405
|
-
return FailureFrom(cause, input.timestamp, input.refreshing)
|
|
363
|
+
switch (input._tag) {
|
|
364
|
+
case "NoData":
|
|
365
|
+
case "Loading":
|
|
366
|
+
return input
|
|
367
|
+
case "Failure": {
|
|
368
|
+
const cause = yield* parseCause(input.cause, options)
|
|
369
|
+
return FailureFrom(cause, input.timestamp, input.refreshing)
|
|
370
|
+
}
|
|
371
|
+
case "Success": {
|
|
372
|
+
const a = yield* parseValue(input.value, options)
|
|
373
|
+
return SuccessFrom(a, input.timestamp, input.refreshing)
|
|
374
|
+
}
|
|
375
|
+
case "Optimistic": {
|
|
376
|
+
const previous = yield* parseAsyncData(input.previous, options)
|
|
377
|
+
const value = yield* parseValue(input.value, options)
|
|
378
|
+
return OptimisticFrom(value, input.timestamp, previous)
|
|
379
|
+
}
|
|
406
380
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
381
|
+
})
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return parseAsyncData
|
|
385
|
+
},
|
|
386
|
+
encode: (valueSchema, causeSchema) => {
|
|
387
|
+
const parseCause = ParseResult.encode(causeSchema)
|
|
388
|
+
const parseValue = ParseResult.encode(valueSchema)
|
|
389
|
+
|
|
390
|
+
const parseAsyncData = (
|
|
391
|
+
input: unknown,
|
|
392
|
+
options?: AST.ParseOptions
|
|
393
|
+
): Effect.Effect<
|
|
394
|
+
AsyncDataFrom<AI, EI>,
|
|
395
|
+
ParseResult.ParseIssue,
|
|
396
|
+
never
|
|
397
|
+
> => {
|
|
398
|
+
return Effect.gen(function*() {
|
|
399
|
+
if (!isAsyncDataFrom<A, E>(input)) {
|
|
400
|
+
return yield* Effect.fail<ParseResult.ParseIssue>(new ParseResult.Forbidden(schema.ast, input))
|
|
410
401
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
402
|
+
|
|
403
|
+
switch (input._tag) {
|
|
404
|
+
case "NoData":
|
|
405
|
+
case "Loading":
|
|
406
|
+
return input
|
|
407
|
+
case "Failure": {
|
|
408
|
+
const cause = yield* parseCause(causeFromToCause(input.cause), options)
|
|
409
|
+
return FailureFrom(cause, input.timestamp, input.refreshing)
|
|
410
|
+
}
|
|
411
|
+
case "Success": {
|
|
412
|
+
const a = yield* parseValue(input.value, options)
|
|
413
|
+
return SuccessFrom(a, input.timestamp, input.refreshing)
|
|
414
|
+
}
|
|
415
|
+
case "Optimistic": {
|
|
416
|
+
const previous = yield* parseAsyncData(input.previous, options)
|
|
417
|
+
const value = yield* parseValue(input.value, options)
|
|
418
|
+
return OptimisticFrom(value, input.timestamp, previous)
|
|
419
|
+
}
|
|
415
420
|
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
421
|
+
})
|
|
422
|
+
}
|
|
419
423
|
|
|
420
|
-
|
|
424
|
+
return parseAsyncData
|
|
425
|
+
}
|
|
421
426
|
},
|
|
422
427
|
{
|
|
423
428
|
title: "AsyncDataFrom",
|
|
@@ -435,131 +440,135 @@ export const asyncDataFromJson = <A, AI, R1, E, EI, R2>(
|
|
|
435
440
|
/**
|
|
436
441
|
* @since 1.0.0
|
|
437
442
|
*/
|
|
438
|
-
export const
|
|
443
|
+
export const AsyncData = <A, AI, R1, E, EI, R2>(
|
|
439
444
|
valueSchema: Schema.Schema<A, AI, R2>,
|
|
440
445
|
errorSchema: Schema.Schema<E, EI, R1>
|
|
441
|
-
): Schema.Schema<
|
|
446
|
+
): Schema.Schema<_AsyncData.AsyncData<A, E>, AsyncDataFrom<AI, EI>, R1 | R2> => {
|
|
442
447
|
const from = asyncDataFromJson(valueSchema, errorSchema)
|
|
443
|
-
const to =
|
|
448
|
+
const to = AsyncDataFromSelf(Schema.typeSchema(valueSchema), Schema.typeSchema(errorSchema))
|
|
444
449
|
|
|
445
450
|
return from
|
|
446
451
|
.pipe(Schema.transform(
|
|
447
452
|
to,
|
|
448
|
-
|
|
449
|
-
|
|
453
|
+
{
|
|
454
|
+
decode: asyncDataFromToAsyncData,
|
|
455
|
+
encode: asyncDataToAsyncDataFrom
|
|
456
|
+
}
|
|
450
457
|
))
|
|
451
458
|
}
|
|
452
459
|
|
|
453
460
|
/**
|
|
454
461
|
* @since 1.0.0
|
|
455
462
|
*/
|
|
456
|
-
export const
|
|
463
|
+
export const AsyncDataFromSelf = <A, AI, R1, E, EI, R2>(
|
|
457
464
|
value: Schema.Schema<A, AI, R2>,
|
|
458
465
|
error: Schema.Schema<E, EI, R1>
|
|
459
|
-
): Schema.Schema<
|
|
466
|
+
): Schema.Schema<_AsyncData.AsyncData<A, E>, _AsyncData.AsyncData<AI, EI>, R1 | R2> => {
|
|
460
467
|
const schema = Schema.declare(
|
|
461
|
-
[value, Schema.
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
switch (input._tag) {
|
|
480
|
-
case "NoData":
|
|
481
|
-
case "Loading":
|
|
482
|
-
return input
|
|
483
|
-
case "Failure": {
|
|
484
|
-
const cause = yield* _(parseCause(input.cause, options))
|
|
485
|
-
|
|
486
|
-
return AsyncData.failCause(cause, {
|
|
487
|
-
timestamp: input.timestamp,
|
|
488
|
-
refreshing: Option.getOrUndefined(input.refreshing)
|
|
489
|
-
})
|
|
468
|
+
[value, Schema.CauseFromSelf({ error })],
|
|
469
|
+
{
|
|
470
|
+
decode: (valueSchema, causeSchema) => {
|
|
471
|
+
const parseCause = ParseResult.decode(causeSchema)
|
|
472
|
+
const parseValue = ParseResult.decode(valueSchema)
|
|
473
|
+
|
|
474
|
+
const parseAsyncData = (
|
|
475
|
+
input: unknown,
|
|
476
|
+
options?: AST.ParseOptions
|
|
477
|
+
): Effect.Effect<
|
|
478
|
+
_AsyncData.AsyncData<A, E>,
|
|
479
|
+
ParseResult.ParseIssue,
|
|
480
|
+
never
|
|
481
|
+
> => {
|
|
482
|
+
return Effect.gen(function*() {
|
|
483
|
+
if (!_AsyncData.isAsyncData<AI, EI>(input)) {
|
|
484
|
+
return yield* Effect.fail<ParseResult.ParseIssue>(new ParseResult.Forbidden(schema.ast, input))
|
|
490
485
|
}
|
|
491
|
-
case "Success": {
|
|
492
|
-
const a = yield* _(parseValue(input.value, options))
|
|
493
486
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
487
|
+
switch (input._tag) {
|
|
488
|
+
case "NoData":
|
|
489
|
+
case "Loading":
|
|
490
|
+
return input
|
|
491
|
+
case "Failure": {
|
|
492
|
+
const cause = yield* parseCause(input.cause, options)
|
|
493
|
+
|
|
494
|
+
return _AsyncData.failCause(cause, {
|
|
495
|
+
timestamp: input.timestamp,
|
|
496
|
+
refreshing: Option.getOrUndefined(input.refreshing)
|
|
497
|
+
})
|
|
498
|
+
}
|
|
499
|
+
case "Success": {
|
|
500
|
+
const a = yield* parseValue(input.value, options)
|
|
501
|
+
|
|
502
|
+
return _AsyncData.success(a, {
|
|
503
|
+
timestamp: input.timestamp,
|
|
504
|
+
refreshing: Option.getOrUndefined(input.refreshing)
|
|
505
|
+
})
|
|
506
|
+
}
|
|
507
|
+
case "Optimistic": {
|
|
508
|
+
const previous = yield* parseAsyncData(input.previous, options)
|
|
509
|
+
const value = yield* parseValue(input.value, options)
|
|
510
|
+
|
|
511
|
+
return _AsyncData.optimistic(previous, value, {
|
|
512
|
+
timestamp: input.timestamp
|
|
513
|
+
})
|
|
514
|
+
}
|
|
498
515
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
516
|
+
})
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
return parseAsyncData
|
|
520
|
+
},
|
|
521
|
+
encode: (valueSchema, causeSchema) => {
|
|
522
|
+
const parseCause = ParseResult.encode(causeSchema)
|
|
523
|
+
const parseValue = ParseResult.encode(valueSchema)
|
|
524
|
+
|
|
525
|
+
const parseAsyncData = (
|
|
526
|
+
input: unknown,
|
|
527
|
+
options?: AST.ParseOptions
|
|
528
|
+
): Effect.Effect<
|
|
529
|
+
_AsyncData.AsyncData<AI, EI>,
|
|
530
|
+
ParseResult.ParseIssue,
|
|
531
|
+
never
|
|
532
|
+
> => {
|
|
533
|
+
return Effect.gen(function*() {
|
|
534
|
+
if (!_AsyncData.isAsyncData<A, E>(input)) {
|
|
535
|
+
return yield* Effect.fail(new ParseResult.Forbidden(schema.ast, input))
|
|
506
536
|
}
|
|
507
|
-
}
|
|
508
|
-
})
|
|
509
|
-
}
|
|
510
537
|
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
refreshing: Option.getOrUndefined(input.refreshing)
|
|
540
|
-
})
|
|
538
|
+
switch (input._tag) {
|
|
539
|
+
case "NoData":
|
|
540
|
+
case "Loading":
|
|
541
|
+
return input
|
|
542
|
+
case "Failure": {
|
|
543
|
+
const cause = yield* parseCause(input.cause, options)
|
|
544
|
+
|
|
545
|
+
return _AsyncData.failCause(cause, {
|
|
546
|
+
timestamp: input.timestamp,
|
|
547
|
+
refreshing: Option.getOrUndefined(input.refreshing)
|
|
548
|
+
})
|
|
549
|
+
}
|
|
550
|
+
case "Success": {
|
|
551
|
+
const a = yield* parseValue(input.value, options)
|
|
552
|
+
|
|
553
|
+
return _AsyncData.success(a, {
|
|
554
|
+
timestamp: input.timestamp,
|
|
555
|
+
refreshing: Option.getOrUndefined(input.refreshing)
|
|
556
|
+
})
|
|
557
|
+
}
|
|
558
|
+
case "Optimistic": {
|
|
559
|
+
const previous = yield* parseAsyncData(input.previous, options)
|
|
560
|
+
const value = yield* parseValue(input.value, options)
|
|
561
|
+
|
|
562
|
+
return _AsyncData.optimistic(previous, value, {
|
|
563
|
+
timestamp: input.timestamp
|
|
564
|
+
})
|
|
565
|
+
}
|
|
541
566
|
}
|
|
542
|
-
|
|
543
|
-
|
|
567
|
+
})
|
|
568
|
+
}
|
|
544
569
|
|
|
545
|
-
|
|
546
|
-
timestamp: input.timestamp,
|
|
547
|
-
refreshing: Option.getOrUndefined(input.refreshing)
|
|
548
|
-
})
|
|
549
|
-
}
|
|
550
|
-
case "Optimistic": {
|
|
551
|
-
const previous = yield* _(parseAsyncData(input.previous, options))
|
|
552
|
-
const value = yield* _(parseValue(input.value, options))
|
|
553
|
-
|
|
554
|
-
return AsyncData.optimistic(previous, value, {
|
|
555
|
-
timestamp: input.timestamp
|
|
556
|
-
})
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
})
|
|
570
|
+
return parseAsyncData
|
|
560
571
|
}
|
|
561
|
-
|
|
562
|
-
return parseAsyncData
|
|
563
572
|
},
|
|
564
573
|
{
|
|
565
574
|
title: "AsyncData",
|
|
@@ -568,14 +577,14 @@ export const asyncDataFromSelf = <A, AI, R1, E, EI, R2>(
|
|
|
568
577
|
equivalence: () => Equal.equals
|
|
569
578
|
}
|
|
570
579
|
)
|
|
571
|
-
return schema
|
|
580
|
+
return schema as any
|
|
572
581
|
}
|
|
573
582
|
|
|
574
583
|
function asyncDataPretty<A, E>(
|
|
575
584
|
A: Pretty.Pretty<A>,
|
|
576
585
|
E: Pretty.Pretty<Cause.Cause<E>>
|
|
577
|
-
): Pretty.Pretty<
|
|
578
|
-
return
|
|
586
|
+
): Pretty.Pretty<_AsyncData.AsyncData<A, E>> {
|
|
587
|
+
return _AsyncData.match({
|
|
579
588
|
NoData: () => NO_DATA_PRETTY,
|
|
580
589
|
Loading: LOADING_PRETTY,
|
|
581
590
|
Failure: (_, data) => FAILURE_PRETTY(E)(data),
|
|
@@ -585,17 +594,17 @@ function asyncDataPretty<A, E>(
|
|
|
585
594
|
}
|
|
586
595
|
|
|
587
596
|
function asyncDataArbitrary<A, E>(
|
|
588
|
-
A: Arbitrary.
|
|
589
|
-
E: Arbitrary.
|
|
590
|
-
): Arbitrary.
|
|
597
|
+
A: Arbitrary.LazyArbitrary<A>,
|
|
598
|
+
E: Arbitrary.LazyArbitrary<Cause.Cause<E>>
|
|
599
|
+
): Arbitrary.LazyArbitrary<_AsyncData.AsyncData<A, E>> {
|
|
591
600
|
const failureArb = failureArbitrary(E)
|
|
592
601
|
const successArb = successArbitrary(A)
|
|
593
602
|
const optimisticArb = optimisticArbitrary(A, E)
|
|
594
603
|
|
|
595
604
|
return (fc) =>
|
|
596
605
|
fc.oneof(
|
|
597
|
-
fc.constant(
|
|
598
|
-
fc.constant(
|
|
606
|
+
fc.constant(_AsyncData.noData()),
|
|
607
|
+
fc.constant(_AsyncData.loading()),
|
|
599
608
|
failureArb(fc),
|
|
600
609
|
successArb(fc),
|
|
601
610
|
optimisticArb(fc)
|
|
@@ -615,15 +624,15 @@ function progressToJson(progres: Option.Option<P.Progress>): ProgressFrom | unde
|
|
|
615
624
|
}
|
|
616
625
|
}
|
|
617
626
|
|
|
618
|
-
function loadingFromJson(json: LoadingFrom | undefined):
|
|
627
|
+
function loadingFromJson(json: LoadingFrom | undefined): _AsyncData.Loading | undefined {
|
|
619
628
|
if (json === undefined) return
|
|
620
|
-
return
|
|
629
|
+
return _AsyncData.loading({
|
|
621
630
|
timestamp: json.timestamp,
|
|
622
631
|
progress: Option.getOrUndefined(progressFromJson(json.progress))
|
|
623
632
|
})
|
|
624
633
|
}
|
|
625
634
|
|
|
626
|
-
function loadingToJson(loading:
|
|
635
|
+
function loadingToJson(loading: _AsyncData.Loading): LoadingFrom {
|
|
627
636
|
const from: LoadingFrom = {
|
|
628
637
|
_tag: "Loading",
|
|
629
638
|
timestamp: loading.timestamp
|
|
@@ -636,7 +645,7 @@ function loadingToJson(loading: AsyncData.Loading): LoadingFrom {
|
|
|
636
645
|
return from
|
|
637
646
|
}
|
|
638
647
|
|
|
639
|
-
function causeFromToCause<E>(from: Schema.
|
|
648
|
+
function causeFromToCause<E>(from: Schema.CauseEncoded<E>): Cause.Cause<E> {
|
|
640
649
|
switch (from._tag) {
|
|
641
650
|
case "Die":
|
|
642
651
|
return Cause.die(from.defect)
|
|
@@ -653,7 +662,7 @@ function causeFromToCause<E>(from: Schema.CauseFrom<E>): Cause.Cause<E> {
|
|
|
653
662
|
}
|
|
654
663
|
}
|
|
655
664
|
|
|
656
|
-
function fiberIdFromToFiberId(id: Schema.
|
|
665
|
+
function fiberIdFromToFiberId(id: Schema.FiberIdEncoded): FiberId.FiberId {
|
|
657
666
|
switch (id._tag) {
|
|
658
667
|
case "None":
|
|
659
668
|
return FiberId.none
|
|
@@ -664,7 +673,7 @@ function fiberIdFromToFiberId(id: Schema.FiberIdFrom): FiberId.FiberId {
|
|
|
664
673
|
}
|
|
665
674
|
}
|
|
666
675
|
|
|
667
|
-
function
|
|
676
|
+
function causeToCauseEncoded<E>(cause: Cause.Cause<E>): Schema.CauseEncoded<E> {
|
|
668
677
|
switch (cause._tag) {
|
|
669
678
|
case "Die":
|
|
670
679
|
return { _tag: "Die", defect: cause.defect }
|
|
@@ -675,13 +684,13 @@ function causeToCauseFrom<E>(cause: Cause.Cause<E>): Schema.CauseFrom<E> {
|
|
|
675
684
|
case "Interrupt":
|
|
676
685
|
return { _tag: "Interrupt", fiberId: fiberIdToFiberIdFrom(cause.fiberId) }
|
|
677
686
|
case "Parallel":
|
|
678
|
-
return { _tag: "Parallel", left:
|
|
687
|
+
return { _tag: "Parallel", left: causeToCauseEncoded(cause.left), right: causeToCauseEncoded(cause.right) }
|
|
679
688
|
case "Sequential":
|
|
680
|
-
return { _tag: "Sequential", left:
|
|
689
|
+
return { _tag: "Sequential", left: causeToCauseEncoded(cause.left), right: causeToCauseEncoded(cause.right) }
|
|
681
690
|
}
|
|
682
691
|
}
|
|
683
692
|
|
|
684
|
-
function fiberIdToFiberIdFrom(id: FiberId.FiberId): Schema.
|
|
693
|
+
function fiberIdToFiberIdFrom(id: FiberId.FiberId): Schema.FiberIdEncoded {
|
|
685
694
|
switch (id._tag) {
|
|
686
695
|
case "None":
|
|
687
696
|
return { _tag: "None" }
|
|
@@ -694,7 +703,7 @@ function fiberIdToFiberIdFrom(id: FiberId.FiberId): Schema.FiberIdFrom {
|
|
|
694
703
|
|
|
695
704
|
const NO_DATA_FROM: NoDataFrom = { _tag: "NoData" } as const
|
|
696
705
|
|
|
697
|
-
function asyncDataToAsyncDataFrom<A, E>(data:
|
|
706
|
+
function asyncDataToAsyncDataFrom<A, E>(data: _AsyncData.AsyncData<A, E>): AsyncDataFrom<A, E> {
|
|
698
707
|
switch (data._tag) {
|
|
699
708
|
case "NoData":
|
|
700
709
|
return NO_DATA_FROM
|
|
@@ -702,7 +711,7 @@ function asyncDataToAsyncDataFrom<A, E>(data: AsyncData.AsyncData<A, E>): AsyncD
|
|
|
702
711
|
return loadingToJson(data)
|
|
703
712
|
case "Failure":
|
|
704
713
|
return FailureFrom(
|
|
705
|
-
|
|
714
|
+
causeToCauseEncoded(data.cause),
|
|
706
715
|
data.timestamp,
|
|
707
716
|
Option.getOrUndefined(Option.map(data.refreshing, loadingToJson))
|
|
708
717
|
)
|
|
@@ -717,24 +726,24 @@ function asyncDataToAsyncDataFrom<A, E>(data: AsyncData.AsyncData<A, E>): AsyncD
|
|
|
717
726
|
}
|
|
718
727
|
}
|
|
719
728
|
|
|
720
|
-
function asyncDataFromToAsyncData<A, E>(data: AsyncDataFrom<A, E>):
|
|
729
|
+
function asyncDataFromToAsyncData<A, E>(data: AsyncDataFrom<A, E>): _AsyncData.AsyncData<A, E> {
|
|
721
730
|
switch (data._tag) {
|
|
722
731
|
case "NoData":
|
|
723
|
-
return
|
|
732
|
+
return _AsyncData.noData()
|
|
724
733
|
case "Loading":
|
|
725
734
|
return loadingFromJson(data)!
|
|
726
735
|
case "Failure":
|
|
727
|
-
return
|
|
736
|
+
return _AsyncData.failCause(causeFromToCause(data.cause), {
|
|
728
737
|
timestamp: data.timestamp,
|
|
729
738
|
refreshing: loadingFromJson(data.refreshing)
|
|
730
739
|
})
|
|
731
740
|
case "Success":
|
|
732
|
-
return
|
|
741
|
+
return _AsyncData.success(data.value, {
|
|
733
742
|
timestamp: data.timestamp,
|
|
734
743
|
refreshing: loadingFromJson(data.refreshing)
|
|
735
744
|
})
|
|
736
745
|
case "Optimistic":
|
|
737
|
-
return
|
|
746
|
+
return _AsyncData.optimistic(asyncDataFromToAsyncData(data.previous), data.value, {
|
|
738
747
|
timestamp: data.timestamp
|
|
739
748
|
})
|
|
740
749
|
}
|