@typed/async-data 0.1.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.
Files changed (39) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -0
  3. package/dist/cjs/AsyncData.js +225 -0
  4. package/dist/cjs/AsyncData.js.map +1 -0
  5. package/dist/cjs/Progress.js +47 -0
  6. package/dist/cjs/Progress.js.map +1 -0
  7. package/dist/cjs/Schema.js +104 -0
  8. package/dist/cjs/Schema.js.map +1 -0
  9. package/dist/cjs/internal/async-data.js +91 -0
  10. package/dist/cjs/internal/async-data.js.map +1 -0
  11. package/dist/cjs/internal/tag.js +11 -0
  12. package/dist/cjs/internal/tag.js.map +1 -0
  13. package/dist/dts/AsyncData.d.ts +288 -0
  14. package/dist/dts/AsyncData.d.ts.map +1 -0
  15. package/dist/dts/Progress.d.ts +38 -0
  16. package/dist/dts/Progress.d.ts.map +1 -0
  17. package/dist/dts/Schema.d.ts +15 -0
  18. package/dist/dts/Schema.d.ts.map +1 -0
  19. package/dist/dts/internal/async-data.d.ts +34 -0
  20. package/dist/dts/internal/async-data.d.ts.map +1 -0
  21. package/dist/dts/internal/tag.d.ts +5 -0
  22. package/dist/dts/internal/tag.d.ts.map +1 -0
  23. package/dist/esm/AsyncData.js +203 -0
  24. package/dist/esm/AsyncData.js.map +1 -0
  25. package/dist/esm/Progress.js +38 -0
  26. package/dist/esm/Progress.js.map +1 -0
  27. package/dist/esm/Schema.js +93 -0
  28. package/dist/esm/Schema.js.map +1 -0
  29. package/dist/esm/internal/async-data.js +85 -0
  30. package/dist/esm/internal/async-data.js.map +1 -0
  31. package/dist/esm/internal/tag.js +5 -0
  32. package/dist/esm/internal/tag.js.map +1 -0
  33. package/dist/esm/package.json +4 -0
  34. package/package.json +48 -0
  35. package/src/AsyncData.ts +434 -0
  36. package/src/Progress.ts +65 -0
  37. package/src/Schema.ts +166 -0
  38. package/src/internal/async-data.ts +101 -0
  39. package/src/internal/tag.ts +7 -0
@@ -0,0 +1,101 @@
1
+ // Internal
2
+
3
+ import { Cause, Effect, Effectable, Equal, Hash, Option, pipe, Unify } from "effect"
4
+ import { constant } from "effect/Function"
5
+ import { type AsyncData, type Failure, type Loading, type Success } from "../AsyncData"
6
+ import { FAILURE_TAG, LOADING_TAG, NO_DATA_TAG, SUCCESS_TAG } from "./tag"
7
+
8
+ export class FailureImpl<E> extends Effectable.Class<never, E, never> implements Failure<E> {
9
+ readonly _tag = "Failure"
10
+
11
+ commit: () => Effect.Effect<never, E, never>;
12
+
13
+ [Unify.typeSymbol]!: unknown;
14
+ [Unify.unifySymbol]!: AsyncData.Unify<this>;
15
+ [Unify.ignoreSymbol]!: AsyncData.IgnoreList
16
+
17
+ constructor(readonly cause: Cause.Cause<E>, readonly refreshing: Option.Option<Loading>) {
18
+ super()
19
+
20
+ this.commit = constant(Effect.failCause(cause))
21
+ }
22
+
23
+ [Equal.symbol] = (that: unknown) => {
24
+ return isAsyncData(that) && that._tag === "Failure"
25
+ && Equal.equals(this.cause, that.cause)
26
+ && Equal.equals(this.refreshing, that.refreshing)
27
+ };
28
+
29
+ [Hash.symbol] = () => {
30
+ return pipe(
31
+ Hash.string(this._tag),
32
+ Hash.combine(Hash.hash(this.cause)),
33
+ Hash.combine(Hash.hash(this.refreshing))
34
+ )
35
+ }
36
+ }
37
+
38
+ export class SuccessImpl<A> extends Effectable.Class<never, never, A> implements Success<A> {
39
+ readonly _tag = "Success"
40
+
41
+ commit: () => Effect.Effect<never, never, A>;
42
+
43
+ [Unify.typeSymbol]!: unknown;
44
+ [Unify.unifySymbol]!: AsyncData.Unify<this>;
45
+ [Unify.ignoreSymbol]!: AsyncData.IgnoreList
46
+
47
+ constructor(readonly value: A, readonly refreshing: Option.Option<Loading>) {
48
+ super()
49
+
50
+ this.commit = constant(Effect.succeed(value))
51
+ }
52
+
53
+ [Equal.symbol] = (that: unknown) => {
54
+ return isAsyncData(that) && that._tag === "Success"
55
+ && Equal.equals(this.value, that.value)
56
+ && Equal.equals(this.refreshing, that.refreshing)
57
+ };
58
+
59
+ [Hash.symbol] = () => {
60
+ return pipe(
61
+ Hash.string(this._tag),
62
+ Hash.combine(Hash.hash(this.value)),
63
+ Hash.combine(Hash.hash(this.refreshing))
64
+ )
65
+ }
66
+ }
67
+
68
+ export function hasDataOptions(u: Record<PropertyKey, unknown>): boolean {
69
+ if ("timestamp" in u && "refreshing" in u) {
70
+ return typeof u.timestamp === "bigint" && Option.isOption(u.refreshing)
71
+ } else return false
72
+ }
73
+
74
+ export function hasEquality(u: Record<PropertyKey, unknown>): boolean {
75
+ return Equal.symbol in u && Hash.symbol in u
76
+ }
77
+
78
+ export function isTaggedRecord(u: unknown): u is Record<PropertyKey, unknown> & { readonly _tag: unknown } {
79
+ return isRecord(u) && "_tag" in u
80
+ }
81
+
82
+ export function isRecord(u: unknown): u is Record<PropertyKey, unknown> {
83
+ return typeof u === "object" && u !== null && !Array.isArray(u)
84
+ }
85
+
86
+ export function isAsyncData<E, A>(u: unknown): u is AsyncData<E, A> {
87
+ if (isTaggedRecord(u) && hasEquality(u)) {
88
+ switch (u._tag) {
89
+ case NO_DATA_TAG:
90
+ return "timstamp" in u && typeof u.timestamp === "bigint"
91
+ case LOADING_TAG:
92
+ return "progress" in u && Option.isOption(u.progress)
93
+ case FAILURE_TAG:
94
+ return hasDataOptions(u) && "cause" in u && Cause.isCause(u.cause)
95
+ case SUCCESS_TAG:
96
+ return hasDataOptions(u) && "value" in u
97
+ default:
98
+ return false
99
+ }
100
+ } else return false
101
+ }
@@ -0,0 +1,7 @@
1
+ export const NO_DATA_TAG = "NoData" as const
2
+
3
+ export const LOADING_TAG = "Loading" as const
4
+
5
+ export const FAILURE_TAG = "Failure" as const
6
+
7
+ export const SUCCESS_TAG = "Success" as const