@typed/fx 1.21.0 → 1.22.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/FormEntry.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @since 1.18.0
3
3
  */
4
- import { Schema } from "@effect/schema"
4
+ import { Equivalence, Schema } from "@effect/schema"
5
5
  import type { ParseOptions } from "@effect/schema/AST"
6
6
  import { type ParseError } from "@effect/schema/ParseResult"
7
7
  import type { Cause } from "effect/Cause"
@@ -18,7 +18,7 @@ import { ComputedTypeId, RefSubjectTypeId } from "./TypeId.js"
18
18
  */
19
19
  export interface FormEntry<out R, in out E, in out I, in out O> extends RefSubject.RefSubject<R, E | ParseError, I> {
20
20
  readonly name: PropertyKey
21
- readonly schema: Schema.Schema<I, O>
21
+ readonly schema: Schema.Schema<R, I, O>
22
22
  readonly decoded: RefSubject.Computed<R, E | ParseError, O>
23
23
  }
24
24
 
@@ -37,9 +37,9 @@ export namespace FormEntry {
37
37
  /**
38
38
  * @since 1.18.0
39
39
  */
40
- export interface FormEntryOptions<I, O> {
40
+ export interface FormEntryOptions<R, I, O> {
41
41
  readonly name: PropertyKey
42
- readonly schema: Schema.Schema<I, O>
42
+ readonly schema: Schema.Schema<R, I, O>
43
43
  }
44
44
 
45
45
  /**
@@ -49,40 +49,42 @@ export interface FormEntryOptions<I, O> {
49
49
  /**
50
50
  * @since 1.18.0
51
51
  */
52
- export type MakeFormEntry<I, O> = {
52
+ export type MakeFormEntry<R0, I, O> = {
53
53
  <R, E>(
54
54
  ref: RefSubject.RefSubject<R, E, O>
55
- ): Effect.Effect<R | Scope.Scope, never, FormEntry.Derived<never, R, E, I, O>>
56
- <R, E>(fx: Fx.Fx<R, E, O>): Effect.Effect<R | Scope.Scope, never, FormEntry<never, E, I, O>>
57
- <R, E>(effect: Effect.Effect<R, E, O>): Effect.Effect<R, never, FormEntry<never, E, I, O>>
55
+ ): Effect.Effect<R0 | R | Scope.Scope, never, FormEntry.Derived<never, R, E, I, O>>
56
+ <R, E>(fx: Fx.Fx<R, E, O>): Effect.Effect<R0 | R | Scope.Scope, never, FormEntry<never, E, I, O>>
57
+ <R, E>(effect: Effect.Effect<R, E, O>): Effect.Effect<R0 | R | Scope.Scope, never, FormEntry<never, E, I, O>>
58
58
  }
59
59
 
60
60
  /**
61
61
  * MakeRefSubject is a RefSubject factory function dervied from a Schema.
62
62
  * @since 1.20.0
63
63
  */
64
- export type MakeInputFormEntry<I, O> = {
64
+ export type MakeInputFormEntry<R0, I, O> = {
65
65
  <R, E>(
66
66
  ref: RefSubject.RefSubject<R, E, I>
67
- ): Effect.Effect<R | Scope.Scope, never, FormEntry.Derived<never, R, E, I, O>>
68
- <R, E>(fx: Fx.Fx<R, E, I>): Effect.Effect<R | Scope.Scope, never, FormEntry<never, E, I, O>>
69
- <R, E>(effect: Effect.Effect<R, E, I>): Effect.Effect<R, never, FormEntry<never, E, I, O>>
67
+ ): Effect.Effect<R | Scope.Scope, never, FormEntry.Derived<R0, R, E, I, O>>
68
+ <R, E>(fx: Fx.Fx<R, E, I>): Effect.Effect<R | Scope.Scope, never, FormEntry<R0, E, I, O>>
69
+ <R, E>(effect: Effect.Effect<R, E, I>): Effect.Effect<R | Scope.Scope, never, FormEntry<R0, E, I, O>>
70
70
  }
71
71
 
72
72
  /**
73
73
  * @since 1.18.0
74
74
  */
75
- export function derive<I, O>(options: FormEntryOptions<I, O>): MakeFormEntry<I, O> {
75
+ export function derive<R, I, O>(options: FormEntryOptions<R, I, O>): MakeFormEntry<R, I, O> {
76
76
  const encode = Schema.encode(options.schema)
77
77
  const decode = Schema.decode(options.schema)
78
- const makeFormEntry = <R, E>(input: RefSubject.RefSubject<R, E, O> | Fx.Fx<R, E, O> | Effect.Effect<R, E, O>) => {
78
+ const makeFormEntry = <R2, E>(input: RefSubject.RefSubject<R2, E, O> | Fx.Fx<R2, E, O> | Effect.Effect<R2, E, O>) => {
79
79
  const initial = Fx.mapEffect(Effect.isEffect(input) ? Fx.fromEffect(input) : input, (o) => encode(o, parseOptions))
80
80
 
81
81
  return Effect.map(
82
- RefSubject.make(initial),
83
- (inputRef): FormEntry<never, E, I, O> | FormEntry.Derived<never, R, E, I, O> => {
84
- if (RefSubject.isRefSubject<R, E, O>(input)) {
85
- const persist = Effect.flatMap(
82
+ RefSubject.make(initial, {
83
+ eq: Equivalence.make(Schema.from(options.schema))
84
+ }),
85
+ (inputRef): FormEntry<R, E, I, O> | FormEntry.Derived<R, R2, E, I, O> => {
86
+ if (RefSubject.isRefSubject<R2, E, O>(input)) {
87
+ const persist: Effect.Effect<R | R2, ParseError | E, O> = Effect.flatMap(
86
88
  Effect.flatMap(
87
89
  inputRef,
88
90
  (i) => decode(i, parseOptions)
@@ -103,25 +105,25 @@ export function derive<I, O>(options: FormEntryOptions<I, O>): MakeFormEntry<I,
103
105
  )
104
106
  }
105
107
 
106
- return makeFormEntry as MakeFormEntry<I, O>
108
+ return makeFormEntry as MakeFormEntry<R, I, O>
107
109
  }
108
110
 
109
111
  /**
110
112
  * @since 1.18.0
111
113
  */
112
- export function deriveInput<I, O>(options: FormEntryOptions<I, O>): MakeInputFormEntry<I, O> {
114
+ export function deriveInput<R, I, O>(options: FormEntryOptions<R, I, O>): MakeInputFormEntry<R, I, O> {
113
115
  const decode = Schema.decode(options.schema)
114
- const makeFormEntry = <R, E>(input: RefSubject.RefSubject<R, E, I> | Fx.Fx<R, E, I> | Effect.Effect<R, E, I>) => {
115
- const initial: Fx.Fx<R, E | ParseError, I> = Effect.isEffect(input) ? Fx.fromEffect(input) : input
116
+ const makeFormEntry = <R2, E>(input: RefSubject.RefSubject<R2, E, I> | Fx.Fx<R2, E, I> | Effect.Effect<R2, E, I>) => {
117
+ const initial: Fx.Fx<R2, E | ParseError, I> = Effect.isEffect(input) ? Fx.fromEffect(input) : input
116
118
 
117
119
  return Effect.map(
118
- RefSubject.make(initial),
119
- (inputRef): FormEntry<never, E, I, O> | FormEntry.Derived<never, R, E, I, O> => {
120
- if (RefSubject.isRefSubject<R, E, O>(input)) {
121
- const persist = Effect.flatMap(
120
+ RefSubject.make(initial, { eq: Equivalence.make(Schema.from(options.schema)) }),
121
+ (inputRef): FormEntry<R, E, I, O> | FormEntry.Derived<R, R2, E, I, O> => {
122
+ if (RefSubject.isRefSubject<R2, E, O>(input)) {
123
+ const persist: Effect.Effect<R | R2, ParseError | E, O> = Effect.flatMap(
122
124
  Effect.flatMap(
123
125
  inputRef,
124
- (i) => RefSubject.set(input as RefSubject.RefSubject<R, E, I>, i)
126
+ (i) => RefSubject.set(input as RefSubject.RefSubject<R2, E, I>, i)
125
127
  ),
126
128
  (i) => decode(i, parseOptions)
127
129
  )
@@ -139,7 +141,7 @@ export function deriveInput<I, O>(options: FormEntryOptions<I, O>): MakeInputFor
139
141
  )
140
142
  }
141
143
 
142
- return makeFormEntry as MakeInputFormEntry<I, O>
144
+ return makeFormEntry as MakeInputFormEntry<R, I, O>
143
145
  }
144
146
 
145
147
  const parseOptions: ParseOptions = {
@@ -147,13 +149,13 @@ const parseOptions: ParseOptions = {
147
149
  onExcessProperty: "ignore"
148
150
  }
149
151
 
150
- class FromEntryImpl<E, I, O> extends FxEffectBase<Scope.Scope, E | ParseError, I, never, ParseError | E, I>
151
- implements FormEntry<never, E, I, O>
152
+ class FromEntryImpl<R, E, I, O> extends FxEffectBase<R | Scope.Scope, E | ParseError, I, R, ParseError | E, I>
153
+ implements FormEntry<R, E, I, O>
152
154
  {
153
155
  readonly [ComputedTypeId]: ComputedTypeId = ComputedTypeId
154
156
  readonly [RefSubjectTypeId]: RefSubjectTypeId = RefSubjectTypeId
155
157
 
156
- readonly decoded: RefSubject.Computed<never, ParseError | E, O>
158
+ readonly decoded: RefSubject.Computed<R, ParseError | E, O>
157
159
  readonly version: Effect.Effect<never, ParseError | E, number>
158
160
  readonly subscriberCount: Effect.Effect<never, never, number>
159
161
  readonly interrupt: Effect.Effect<never, never, void>
@@ -161,7 +163,7 @@ class FromEntryImpl<E, I, O> extends FxEffectBase<Scope.Scope, E | ParseError, I
161
163
  constructor(
162
164
  readonly ref: RefSubject.RefSubject<never, E | ParseError, I>,
163
165
  readonly name: PropertyKey,
164
- readonly schema: Schema.Schema<I, O>
166
+ readonly schema: Schema.Schema<R, I, O>
165
167
  ) {
166
168
  super()
167
169
 
@@ -195,12 +197,14 @@ class FromEntryImpl<E, I, O> extends FxEffectBase<Scope.Scope, E | ParseError, I
195
197
  }
196
198
  }
197
199
 
198
- class DerivedFormEntryImpl<R, E, I, O> extends FromEntryImpl<E, I, O> implements FormEntry.Derived<never, R, E, I, O> {
200
+ class DerivedFormEntryImpl<R, R2, E, I, O> extends FromEntryImpl<R, E, I, O>
201
+ implements FormEntry.Derived<R, R2, E, I, O>
202
+ {
199
203
  constructor(
200
204
  ref: RefSubject.RefSubject<never, E | ParseError, I>,
201
205
  name: PropertyKey,
202
- schema: Schema.Schema<I, O>,
203
- readonly persist: Effect.Effect<R, E | ParseError, O>
206
+ schema: Schema.Schema<R, I, O>,
207
+ readonly persist: Effect.Effect<R2, E | ParseError, O>
204
208
  ) {
205
209
  super(ref, name, schema)
206
210
  }
@@ -2464,12 +2464,16 @@ export function debounce<R, E, A>(fx: Fx<R, E, A>, delay: Duration.DurationInput
2464
2464
  return switchMapEffect(fx, (a) => Effect.as(Effect.sleep(delay), a))
2465
2465
  }
2466
2466
 
2467
+ function emitAndSleep<A>(value: A, delay: Duration.DurationInput) {
2468
+ return make<A>((sink) => Effect.zipRight(sink.onSuccess(value), Effect.sleep(delay)))
2469
+ }
2470
+
2467
2471
  export function throttle<R, E, A>(fx: Fx<R, E, A>, delay: Duration.DurationInput): Fx<R | Scope.Scope, E, A> {
2468
- return exhaustMapEffect(fx, (a) => Effect.as(Effect.sleep(delay), a))
2472
+ return exhaustMap(fx, (a) => emitAndSleep(a, delay))
2469
2473
  }
2470
2474
 
2471
2475
  export function throttleLatest<R, E, A>(fx: Fx<R, E, A>, delay: Duration.DurationInput): Fx<R | Scope.Scope, E, A> {
2472
- return exhaustMapLatestEffect(fx, (a) => Effect.as(Effect.sleep(delay), a))
2476
+ return exhaustMapLatest(fx, (a) => emitAndSleep(a, delay))
2473
2477
  }
2474
2478
 
2475
2479
  export function fromAsyncIterable<A>(iterable: AsyncIterable<A>): Fx<never, never, A> {
@@ -1,4 +1,4 @@
1
- import type { Sink } from "../Sink"
1
+ import type { Sink } from "../Sink.js"
2
2
  import * as EffectLoopOp from "./effect-loop-operator.js"
3
3
  import * as EffectOp from "./effect-operator.js"
4
4
  import * as LoopOp from "./loop-operator.js"