@typed/async-data 0.3.3 → 0.4.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/TypeId/package.json +6 -0
- package/dist/cjs/AsyncData.js +88 -24
- package/dist/cjs/AsyncData.js.map +1 -1
- package/dist/cjs/Schema.js +308 -143
- package/dist/cjs/Schema.js.map +1 -1
- package/dist/cjs/TypeId.js +14 -0
- package/dist/cjs/TypeId.js.map +1 -0
- package/dist/cjs/internal/async-data.js +47 -55
- package/dist/cjs/internal/async-data.js.map +1 -1
- package/dist/cjs/internal/tag.js +2 -1
- package/dist/cjs/internal/tag.js.map +1 -1
- package/dist/dts/AsyncData.d.ts +52 -9
- package/dist/dts/AsyncData.d.ts.map +1 -1
- package/dist/dts/Schema.d.ts +23 -82
- package/dist/dts/Schema.d.ts.map +1 -1
- package/dist/dts/TypeId.d.ts +12 -0
- package/dist/dts/TypeId.d.ts.map +1 -0
- package/dist/dts/internal/async-data.d.ts +23 -22
- package/dist/dts/internal/async-data.d.ts.map +1 -1
- package/dist/dts/internal/tag.d.ts +1 -0
- package/dist/dts/internal/tag.d.ts.map +1 -1
- package/dist/esm/AsyncData.js +92 -25
- package/dist/esm/AsyncData.js.map +1 -1
- package/dist/esm/Schema.js +294 -142
- package/dist/esm/Schema.js.map +1 -1
- package/dist/esm/TypeId.js +8 -0
- package/dist/esm/TypeId.js.map +1 -0
- package/dist/esm/internal/async-data.js +51 -56
- package/dist/esm/internal/async-data.js.map +1 -1
- package/dist/esm/internal/tag.js +1 -0
- package/dist/esm/internal/tag.js.map +1 -1
- package/package.json +9 -1
- package/src/AsyncData.ts +148 -32
- package/src/Schema.ts +427 -280
- package/src/TypeId.ts +13 -0
- package/src/internal/async-data.ts +65 -54
- package/src/internal/tag.ts +2 -0
package/src/TypeId.ts
ADDED
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
// Internal
|
|
2
2
|
|
|
3
|
-
import * as Cause from "effect/Cause"
|
|
3
|
+
import type * as Cause from "effect/Cause"
|
|
4
4
|
import * as Effect from "effect/Effect"
|
|
5
5
|
import * as Effectable from "effect/Effectable"
|
|
6
6
|
import * as Equal from "effect/Equal"
|
|
7
7
|
import { constant, pipe } from "effect/Function"
|
|
8
8
|
import * as Hash from "effect/Hash"
|
|
9
|
-
import * as Option from "effect/Option"
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
9
|
+
import type * as Option from "effect/Option"
|
|
10
|
+
import { hasProperty } from "effect/Predicate"
|
|
11
|
+
import type { AsyncData, Failure, Loading, Optimistic, Success } from "../AsyncData.js"
|
|
12
|
+
import { AsyncDataTypeId } from "../TypeId.js"
|
|
13
|
+
import { FAILURE_TAG, OPTIMISTIC_TAG, SUCCESS_TAG } from "./tag.js"
|
|
13
14
|
|
|
15
|
+
// @ts-expect-error
|
|
14
16
|
export class FailureImpl<E> extends Effectable.Class<never, E, never> implements Failure<E> {
|
|
15
|
-
readonly
|
|
17
|
+
readonly [AsyncDataTypeId]: AsyncDataTypeId = AsyncDataTypeId
|
|
18
|
+
readonly _tag = FAILURE_TAG
|
|
16
19
|
|
|
17
|
-
commit: () => Effect.Effect<never, E, never
|
|
18
|
-
|
|
19
|
-
[Unify.typeSymbol]!: unknown;
|
|
20
|
-
[Unify.unifySymbol]!: AsyncData.Unify<this>;
|
|
21
|
-
[Unify.ignoreSymbol]!: AsyncData.IgnoreList
|
|
20
|
+
commit: () => Effect.Effect<never, E, never>
|
|
22
21
|
|
|
23
22
|
constructor(readonly cause: Cause.Cause<E>, readonly timestamp: number, readonly refreshing: Option.Option<Loading>) {
|
|
24
23
|
super()
|
|
@@ -26,30 +25,38 @@ export class FailureImpl<E> extends Effectable.Class<never, E, never> implements
|
|
|
26
25
|
this.commit = constant(Effect.failCause(cause))
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
[Equal.symbol]
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
[Equal.symbol](that: unknown) {
|
|
29
|
+
if (this === that) return true
|
|
30
|
+
|
|
31
|
+
if (!isAsyncData(that) || that._tag !== FAILURE_TAG) return false
|
|
32
|
+
|
|
33
|
+
console.log(
|
|
34
|
+
Equal.equals(this.cause, that.cause),
|
|
35
|
+
this.timestamp === that.timestamp,
|
|
36
|
+
Equal.equals(this.refreshing, that.refreshing)
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
return Equal.equals(this.cause, that.cause)
|
|
40
|
+
&& this.timestamp === that.timestamp
|
|
33
41
|
&& Equal.equals(this.refreshing, that.refreshing)
|
|
34
|
-
}
|
|
42
|
+
}
|
|
35
43
|
|
|
36
|
-
[Hash.symbol]
|
|
44
|
+
[Hash.symbol]() {
|
|
37
45
|
return pipe(
|
|
38
46
|
Hash.string(this._tag),
|
|
39
47
|
Hash.combine(Hash.hash(this.cause)),
|
|
48
|
+
Hash.combine(Hash.hash(this.timestamp)),
|
|
40
49
|
Hash.combine(Hash.hash(this.refreshing))
|
|
41
50
|
)
|
|
42
51
|
}
|
|
43
52
|
}
|
|
44
53
|
|
|
54
|
+
// @ts-expect-error
|
|
45
55
|
export class SuccessImpl<A> extends Effectable.Class<never, never, A> implements Success<A> {
|
|
46
|
-
readonly
|
|
56
|
+
readonly [AsyncDataTypeId]: AsyncDataTypeId = AsyncDataTypeId
|
|
57
|
+
readonly _tag = SUCCESS_TAG
|
|
47
58
|
|
|
48
|
-
commit: () => Effect.Effect<never, never, A
|
|
49
|
-
|
|
50
|
-
[Unify.typeSymbol]!: unknown;
|
|
51
|
-
[Unify.unifySymbol]!: AsyncData.Unify<this>;
|
|
52
|
-
[Unify.ignoreSymbol]!: AsyncData.IgnoreList
|
|
59
|
+
commit: () => Effect.Effect<never, never, A>
|
|
53
60
|
|
|
54
61
|
constructor(readonly value: A, readonly timestamp: number, readonly refreshing: Option.Option<Loading>) {
|
|
55
62
|
super()
|
|
@@ -57,53 +64,57 @@ export class SuccessImpl<A> extends Effectable.Class<never, never, A> implements
|
|
|
57
64
|
this.commit = constant(Effect.succeed(value))
|
|
58
65
|
}
|
|
59
66
|
|
|
60
|
-
[Equal.symbol]
|
|
61
|
-
return isAsyncData(that) && that._tag ===
|
|
67
|
+
[Equal.symbol](that: unknown) {
|
|
68
|
+
return isAsyncData(that) && that._tag === SUCCESS_TAG
|
|
62
69
|
&& Equal.equals(this.value, that.value)
|
|
63
70
|
&& Equal.equals(this.timestamp, that.timestamp)
|
|
64
71
|
&& Equal.equals(this.refreshing, that.refreshing)
|
|
65
|
-
}
|
|
72
|
+
}
|
|
66
73
|
|
|
67
|
-
[Hash.symbol]
|
|
74
|
+
[Hash.symbol]() {
|
|
68
75
|
return pipe(
|
|
69
76
|
Hash.string(this._tag),
|
|
70
77
|
Hash.combine(Hash.hash(this.value)),
|
|
78
|
+
Hash.combine(Hash.hash(this.timestamp)),
|
|
71
79
|
Hash.combine(Hash.hash(this.refreshing))
|
|
72
80
|
)
|
|
73
81
|
}
|
|
74
82
|
}
|
|
75
83
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
84
|
+
// @ts-expect-error
|
|
85
|
+
export class OptimisticImpl<E, A> extends Effectable.Class<never, never, A> implements Optimistic<E, A> {
|
|
86
|
+
readonly [AsyncDataTypeId]: AsyncDataTypeId = AsyncDataTypeId
|
|
87
|
+
readonly _tag = OPTIMISTIC_TAG
|
|
81
88
|
|
|
82
|
-
|
|
83
|
-
return Equal.symbol in u && Hash.symbol in u
|
|
84
|
-
}
|
|
89
|
+
commit: () => Effect.Effect<never, never, A>
|
|
85
90
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
91
|
+
constructor(
|
|
92
|
+
readonly value: A,
|
|
93
|
+
readonly timestamp: number,
|
|
94
|
+
readonly previous: AsyncData<E, A>
|
|
95
|
+
) {
|
|
96
|
+
super()
|
|
89
97
|
|
|
90
|
-
|
|
91
|
-
|
|
98
|
+
this.commit = constant(Effect.succeed(value))
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
[Equal.symbol](that: unknown) {
|
|
102
|
+
return isAsyncData(that) && that._tag === OPTIMISTIC_TAG
|
|
103
|
+
&& Equal.equals(this.value, that.value)
|
|
104
|
+
&& Equal.equals(this.timestamp, that.timestamp)
|
|
105
|
+
&& Equal.equals(this.previous, that.previous)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
[Hash.symbol]() {
|
|
109
|
+
return pipe(
|
|
110
|
+
Hash.string(this._tag),
|
|
111
|
+
Hash.combine(Hash.hash(this.value)),
|
|
112
|
+
Hash.combine(Hash.hash(this.timestamp)),
|
|
113
|
+
Hash.combine(Hash.hash(this.previous))
|
|
114
|
+
)
|
|
115
|
+
}
|
|
92
116
|
}
|
|
93
117
|
|
|
94
118
|
export function isAsyncData<E, A>(u: unknown): u is AsyncData<E, A> {
|
|
95
|
-
|
|
96
|
-
switch (u._tag) {
|
|
97
|
-
case NO_DATA_TAG:
|
|
98
|
-
return "timstamp" in u && typeof u.timestamp === "bigint"
|
|
99
|
-
case LOADING_TAG:
|
|
100
|
-
return "progress" in u && Option.isOption(u.progress)
|
|
101
|
-
case FAILURE_TAG:
|
|
102
|
-
return hasDataOptions(u) && "cause" in u && Cause.isCause(u.cause)
|
|
103
|
-
case SUCCESS_TAG:
|
|
104
|
-
return hasDataOptions(u) && "value" in u
|
|
105
|
-
default:
|
|
106
|
-
return false
|
|
107
|
-
}
|
|
108
|
-
} else return false
|
|
119
|
+
return hasProperty(u, AsyncDataTypeId)
|
|
109
120
|
}
|