@typed/async-data 0.13.0 → 1.0.0-beta.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/src/LazyRef.ts DELETED
@@ -1,87 +0,0 @@
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 DELETED
@@ -1,33 +0,0 @@
1
- import { Option, Schema } from 'effect'
2
- import { dual } from 'effect/Function'
3
- import { DataStruct } from './_internal.js'
4
-
5
- export const Progress = DataStruct({
6
- loaded: Schema.Number,
7
- total: Schema.optionalWith(Schema.Number, { as: 'Option', default: undefined }),
8
- })
9
-
10
- export type ProgressEncoded = typeof Progress.Encoded
11
- export type Progress = typeof Progress.Type
12
-
13
- export const empty: Progress = Progress.make({ loaded: 0, total: Option.none() })
14
-
15
- /**
16
- * @since 1.0.0
17
- */
18
- export const setLoaded: {
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 })
23
- })
24
-
25
- /**
26
- * @since 1.0.0
27
- */
28
- export const setTotal: {
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) })
33
- })
package/src/TypeId.ts DELETED
@@ -1,13 +0,0 @@
1
- /**
2
- * @since 1.0.0
3
- */
4
-
5
- /**
6
- * @since 1.0.0
7
- */
8
- export const AsyncDataTypeId = Symbol.for('@typed/async-data/AsyncData')
9
-
10
- /**
11
- * @since 1.0.0
12
- */
13
- export type AsyncDataTypeId = typeof AsyncDataTypeId
package/src/_internal.ts DELETED
@@ -1,114 +0,0 @@
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
- }