@typed/async-data 0.12.0 → 0.13.1

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 (75) hide show
  1. package/.nvmrc +1 -0
  2. package/biome.json +36 -0
  3. package/dist/AsyncData.d.ts +203 -0
  4. package/dist/AsyncData.js +294 -0
  5. package/dist/AsyncData.js.map +1 -0
  6. package/dist/LazyRef.d.ts +22 -0
  7. package/dist/LazyRef.js +28 -0
  8. package/dist/LazyRef.js.map +1 -0
  9. package/dist/Progress.d.ts +25 -0
  10. package/dist/Progress.js +22 -0
  11. package/dist/Progress.js.map +1 -0
  12. package/dist/{dts/TypeId.d.ts → TypeId.d.ts} +0 -1
  13. package/dist/TypeId.js +8 -0
  14. package/dist/TypeId.js.map +1 -0
  15. package/dist/_internal.d.ts +20 -0
  16. package/dist/_internal.js +58 -0
  17. package/dist/_internal.js.map +1 -0
  18. package/dist/index.d.ts +4 -0
  19. package/dist/index.js +5 -0
  20. package/dist/index.js.map +1 -0
  21. package/package.json +31 -49
  22. package/readme.md +218 -0
  23. package/src/AsyncData.test.ts +89 -0
  24. package/src/AsyncData.ts +552 -595
  25. package/src/LazyRef.ts +89 -0
  26. package/src/Progress.ts +20 -66
  27. package/src/TypeId.ts +1 -1
  28. package/src/_internal.ts +111 -0
  29. package/src/index.ts +4 -0
  30. package/tsconfig.json +27 -0
  31. package/AsyncData/package.json +0 -6
  32. package/LICENSE +0 -21
  33. package/Progress/package.json +0 -6
  34. package/README.md +0 -5
  35. package/Schema/package.json +0 -6
  36. package/TypeId/package.json +0 -6
  37. package/dist/cjs/AsyncData.js +0 -399
  38. package/dist/cjs/AsyncData.js.map +0 -1
  39. package/dist/cjs/Progress.js +0 -62
  40. package/dist/cjs/Progress.js.map +0 -1
  41. package/dist/cjs/Schema.js +0 -539
  42. package/dist/cjs/Schema.js.map +0 -1
  43. package/dist/cjs/TypeId.js +0 -14
  44. package/dist/cjs/TypeId.js.map +0 -1
  45. package/dist/cjs/internal/async-data.js +0 -94
  46. package/dist/cjs/internal/async-data.js.map +0 -1
  47. package/dist/cjs/internal/tag.js +0 -12
  48. package/dist/cjs/internal/tag.js.map +0 -1
  49. package/dist/dts/AsyncData.d.ts +0 -386
  50. package/dist/dts/AsyncData.d.ts.map +0 -1
  51. package/dist/dts/Progress.d.ts +0 -43
  52. package/dist/dts/Progress.d.ts.map +0 -1
  53. package/dist/dts/Schema.d.ts +0 -78
  54. package/dist/dts/Schema.d.ts.map +0 -1
  55. package/dist/dts/TypeId.d.ts.map +0 -1
  56. package/dist/dts/internal/async-data.d.ts +0 -43
  57. package/dist/dts/internal/async-data.d.ts.map +0 -1
  58. package/dist/dts/internal/tag.d.ts +0 -6
  59. package/dist/dts/internal/tag.d.ts.map +0 -1
  60. package/dist/esm/AsyncData.js +0 -372
  61. package/dist/esm/AsyncData.js.map +0 -1
  62. package/dist/esm/Progress.js +0 -49
  63. package/dist/esm/Progress.js.map +0 -1
  64. package/dist/esm/Schema.js +0 -500
  65. package/dist/esm/Schema.js.map +0 -1
  66. package/dist/esm/TypeId.js +0 -8
  67. package/dist/esm/TypeId.js.map +0 -1
  68. package/dist/esm/internal/async-data.js +0 -91
  69. package/dist/esm/internal/async-data.js.map +0 -1
  70. package/dist/esm/internal/tag.js +0 -6
  71. package/dist/esm/internal/tag.js.map +0 -1
  72. package/dist/esm/package.json +0 -4
  73. package/src/Schema.ts +0 -754
  74. package/src/internal/async-data.ts +0 -114
  75. package/src/internal/tag.ts +0 -9
