@livestore/utils 0.3.0-dev.1 → 0.3.0-dev.10

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.
@@ -0,0 +1,143 @@
1
+ // Fork of effect/Subscribable.ts which makes Subscribable yieldable
2
+
3
+ /**
4
+ * @since 2.0.0
5
+ */
6
+
7
+ import { Effect, Effectable, Readable, Stream } from 'effect'
8
+ import { dual } from 'effect/Function'
9
+ import { hasProperty } from 'effect/Predicate'
10
+
11
+ /**
12
+ * @since 2.0.0
13
+ * @category type ids
14
+ */
15
+ export const TypeId: unique symbol = Symbol.for('effect/Subscribable')
16
+
17
+ /**
18
+ * @since 2.0.0
19
+ * @category type ids
20
+ */
21
+ export type TypeId = typeof TypeId
22
+
23
+ /**
24
+ * @since 2.0.0
25
+ * @category models
26
+ */
27
+ export interface Subscribable<A, E = never, R = never> extends Readable.Readable<A, E, R>, Effect.Effect<A, E, R> {
28
+ readonly [TypeId]: TypeId
29
+ readonly changes: Stream.Stream<A, E, R>
30
+ }
31
+
32
+ /**
33
+ * @since 2.0.0
34
+ * @category refinements
35
+ */
36
+ export const isSubscribable = (u: unknown): u is Subscribable<unknown, unknown, unknown> => hasProperty(u, TypeId)
37
+
38
+ // const Proto: Omit<Subscribable<any>, 'get' | 'changes'> = {
39
+ // [Readable.TypeId]: Readable.TypeId,
40
+ // [TypeId]: TypeId,
41
+ // pipe() {
42
+ // return pipeArguments(this, arguments)
43
+ // },
44
+ // }
45
+
46
+ class SubscribableImpl<in out A> extends Effectable.Class<A> implements Subscribable<A> {
47
+ // @ts-expect-error type symbol
48
+ readonly [TypeId] = TypeId
49
+ // @ts-expect-error type symbol
50
+ readonly [Readable.TypeId] = Readable.TypeId
51
+ constructor(
52
+ readonly get: Effect.Effect<A>,
53
+ readonly changes: Stream.Stream<A>,
54
+ ) {
55
+ super()
56
+ }
57
+ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions
58
+ commit() {
59
+ return this.get
60
+ }
61
+ }
62
+
63
+ /**
64
+ * @since 2.0.0
65
+ * @category constructors
66
+ */
67
+ // export const make = <A, E, R>(options: {
68
+ // readonly get: Effect.Effect<A, E, R>
69
+ // readonly changes: Stream.Stream<A, E, R>
70
+ // }): Subscribable<A, E, R> => Object.assign(Object.create(Proto), options)
71
+
72
+ export const make = <A, E, R>(options: {
73
+ readonly get: Effect.Effect<A, E, R>
74
+ readonly changes: Stream.Stream<A, E, R>
75
+ }): Subscribable<A, E, R> => new SubscribableImpl(options.get as any, options.changes as any) as Subscribable<A, E, R>
76
+
77
+ /**
78
+ * @since 2.0.0
79
+ * @category combinators
80
+ */
81
+ export const map: {
82
+ /**
83
+ * @since 2.0.0
84
+ * @category combinators
85
+ */
86
+ <A, B>(f: (a: NoInfer<A>) => B): <E, R>(fa: Subscribable<A, E, R>) => Subscribable<B, E, R>
87
+ /**
88
+ * @since 2.0.0
89
+ * @category combinators
90
+ */
91
+ <A, E, R, B>(self: Subscribable<A, E, R>, f: (a: NoInfer<A>) => B): Subscribable<B, E, R>
92
+ } = dual(
93
+ 2,
94
+ <A, E, R, B>(self: Subscribable<A, E, R>, f: (a: NoInfer<A>) => B): Subscribable<B, E, R> =>
95
+ make({
96
+ get: Effect.map(self.get, f),
97
+ changes: Stream.map(self.changes, f),
98
+ }),
99
+ )
100
+
101
+ /**
102
+ * @since 2.0.0
103
+ * @category combinators
104
+ */
105
+ export const mapEffect: {
106
+ /**
107
+ * @since 2.0.0
108
+ * @category combinators
109
+ */
110
+ <A, B, E2, R2>(
111
+ f: (a: NoInfer<A>) => Effect.Effect<B, E2, R2>,
112
+ ): <E, R>(fa: Subscribable<A, E, R>) => Subscribable<B, E | E2, R | R2>
113
+ /**
114
+ * @since 2.0.0
115
+ * @category combinators
116
+ */
117
+ <A, E, R, B, E2, R2>(
118
+ self: Subscribable<A, E, R>,
119
+ f: (a: NoInfer<A>) => Effect.Effect<B, E2, R2>,
120
+ ): Subscribable<B, E | E2, R | R2>
121
+ } = dual(
122
+ 2,
123
+ <A, E, R, B, E2, R2>(
124
+ self: Subscribable<A, E, R>,
125
+ f: (a: NoInfer<A>) => Effect.Effect<B, E2, R2>,
126
+ ): Subscribable<B, E | E2, R | R2> =>
127
+ make({
128
+ get: Effect.flatMap(self.get, f),
129
+ changes: Stream.mapEffect(self.changes, f),
130
+ }),
131
+ )
132
+
133
+ /**
134
+ * @since 2.0.0
135
+ * @category constructors
136
+ */
137
+ export const unwrap = <A, E, R, E1, R1>(
138
+ effect: Effect.Effect<Subscribable<A, E, R>, E1, R1>,
139
+ ): Subscribable<A, E | E1, R | R1> =>
140
+ make({
141
+ get: Effect.flatMap(effect, (s) => s.get),
142
+ changes: Stream.unwrap(Effect.map(effect, (s) => s.changes)),
143
+ })
@@ -9,6 +9,21 @@ export * from './WebChannel/broadcastChannelWithAck.js'
9
9
 
