@typed/async-data 0.13.1 → 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,89 +0,0 @@
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 DELETED
@@ -1,34 +0,0 @@
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
- })
10
-
11
- export type ProgressEncoded = typeof Progress.Encoded
12
- export type Progress = typeof Progress.Type
13
-
14
- export const empty: Progress = Progress.make({ loaded: 0, total: Option.none() })
15
-
16
- /**
17
- * @since 1.0.0
18
- */
19
- export const setLoaded: {
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 })
24
- })
25
-
26
- /**
27
- * @since 1.0.0
28
- */
29
- export const setTotal: {
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) })
34
- })
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,111 +0,0 @@
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
- }