package/src/LazyRef.ts ADDED
@@ -0,0 +1,89 @@
1
+ import * as L from '@typed/lazy-ref'
2
+ import * as Effect from 'effect/Effect'
3
+ import { dual } from 'effect/Function'
4
+ import * as Layer from 'effect/Layer'
5
+ import type { Scope } from 'effect/Scope'
6
+ import * as AsyncData from './AsyncData.js'
7
+
8
+ export interface LazyRef<A, E = never, R = never>
9
+ extends L.LazyRef<AsyncData.AsyncData<A, E>, never, R> {}
10
+
11
+ export type LazyRefOptions<A, E = never> = L.LazyRefOptions<AsyncData.AsyncData<A, E>> & {
12
+ readonly drop?: number
13
+ readonly take?: number
14
+ }
15
+
16
+ export interface Tagged<Self, Id extends string, A, E = never>
17
+ extends L.TaggedClass<Self, Id, AsyncData.AsyncData<A, E>, never> {
18
+ // Layers
19
+
20
+ readonly Default: Layer.Layer<Self>
21
+ readonly fromEffect: <R>(
22
+ effect: Effect.Effect<A, E, R>,
23
+ ) => Layer.Layer<Self, never, Exclude<R, Scope>>
24
+
25
+ // Effects
26
+ readonly run: <R>(
27
+ effect: Effect.Effect<A, E, R>,
28
+ ) => Effect.Effect<AsyncData.AsyncData<A, E>, never, R | Self>
29
+ }
30
+
31
+ export function Tag<const Id extends string>(id: Id) {
32
+ return <Self, A, E = never>(
33
+ options?: L.LazyRefOptions<AsyncData.AsyncData<A, E>>,
34
+ ): Tagged<Self, Id, A, E> =>
35
+ class extends L.Tag(id)<Self, AsyncData.AsyncData<A, E>>() {
36
+ static readonly Default = this.make(Effect.sync(AsyncData.noData<A, E>), options)
37
+
38
+ static readonly fromEffect = <R>(effect: Effect.Effect<A, E, R>) =>
39
+ Layer.scoped(
40
+ this.tag,
41
+ Effect.gen(function* () {
42
+ const ref = yield* lazyRef(options)
43
+ yield* runEffect(ref, effect).pipe(Effect.forkScoped, Effect.interruptible)
44
+ return ref
45
+ }),
46
+ )
47
+
48
+ static readonly run = <R>(effect: Effect.Effect<A, E, R>) => runEffect(this, effect)
49
+ }
50
+ }
51
+
52
+ export function lazyRef<A, E = never>(
53
+ options?: L.LazyRefOptions<AsyncData.AsyncData<A, E>>,
54
+ ): Effect.Effect<LazyRef<A, E>, never, Scope> {
55
+ return L.sync<AsyncData.AsyncData<A, E>>(AsyncData.noData<A, E>, options)
56
+ }
57
+
58
+ export const runEffect: {
59
+ <A, E, R2>(
60
+ effect: Effect.Effect<A, E, R2>,
61
+ ): <R>(
62
+ lazyRef: L.LazyRef<AsyncData.AsyncData<A, E>, never, R>,
63
+ ) => Effect.Effect<AsyncData.AsyncData<A, E>, never, R | R2>
64
+ <A, E, R, R2>(
65
+ lazyRef: L.LazyRef<AsyncData.AsyncData<A, E>, never, R>,
66
+ effect: Effect.Effect<A, E, R2>,
67
+ ): Effect.Effect<AsyncData.AsyncData<A, E>, never, R | R2>
68
+ } = dual(2, function runEffect<
69
+ A,
70
+ E,
71
+ R,
72
+ R2,
73
+ >(lazyRef: L.LazyRef<AsyncData.AsyncData<A, E>, never, R>, effect: Effect.Effect<A, E, R2>): Effect.Effect<
74
+ AsyncData.AsyncData<A, E>,
75
+ never,
76
+ R | R2
77
+ > {
78
+ return lazyRef.runUpdates(({ get, set }) =>
79
+ Effect.gen(function* () {
80
+ const initial = yield* get
81
+ yield* set(AsyncData.startLoading(initial))
82
+ const exit = yield* effect.pipe(
83
+ Effect.onInterrupt(() => set(initial)),
84
+ Effect.exit,
85
+ )
86
+ return yield* set(AsyncData.fromExit(exit))
87
+ }),
88
+ )
89
+ })
package/src/Progress.ts CHANGED
@@ -1,80 +1,34 @@
1
- /**
2
- * @since 1.0.0
3
- */
4
-
5
- import * as Data from "effect/Data"
6
- import * as Equivalence from "effect/Equivalence"
7
- import { dual } from "effect/Function"
8
- import * as Option from "effect/Option"
9
-
10
- /**
11
- * @since 1.0.0
12
- */
13
- export interface Progress {
14
- readonly loaded: bigint
15
- readonly total: Option.Option<bigint>
16
- }
1
+ import * as Option from 'effect/Option'
2
+ import * as Schema from 'effect/Schema'
3
+ import { dual } from 'effect/Function'
4
+ import { DataStruct } from './_internal.js'
5
+
6
+ export const Progress = DataStruct({
7
+ loaded: Schema.Number,
8
+ total: Schema.optionalWith(Schema.Number, { as: 'Option', default: undefined }),
9
+ })
17
10
 