10
10
  export * from './WebChannel/common.js'
11
11
 
12
+ export const noopChannel = <MsgListen, MsgSend>(): Effect.Effect<WebChannel<MsgListen, MsgSend>> =>
13
+ Effect.gen(function* () {
14
+ return {
15
+ [WebChannelSymbol]: WebChannelSymbol,
16
+ send: () => Effect.void,
17
+ listen: Stream.never,
18
+ closedDeferred: yield* Deferred.make<void>(),
19
+ schema: {
20
+ listen: Schema.Any,
21
+ send: Schema.Any,
22
+ } as any,
23
+ supportsTransferables: false,
24
+ }
25
+ })
26
+
12
27
  export const broadcastChannel = <MsgListen, MsgSend, MsgListenEncoded, MsgSendEncoded>({
13
28
  channelName,
14
29
  schema: inputSchema,
@@ -49,11 +49,13 @@ export {
49
49
  TRef,
50
50
  Channel,
51
51
  Predicate,
52
+ // Subscribable,
52
53
  pipe,
53
54
  identity,
54
55
  GlobalValue,
55
56
  Match,
56
57
  TestServices,
58
+ Mailbox,
57
59
  } from 'effect'
58
60
 
59
61
  export { dual } from 'effect/Function'
@@ -63,6 +65,7 @@ export * as Stream from './Stream.js'
63
65
  export * as BucketQueue from './BucketQueue.js'
64
66
 
65
67
  export * as SubscriptionRef from './SubscriptionRef.js'
68
+ export * as Subscribable from './Subscribable.js'
66
69
 
67
70
  export * as Logger from './Logger.js'
68
71
 
package/src/node/mod.ts CHANGED
@@ -8,7 +8,8 @@ import { Config, Effect, Layer } from 'effect'
8
8
  import type { ParentSpan } from 'effect/Tracer'
9
9
 
10
10
  import { tapCauseLogPretty } from '../effect/Effect.js'
11
- import type { OtelTracer } from '../effect/index.js'
11
+ import { OtelTracer } from '../effect/index.js'
12
+ import { makeNoopTracer } from '../NoopTracer.js'
12
13
 
13
14
  // import { tapCauseLogPretty } from '../effect/Effect.js'
14
15
 
@@ -30,6 +31,16 @@ export * as ChildProcessWorker from './ChildProcessRunner/ChildProcessWorker.js'
30
31
 
31
32
  // export const OtelLiveHttp = (args: any): Layer.Layer<never> => Layer.empty
32
33
 
34
+ export const OtelLiveDummy: Layer.Layer<OtelTracer.OtelTracer> = Layer.suspend(() => {
35
+ const OtelTracerLive = Layer.succeed(OtelTracer.OtelTracer, makeNoopTracer())
36
+
37
+ const TracingLive = Layer.unwrapEffect(Effect.map(OtelTracer.make, Layer.setTracer)).pipe(
38
+ Layer.provideMerge(OtelTracerLive),
39
+ ) as any as Layer.Layer<OtelTracer.OtelTracer>
40
+
41
+ return TracingLive
42
+ })
43
+
33
44
  export const OtelLiveHttp = ({
34
45
  serviceName,
35
46
  rootSpanName,