@typed/async-data 0.7.0 → 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/TypeId.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/cjs/internal/tag.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/AsyncData.ts
CHANGED
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @since 1.0.0
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type { Effect } from "effect"
|
|
9
8
|
import * as Cause from "effect/Cause"
|
|
10
9
|
import * as Data from "effect/Data"
|
|
11
10
|
import * as Duration from "effect/Duration"
|
|
11
|
+
import type * as Effect from "effect/Effect"
|
|
12
12
|
import * as Either from "effect/Either"
|
|
13
13
|
import * as Equal from "effect/Equal"
|
|
14
14
|
import * as Equivalence from "effect/Equivalence"
|
|
@@ -457,17 +457,20 @@ export const flatMap: {
|
|
|
457
457
|
/**
|
|
458
458
|
* @since 1.0.0
|
|
459
459
|
*/
|
|
460
|
-
export const startLoading = <A, E>(
|
|
460
|
+
export const startLoading = <A, E>(
|
|
461
|
+
data: AsyncData<A, E>,
|
|
462
|
+
options?: OptionalPartial<LoadingOptions>
|
|
463
|
+
): AsyncData<A, E> => {
|
|
461
464
|
if (isSuccess(data)) {
|
|
462
|
-
return Option.isSome(data.refreshing) ? data : success(data.value, { ...data, refreshing: loading() })
|
|
465
|
+
return Option.isSome(data.refreshing) ? data : success(data.value, { ...data, refreshing: loading(options) })
|
|
463
466
|
} else if (isFailure(data)) {
|
|
464
467
|
return Option.isSome(data.refreshing)
|
|
465
468
|
? data
|
|
466
|
-
: failCause(data.cause, { ...data, refreshing: loading() })
|
|
469
|
+
: failCause(data.cause, { ...data, refreshing: loading(options) })
|
|
467
470
|
} else if (isOptimistic(data)) {
|
|
468
|
-
return optimistic(startLoading(data.previous), data.value, data)
|
|
471
|
+
return optimistic(startLoading(data.previous, options), data.value, data)
|
|
469
472
|
} else {
|
|
470
|
-
return loading()
|
|
473
|
+
return loading(options)
|
|
471
474
|
}
|
|
472
475
|
}
|
|
473
476
|
|
|
@@ -476,9 +479,9 @@ export const startLoading = <A, E>(data: AsyncData<A, E>): AsyncData<A, E> => {
|
|
|
476
479
|
*/
|
|
477
480
|
export const stopLoading = <A, E>(data: AsyncData<A, E>): AsyncData<A, E> => {
|
|
478
481
|
if (isSuccess(data)) {
|
|
479
|
-
return Option.isSome(data.refreshing) ? success(data.value) : data
|
|
482
|
+
return Option.isSome(data.refreshing) ? success(data.value, { timestamp: data.timestamp }) : data
|
|
480
483
|
} else if (isFailure(data)) {
|
|
481
|
-
return Option.isSome(data.refreshing) ? failCause(data.cause) : data
|
|
484
|
+
return Option.isSome(data.refreshing) ? failCause(data.cause, { timestamp: data.timestamp }) : data
|
|
482
485
|
} else if (isOptimistic(data)) {
|
|
483
486
|
return optimistic(stopLoading(data.previous), data.value, data)
|
|
484
487
|
} else {
|
|
@@ -580,7 +583,7 @@ export const getEquivalence = <A, E>(
|
|
|
580
583
|
/**
|
|
581
584
|
* @since 1.0.0
|
|
582
585
|
*/
|
|
583
|
-
export function fromExit<A, E>(exit: Exit.Exit<A, E>):
|
|
586
|
+
export function fromExit<A, E>(exit: Exit.Exit<A, E>): Success<A> | Failure<E> {
|
|
584
587
|
return Exit.match(exit, {
|
|
585
588
|
onFailure: (cause) => failCause(cause),
|
|
586
589
|
onSuccess: (value) => success(value)
|
|
@@ -590,7 +593,7 @@ export function fromExit<A, E>(exit: Exit.Exit<A, E>): AsyncData<A, E> {
|
|
|
590
593
|
/**
|
|
591
594
|
* @since 1.0.0
|
|
592
595
|
*/
|
|
593
|
-
export function fromEither<A, E = never>(either: Either.Either<A, E>):
|
|
596
|
+
export function fromEither<A, E = never>(either: Either.Either<A, E>): Success<A> | Failure<E> {
|
|
594
597
|
return Either.match(either, {
|
|
595
598
|
onLeft: (e) => fail(e),
|
|
596
599
|
onRight: (a) => success(a)
|
|
@@ -650,3 +653,42 @@ export function dataEqual<A, E>(first: AsyncData<A, E>, second: AsyncData<A, E>)
|
|
|
650
653
|
isOptimistic(second) && Equal.equals(o1.value, second.value) && dataEqual(o1.previous, second.previous)
|
|
651
654
|
})
|
|
652
655
|
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* @since 1.0.0
|
|
659
|
+
*/
|
|
660
|
+
export function toOption<A, E>(data: AsyncData<A, E>): Option.Option<A> {
|
|
661
|
+
return match(data, {
|
|
662
|
+
NoData: Option.none,
|
|
663
|
+
Loading: Option.none,
|
|
664
|
+
Failure: Option.none,
|
|
665
|
+
Success: Option.some,
|
|
666
|
+
Optimistic: Option.some
|
|
667
|
+
})
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* @since 1.0.0
|
|
672
|
+
*/
|
|
673
|
+
export function toOptionCause<A, E>(data: AsyncData<A, E>): Option.Option<Cause.Cause<E>> {
|
|
674
|
+
return match(data, {
|
|
675
|
+
NoData: Option.none,
|
|
676
|
+
Loading: Option.none,
|
|
677
|
+
Failure: Option.some,
|
|
678
|
+
Success: Option.none,
|
|
679
|
+
Optimistic: Option.none
|
|
680
|
+
})
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* @since 1.0.0
|
|
685
|
+
*/
|
|
686
|
+
export function toOptionError<A, E>(data: AsyncData<A, E>): Option.Option<E> {
|
|
687
|
+
return match(data, {
|
|
688
|
+
NoData: Option.none,
|
|
689
|
+
Loading: Option.none,
|
|
690
|
+
Failure: Cause.failureOption,
|
|
691
|
+
Success: Option.none,
|
|
692
|
+
Optimistic: Option.none
|
|
693
|
+
})
|
|
694
|
+
}
|