@typed/async-data 0.11.0 → 0.13.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 (75) hide show
  1. package/.nvmrc +1 -0
  2. package/biome.json +36 -0
  3. package/dist/AsyncData.d.ts +196 -0
  4. package/dist/AsyncData.js +285 -0
  5. package/dist/AsyncData.js.map +1 -0
  6. package/dist/LazyRef.d.ts +21 -0
  7. package/dist/LazyRef.js +27 -0
  8. package/dist/LazyRef.js.map +1 -0
  9. package/dist/Progress.d.ts +25 -0
  10. package/dist/Progress.js +21 -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 +16 -0
  16. package/dist/_internal.js +53 -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 -48
  22. package/readme.md +218 -0
  23. package/src/AsyncData.test.ts +89 -0
  24. package/src/AsyncData.ts +555 -595
  25. package/src/LazyRef.ts +87 -0
  26. package/src/Progress.ts +18 -65
  27. package/src/TypeId.ts +1 -1
  28. package/src/_internal.ts +114 -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 -538
  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 -750
  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,87 @@
1
+ import * as L from '@typed/lazy-ref'
2
+ import { Effect, Layer } from 'effect'
3
+ import { dual } from 'effect/Function'
4
+ import type { Scope } from 'effect/Scope'
5
+ import * as AsyncData from './AsyncData.js'
6
+
7
+ export interface LazyRef<A, E = never, R = never> extends L.LazyRef<AsyncData.AsyncData<A, E>, never, R> {}
8
+
9
+ export type LazyRefOptions<A, E = never> = L.LazyRefOptions<AsyncData.AsyncData<A, E>> & {
10
+ readonly drop?: number
11
+ readonly take?: number
12
+ }
13
+
14
+ export interface Tagged<Self, Id extends string, A, E = never>
15
+ extends L.TaggedClass<Self, Id, AsyncData.AsyncData<A, E>, never> {
16
+ // Layers
17
+
18
+ readonly Default: Layer.Layer<Self>
19
+ readonly fromEffect: <R>(
20
+ effect: Effect.Effect<A, E, R>,
21
+ ) => Layer.Layer<Self, never, Exclude<R, Scope>>
22
+
23
+ // Effects
24
+ readonly run: <R>(
25
+ effect: Effect.Effect<A, E, R>,
26
+ ) => Effect.Effect<AsyncData.AsyncData<A, E>, never, R | Self>
27
+ }
28
+
29
+ export function Tag<const Id extends string>(id: Id) {
30
+ return <Self, A, E = never>(
31
+ options?: L.LazyRefOptions<AsyncData.AsyncData<A, E>>,
32
+ ): Tagged<Self, Id, A, E> =>
33
+ class extends L.Tag(id)<Self, AsyncData.AsyncData<A, E>>() {
34
+ static readonly Default = this.make(Effect.sync(AsyncData.noData<A, E>), options)
35
+
36
+ static readonly fromEffect = <R>(effect: Effect.Effect<A, E, R>) =>
37
+ Layer.scoped(
38
+ this.tag,
39
+ Effect.gen(function* () {
40
+ const ref = yield* lazyRef(options)
41
+ yield* runEffect(ref, effect).pipe(Effect.forkScoped, Effect.interruptible)
42
+ return ref
43
+ }),
44
+ )
45
+
46
+ static readonly run = <R>(effect: Effect.Effect<A, E, R>) => runEffect(this, effect)
47
+ }
48
+ }
49
+
50
+ export function lazyRef<A, E = never>(
51
+ options?: L.LazyRefOptions<AsyncData.AsyncData<A, E>>,
52
+ ): Effect.Effect<LazyRef<A, E>, never, Scope> {
53
+ return L.sync<AsyncData.AsyncData<A, E>>(AsyncData.noData<A, E>, options)
54
+ }
55
+
56
+ export const runEffect: {
57
+ <A, E, R2>(
58
+ effect: Effect.Effect<A, E, R2>,
59
+ ): <R>(
60
+ lazyRef: L.LazyRef<AsyncData.AsyncData<A, E>, never, R>,
61
+ ) => Effect.Effect<AsyncData.AsyncData<A, E>, never, R | R2>
62
+ <A, E, R, R2>(
63
+ lazyRef: L.LazyRef<AsyncData.AsyncData<A, E>, never, R>,
64
+ effect: Effect.Effect<A, E, R2>,
65
+ ): Effect.Effect<AsyncData.AsyncData<A, E>, never, R | R2>
66
+ } = dual(2, function runEffect<
67
+ A,
68
+ E,
69
+ R,
70
+ R2,
71
+ >(lazyRef: L.LazyRef<AsyncData.AsyncData<A, E>, never, R>, effect: Effect.Effect<A, E, R2>): Effect.Effect<
72
+ AsyncData.AsyncData<A, E>,
73
+ never,
74
+ R | R2
75
+ > {
76
+ return lazyRef.runUpdates(({ get, set }) =>
77
+ Effect.gen(function* () {
78
+ const initial = yield* get
79
+ yield* set(AsyncData.startLoading(initial))
80
+ const exit = yield* effect.pipe(
81
+ Effect.onInterrupt(() => set(initial)),
82
+ Effect.exit,
83
+ )
84
+ return yield* set(AsyncData.fromExit(exit))
85
+ }),
86
+ )
87
+ })
package/src/Progress.ts CHANGED
@@ -1,80 +1,33 @@
1
- /**
2
- * @since 1.0.0
3
- */
1
+ import { Option, Schema } from 'effect'
2
+ import { dual } from 'effect/Function'
3
+ import { DataStruct } from './_internal.js'
4
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"
5
+ export const Progress = DataStruct({
6
+ loaded: Schema.Number,
7
+ total: Schema.optionalWith(Schema.Number, { as: 'Option', default: undefined }),
8
+ })
9
9
 