18
- /**
19
- * @since 1.0.0
20
- */
21
- export function Progress(loaded: bigint, total: Option.Option<bigint> = Option.none()): Progress {
22
- return Data.struct({
23
- loaded,
24
- total
25
- })
26
- }
11
+ export type ProgressEncoded = typeof Progress.Encoded
12
+ export type Progress = typeof Progress.Type
27
13
 
28
- /**
29
- * @since 1.0.0
30
- */
31
- export const make = (loaded: bigint, total?: bigint | null): Progress => Progress(loaded, Option.fromNullable(total))
14
+ export const empty: Progress = Progress.make({ loaded: 0, total: Option.none() })
32
15
 
33
16
  /**
34
17
  * @since 1.0.0
35
18
  */
36
19
  export const setLoaded: {
37
- (loaded: bigint): (progress: Progress) => Progress
38
- (progress: Progress, loaded: bigint): Progress
39
- } = dual(2, function setLoaded(progress: Progress, loaded: bigint): Progress {
40
- return Progress(
41
- loaded,
42
- progress.total
43
- )
20
+ (loaded: number): (progress: Progress) => Progress
21
+ (progress: Progress, loaded: number): Progress
22
+ } = dual(2, function setLoaded(progress: Progress, loaded: number): Progress {
23
+ return Progress.make({ loaded, total: progress.total })
44
24
  })
45
25
 
46
26
  /**
47
27
  * @since 1.0.0
48
28
  */
49
29
  export const setTotal: {
50
- (total: bigint): (progress: Progress) => Progress
51
- (progress: Progress, total: bigint): Progress
52
- } = dual(2, function setTotal(progress: Progress, total: bigint): Progress {
53
- return Progress(
54
- progress.loaded,
55
- Option.some(total)
56
- )
57
- })
58
-
59
- /**
60
- * @since 1.0.0
61
- */
62
- export const equals: Equivalence.Equivalence<Progress> = Equivalence.struct({
63
- loaded: Equivalence.bigint,
64
- total: Option.getEquivalence(Equivalence.bigint)
30
+ (total: number): (progress: Progress) => Progress
31
+ (progress: Progress, total: number): Progress
32
+ } = dual(2, function setTotal(progress: Progress, total: number): Progress {
33
+ return Progress.make({ loaded: progress.loaded, total: Option.some(total) })
65
34
  })
66
-
67
- /**
68
- * @since 1.0.0
69
- */
70
- export function pretty(progress: Progress): string {
71
- return `${progress.loaded}${
72
- Option.match(
73
- progress.total,
74
- {
75
- onNone: () => "",
76
- onSome: (total) => `/${total}`
77
- }
78
- )
79
- }`
80
- }
package/src/TypeId.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * @since 1.0.0
7
7
  */
8
- export const AsyncDataTypeId = Symbol.for("@typed/async-data/AsyncData")
8
+ export const AsyncDataTypeId = Symbol.for('@typed/async-data/AsyncData')
9
9
 
10
10
  /**
11
11
  * @since 1.0.0
@@ -0,0 +1,111 @@
1
+ import * as Data from 'effect/Data'
2
+ import type * as Effect from 'effect/Effect'
3
+ import * as Effectable from 'effect/Effectable'
4
+ import * as Equal from 'effect/Equal'
5
+ import * as Hash from 'effect/Hash'
6
+ import * as Predicate from 'effect/Predicate'
7
+ import * as Schema from 'effect/Schema'
8
+ import type * as SchemaAST from 'effect/SchemaAST'
9
+ import { structuralRegion } from 'effect/Utils'
10
+
11
+ /**
12
+ * Constructs the same interface as `Schema.Struct`, but with the ability to
13
+ * wrap it in Data.struct to ensure we can hash the struct.
14
+ */
15
+ export function DataStruct<Fields extends Schema.Struct.Fields>(
16
+ fields: Fields,
17
+ ): Schema.Struct<Fields> {
18
+ return liftStructIntoDataStruct(Schema.Struct(fields))
19
+ }
20
+
21
+ function liftStructIntoDataStruct<Fields extends Schema.Struct.Fields>(
22
+ struct: Schema.Struct<Fields>,
23
+ ): Schema.Struct<Fields> {
24
+ const data = Schema.Data(struct)
25
+
26
+ return Object.assign(data, {
27
+ make: (...params: Parameters<typeof struct.make>) => Data.struct(struct.make(...params)),
28
+ pick: <K extends ReadonlyArray<keyof Fields>>(...params: K) =>
29
+ liftStructIntoDataStruct(struct.pick(...params)),
30
+ omit: <K extends ReadonlyArray<keyof Fields>>(...params: K) =>
31
+ liftStructIntoDataStruct(struct.omit(...params)),
32
+ fields: struct.fields,
33
+ records: struct.records,
34
+ annotations: (...params: Parameters<typeof struct.annotations>) =>
35
+ liftStructIntoDataStruct(struct.annotations(...params)),
36
+ })
37
+ }
38
+
39
+ export type LiteralWithDefault<
40
+ K extends string,
41
+ A extends SchemaAST.LiteralValue,
42
+ > = Schema.PropertySignature<':', A, K, ':', A, true, never>
43
+
44
+ export function LiteralWithDefault<K extends string>() {
45
+ return <const A extends SchemaAST.LiteralValue>(value: A): LiteralWithDefault<K, A> => {
46
+ return Schema.optionalWith(Schema.Literal(value), { default: () => value }) as any
47
+ }
48
+ }
49
+
50
+ export type DataEffect<F extends Record<string, any>, A, E = never, R = never> = Effect.Effect<
51
+ A,
52
+ E,
53
+ R
54
+ > &
55
+ F &
56
+ Hash.Hash &
57
+ Equal.Equal
58
+
59
+ export interface DataEffectClass<Tag extends string> {
60
+ readonly _tag: Tag
61
+
62
+ new <Fields extends Record<string, any>, A = never, E = never, R = never>(
63
+ fields: Fields,
64
+ commit: Effect.Effect<A, E, R>,
65
+ ): DataEffect<Fields & { readonly _tag: Tag }, A, E, R>
66
+ }
67
+
68
+ abstract class AbstractDataEffect<F extends Record<string, any>, A, E, R>
69
+ extends Effectable.Class<A, E, R>
70
+ implements Hash.Hash, Equal.Equal
71
+ {
72
+ constructor(fields: F) {
73
+ super()
74
+ Object.assign(this, fields)
75
+ }
76
+
77
+ abstract commit(): Effect.Effect<A, E, R>
78
+
79
+ [Hash.symbol]() {
80
+ return Hash.structure(this)
81
+ }
82
+
83
+ [Equal.symbol](that: unknown) {
84
+ return Predicate.isObject(that) && structuralRegion(() => Equal.equals(this, that))
85
+ }
86
+ }
87
+
88
+ export function DataEffect<const Tag extends string>(tag: Tag): DataEffectClass<Tag> {
89
+ class DataEffect<
90
+ Fields extends Record<string, any>,
91
+ A = never,
92
+ E = never,
93
+ R = never,
94
+ > extends AbstractDataEffect<Fields, A, E, R> {
95
+ static readonly _tag = tag
96
+ readonly _tag = tag
97
+
98
+ constructor(
99
+ fields: Fields,
100
+ readonly effect: Effect.Effect<A, E, R>,
101
+ ) {
102
+ super(fields)
103
+ }
104
+
105
+ commit() {
106
+ return this.effect
107
+ }
108
+ }
109
+
110
+ return DataEffect as any
111
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './AsyncData.js'
2
+ export * from './LazyRef.js'
3
+ export * from './Progress.js'
4
+ export * from './TypeId.js'
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "lib": [
7
+ "ES2022",
8
+ "DOM"
9
+ ],
10
+ "strict": true,
11
+ "esModuleInterop": true,
12
+ "skipLibCheck": true,
13
+ "forceConsistentCasingInFileNames": true,
14
+ "declaration": true,
15
+ "sourceMap": true,
16
+ "outDir": "dist",
17
+ "rootDir": "src"
18
+ },
19
+ "include": [
20
+ "src/**/*"
21
+ ],
22
+ "exclude": [
23
+ "node_modules",
24
+ "dist",
25
+ "**/*.test.ts"
26
+ ]
27
+ }
@@ -1,6 +0,0 @@
1
- {
2
- "main": "../dist/cjs/AsyncData.js",
3
- "module": "../dist/esm/AsyncData.js",
4
- "types": "../dist/dts/AsyncData.d.ts",
5
- "sideEffects": []
6
- }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023-present The Contributors
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,6 +0,0 @@
1
- {
2
- "main": "../dist/cjs/Progress.js",
3
- "module": "../dist/esm/Progress.js",
4
- "types": "../dist/dts/Progress.d.ts",
5
- "sideEffects": []
6
- }
package/README.md DELETED
@@ -1,5 +0,0 @@
1
- # @typed/async-data
2
-
3
- > WIP
4
-
5
- Docs: https://tylors.github.io/typed/docs/async-data
@@ -1,6 +0,0 @@
1
- {
2
- "main": "../dist/cjs/Schema.js",
3
- "module": "../dist/esm/Schema.js",
4
- "types": "../dist/dts/Schema.d.ts",
5
- "sideEffects": []
6
- }
@@ -1,6 +0,0 @@
1
- {
2
- "main": "../dist/cjs/TypeId.js",
3
- "module": "../dist/esm/TypeId.js",
4
- "types": "../dist/dts/TypeId.d.ts",
5
- "sideEffects": []
6
- }