10
- /**
11
- * @since 1.0.0
12
- */
13
- export interface Progress {
14
- readonly loaded: bigint
15
- readonly total: Option.Option<bigint>
16
- }
10
+ export type ProgressEncoded = typeof Progress.Encoded
11
+ export type Progress = typeof Progress.Type
17
12
 
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
- }
27
-
28
- /**
29
- * @since 1.0.0
30
- */
31
- export const make = (loaded: bigint, total?: bigint | null): Progress => Progress(loaded, Option.fromNullable(total))
13
+ export const empty: Progress = Progress.make({ loaded: 0, total: Option.none() })
32
14
 
33
15
  /**
34
16
  * @since 1.0.0
35
17
  */
36
18
  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
- )
19
+ (loaded: number): (progress: Progress) => Progress
20
+ (progress: Progress, loaded: number): Progress
21
+ } = dual(2, function setLoaded(progress: Progress, loaded: number): Progress {
22
+ return Progress.make({ loaded, total: progress.total })
44
23
  })
45
24
 
46
25
  /**
47
26
  * @since 1.0.0
48
27
  */
49
28
  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
- )
29
+ (total: number): (progress: Progress) => Progress
30
+ (progress: Progress, total: number): Progress
31
+ } = dual(2, function setTotal(progress: Progress, total: number): Progress {
32
+ return Progress.make({ loaded: progress.loaded, total: Option.some(total) })
57
33
  })
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)
65
- })
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,114 @@
1
+ import {
2
+ Data,
3
+ type Effect,
4
+ Effectable,
5
+ Equal,
6
+ Hash,
7
+ Predicate,
8
+ Schema,
9
+ type SchemaAST,
10
+ } from 'effect'
11
+ import { constant } from 'effect/Function'
12
+ import { structuralRegion } from 'effect/Utils'
13
+
14
+ /**
15
+ * Constructs the same interface as `Schema.Struct`, but with the ability to
16
+ * wrap it in Data.struct to ensure we can hash the struct.
17
+ */
18
+ export function DataStruct<Fields extends Schema.Struct.Fields>(
19
+ fields: Fields,
20
+ ): Schema.Struct<Fields> {
21
+ return liftStructIntoDataStruct(Schema.Struct(fields))
22
+ }
23
+
24
+ function liftStructIntoDataStruct<Fields extends Schema.Struct.Fields>(
25
+ struct: Schema.Struct<Fields>,
26
+ ): Schema.Struct<Fields> {
27
+ const data = Schema.Data(struct)
28
+
29
+ return Object.assign(data, {
30
+ make: (...params: Parameters<typeof struct.make>) => Data.struct(struct.make(...params)),
31
+ pick: <K extends ReadonlyArray<keyof Fields>>(...params: K) =>
32
+ liftStructIntoDataStruct(struct.pick(...params)),
33
+ omit: <K extends ReadonlyArray<keyof Fields>>(...params: K) =>
34
+ liftStructIntoDataStruct(struct.omit(...params)),
35
+ fields: struct.fields,
36
+ records: struct.records,
37
+ annotations: (...params: Parameters<typeof struct.annotations>) =>
38
+ liftStructIntoDataStruct(struct.annotations(...params)),
39
+ })
40
+ }
41
+
42
+ export type LiteralWithDefault<
43
+ K extends string,
44
+ A extends SchemaAST.LiteralValue,
45
+ > = Schema.PropertySignature<':', A, K, ':', A, true, never>
46
+
47
+ export function LiteralWithDefault<K extends string>() {
48
+ return <const A extends SchemaAST.LiteralValue>(value: A): LiteralWithDefault<K, A> => {
49
+ return Schema.optionalWith(Schema.Literal(value), { default: () => value }) as any
50
+ }
51
+ }
52
+
53
+ export type DataEffect<F extends Record<string, any>, A, E = never, R = never> = Effect.Effect<
54
+ A,
55
+ E,
56
+ R
57
+ > &
58
+ F &
59
+ Hash.Hash &
60
+ Equal.Equal
61
+
62
+ export interface DataEffectClass<Tag extends string> {
63
+ readonly _tag: Tag
64
+
65
+ new <Fields extends Record<string, any>, A = never, E = never, R = never>(
66
+ fields: Fields,
67
+ commit: Effect.Effect<A, E, R>,
68
+ ): DataEffect<Fields & { readonly _tag: Tag }, A, E, R>
69
+ }
70
+
71
+ abstract class AbstractDataEffect<F extends Record<string, any>, A, E, R>
72
+ extends Effectable.Class<A, E, R>
73
+ implements Hash.Hash, Equal.Equal
74
+ {
75
+ constructor(fields: F) {
76
+ super()
77
+ Object.assign(this, fields)
78
+ }
79
+
80
+ abstract commit(): Effect.Effect<A, E, R>
81
+
82
+ [Hash.symbol]() {
83
+ return Hash.structure(this)
84
+ }
85
+
86
+ [Equal.symbol](that: unknown) {
87
+ return Predicate.isObject(that) && structuralRegion(() => Equal.equals(this, that))
88
+ }
89
+ }
90
+
91
+ export function DataEffect<const Tag extends string>(tag: Tag): DataEffectClass<Tag> {
92
+ class DataEffect<
93
+ Fields extends Record<string, any>,
94
+ A = never,
95
+ E = never,
96
+ R = never,
97
+ > extends AbstractDataEffect<Fields, A, E, R> {
98
+ static readonly _tag = tag
99
+ readonly _tag = tag
100
+
101
+ constructor(
102
+ fields: Fields,
103
+ readonly effect: Effect.Effect<A, E, R>,
104
+ ) {
105
+ super(fields)
106
+ }
107
+
108
+ commit() {
109
+ return this.effect
110
+ }
111
+ }
112
+
113
+ return DataEffect as any
114
+ }
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
- }