effect 4.0.0-beta.17 → 4.0.0-beta.19
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/dist/Array.d.ts +127 -299
- package/dist/Array.d.ts.map +1 -1
- package/dist/Array.js +102 -62
- package/dist/Array.js.map +1 -1
- package/dist/Cache.d.ts.map +1 -1
- package/dist/Cache.js +5 -4
- package/dist/Cache.js.map +1 -1
- package/dist/Channel.d.ts +97 -11
- package/dist/Channel.d.ts.map +1 -1
- package/dist/Channel.js +72 -29
- package/dist/Channel.js.map +1 -1
- package/dist/Chunk.d.ts +54 -247
- package/dist/Chunk.d.ts.map +1 -1
- package/dist/Chunk.js +36 -67
- package/dist/Chunk.js.map +1 -1
- package/dist/Effect.d.ts +337 -437
- package/dist/Effect.d.ts.map +1 -1
- package/dist/Effect.js +118 -134
- package/dist/Effect.js.map +1 -1
- package/dist/Filter.d.ts +0 -33
- package/dist/Filter.d.ts.map +1 -1
- package/dist/Filter.js +0 -13
- package/dist/Filter.js.map +1 -1
- package/dist/HashMap.d.ts +15 -14
- package/dist/HashMap.d.ts.map +1 -1
- package/dist/HashMap.js +4 -4
- package/dist/HashMap.js.map +1 -1
- package/dist/Iterable.d.ts +40 -39
- package/dist/Iterable.d.ts.map +1 -1
- package/dist/Iterable.js +94 -22
- package/dist/Iterable.js.map +1 -1
- package/dist/Option.d.ts +22 -15
- package/dist/Option.d.ts.map +1 -1
- package/dist/Option.js +14 -7
- package/dist/Option.js.map +1 -1
- package/dist/Pull.d.ts.map +1 -1
- package/dist/Pull.js +1 -1
- package/dist/Pull.js.map +1 -1
- package/dist/Record.d.ts +24 -120
- package/dist/Record.d.ts.map +1 -1
- package/dist/Record.js +21 -41
- package/dist/Record.js.map +1 -1
- package/dist/Sink.d.ts +11 -11
- package/dist/Sink.d.ts.map +1 -1
- package/dist/Sink.js +53 -6
- package/dist/Sink.js.map +1 -1
- package/dist/Stream.d.ts +198 -386
- package/dist/Stream.d.ts.map +1 -1
- package/dist/Stream.js +103 -59
- package/dist/Stream.js.map +1 -1
- package/dist/Trie.d.ts +18 -17
- package/dist/Trie.d.ts.map +1 -1
- package/dist/Trie.js +5 -5
- package/dist/Trie.js.map +1 -1
- package/dist/TxChunk.d.ts +37 -37
- package/dist/TxChunk.d.ts.map +1 -1
- package/dist/TxChunk.js +3 -3
- package/dist/TxChunk.js.map +1 -1
- package/dist/TxDeferred.d.ts +328 -0
- package/dist/TxDeferred.d.ts.map +1 -0
- package/dist/TxDeferred.js +196 -0
- package/dist/TxDeferred.js.map +1 -0
- package/dist/TxHashMap.d.ts +84 -83
- package/dist/TxHashMap.d.ts.map +1 -1
- package/dist/TxHashMap.js +24 -24
- package/dist/TxHashMap.js.map +1 -1
- package/dist/TxHashSet.d.ts +35 -35
- package/dist/TxHashSet.d.ts.map +1 -1
- package/dist/TxHashSet.js +14 -14
- package/dist/TxHashSet.js.map +1 -1
- package/dist/TxPriorityQueue.d.ts +609 -0
- package/dist/TxPriorityQueue.d.ts.map +1 -0
- package/dist/TxPriorityQueue.js +415 -0
- package/dist/TxPriorityQueue.js.map +1 -0
- package/dist/TxPubSub.d.ts +585 -0
- package/dist/TxPubSub.d.ts.map +1 -0
- package/dist/TxPubSub.js +521 -0
- package/dist/TxPubSub.js.map +1 -0
- package/dist/TxQueue.d.ts +32 -32
- package/dist/TxQueue.d.ts.map +1 -1
- package/dist/TxQueue.js +26 -26
- package/dist/TxQueue.js.map +1 -1
- package/dist/TxReentrantLock.d.ts +523 -0
- package/dist/TxReentrantLock.d.ts.map +1 -0
- package/dist/TxReentrantLock.js +504 -0
- package/dist/TxReentrantLock.js.map +1 -0
- package/dist/TxRef.d.ts +34 -34
- package/dist/TxRef.d.ts.map +1 -1
- package/dist/TxRef.js +21 -14
- package/dist/TxRef.js.map +1 -1
- package/dist/TxSemaphore.d.ts +8 -8
- package/dist/TxSemaphore.d.ts.map +1 -1
- package/dist/TxSemaphore.js +7 -7
- package/dist/TxSemaphore.js.map +1 -1
- package/dist/TxSubscriptionRef.d.ts +508 -0
- package/dist/TxSubscriptionRef.d.ts.map +1 -0
- package/dist/TxSubscriptionRef.js +293 -0
- package/dist/TxSubscriptionRef.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/effect.js +99 -42
- package/dist/internal/effect.js.map +1 -1
- package/dist/internal/hashMap.js +3 -2
- package/dist/internal/hashMap.js.map +1 -1
- package/dist/internal/trie.js +5 -4
- package/dist/internal/trie.js.map +1 -1
- package/dist/unstable/ai/Tool.d.ts.map +1 -1
- package/dist/unstable/ai/Tool.js +0 -9
- package/dist/unstable/ai/Tool.js.map +1 -1
- package/dist/unstable/cli/Command.d.ts +1 -1
- package/dist/unstable/cli/Command.d.ts.map +1 -1
- package/dist/unstable/cli/Command.js +1 -1
- package/dist/unstable/cli/Command.js.map +1 -1
- package/dist/unstable/cluster/K8sHttpClient.js +4 -4
- package/dist/unstable/cluster/K8sHttpClient.js.map +1 -1
- package/dist/unstable/cluster/Sharding.js +1 -1
- package/dist/unstable/cluster/Sharding.js.map +1 -1
- package/dist/unstable/encoding/Sse.js +1 -1
- package/dist/unstable/encoding/Sse.js.map +1 -1
- package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcServer.js +1 -2
- package/dist/unstable/rpc/RpcServer.js.map +1 -1
- package/dist/unstable/socket/Socket.d.ts.map +1 -1
- package/dist/unstable/socket/Socket.js +3 -3
- package/dist/unstable/socket/Socket.js.map +1 -1
- package/package.json +1 -1
- package/src/Array.ts +190 -342
- package/src/Cache.ts +6 -5
- package/src/Channel.ts +506 -102
- package/src/Chunk.ts +81 -268
- package/src/Effect.ts +437 -518
- package/src/Filter.ts +0 -57
- package/src/HashMap.ts +15 -14
- package/src/Iterable.ts +105 -50
- package/src/Option.ts +30 -20
- package/src/Pull.ts +1 -1
- package/src/Record.ts +43 -152
- package/src/Sink.ts +75 -23
- package/src/Stream.ts +442 -502
- package/src/Trie.ts +18 -17
- package/src/TxChunk.ts +72 -53
- package/src/TxDeferred.ts +394 -0
- package/src/TxHashMap.ts +332 -285
- package/src/TxHashSet.ts +111 -116
- package/src/TxPriorityQueue.ts +767 -0
- package/src/TxPubSub.ts +789 -0
- package/src/TxQueue.ts +241 -251
- package/src/TxReentrantLock.ts +753 -0
- package/src/TxRef.ts +50 -38
- package/src/TxSemaphore.ts +29 -32
- package/src/TxSubscriptionRef.ts +639 -0
- package/src/index.ts +45 -0
- package/src/internal/effect.ts +368 -163
- package/src/internal/hashMap.ts +7 -5
- package/src/internal/trie.ts +16 -9
- package/src/unstable/ai/Tool.ts +0 -9
- package/src/unstable/cli/Command.ts +6 -4
- package/src/unstable/cluster/K8sHttpClient.ts +4 -4
- package/src/unstable/cluster/Sharding.ts +1 -1
- package/src/unstable/encoding/Sse.ts +1 -1
- package/src/unstable/rpc/RpcServer.ts +1 -7
- package/src/unstable/socket/Socket.ts +9 -11
|
@@ -0,0 +1,639 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TxSubscriptionRef is a TxRef that allows subscribing to all committed changes. Subscribers
|
|
3
|
+
* receive the current value followed by every subsequent update via a transactional queue.
|
|
4
|
+
*
|
|
5
|
+
* @since 4.0.0
|
|
6
|
+
*/
|
|
7
|
+
import * as Effect from "./Effect.ts"
|
|
8
|
+
import { dual } from "./Function.ts"
|
|
9
|
+
import type { Inspectable } from "./Inspectable.ts"
|
|
10
|
+
import { NodeInspectSymbol, toJson } from "./Inspectable.ts"
|
|
11
|
+
import type { Pipeable } from "./Pipeable.ts"
|
|
12
|
+
import { pipeArguments } from "./Pipeable.ts"
|
|
13
|
+
import { hasProperty } from "./Predicate.ts"
|
|
14
|
+
import type * as Scope from "./Scope.ts"
|
|
15
|
+
import * as Stream from "./Stream.ts"
|
|
16
|
+
import * as TxPubSub from "./TxPubSub.ts"
|
|
17
|
+
import * as TxQueue from "./TxQueue.ts"
|
|
18
|
+
import * as TxRef from "./TxRef.ts"
|
|
19
|
+
|
|
20
|
+
const TypeId = "~effect/transactions/TxSubscriptionRef"
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A TxSubscriptionRef is a transactional reference that allows subscribing to all
|
|
24
|
+
* committed changes. Subscribers receive the current value followed by every subsequent
|
|
25
|
+
* update via a transactional dequeue.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { Effect, TxSubscriptionRef, TxQueue } from "effect"
|
|
30
|
+
*
|
|
31
|
+
* const program = Effect.gen(function*() {
|
|
32
|
+
* const ref = yield* TxSubscriptionRef.make(0)
|
|
33
|
+
*
|
|
34
|
+
* yield* Effect.scoped(
|
|
35
|
+
* Effect.gen(function*() {
|
|
36
|
+
* const sub = yield* TxSubscriptionRef.changes(ref)
|
|
37
|
+
* const initial = yield* TxQueue.take(sub)
|
|
38
|
+
* console.log(initial) // 0
|
|
39
|
+
*
|
|
40
|
+
* yield* TxSubscriptionRef.set(ref, 1)
|
|
41
|
+
* const next = yield* TxQueue.take(sub)
|
|
42
|
+
* console.log(next) // 1
|
|
43
|
+
* })
|
|
44
|
+
* )
|
|
45
|
+
* })
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @since 4.0.0
|
|
49
|
+
* @category models
|
|
50
|
+
*/
|
|
51
|
+
export interface TxSubscriptionRef<in out A> extends Inspectable, Pipeable {
|
|
52
|
+
readonly [TypeId]: typeof TypeId
|
|
53
|
+
/** @internal */
|
|
54
|
+
readonly ref: TxRef.TxRef<A>
|
|
55
|
+
/** @internal */
|
|
56
|
+
readonly pubsub: TxPubSub.TxPubSub<A>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const TxSubscriptionRefProto: Omit<TxSubscriptionRef<any>, typeof TypeId | "ref" | "pubsub"> = {
|
|
60
|
+
[NodeInspectSymbol](this: TxSubscriptionRef<unknown>) {
|
|
61
|
+
return toJson(this)
|
|
62
|
+
},
|
|
63
|
+
toJSON(this: TxSubscriptionRef<unknown>) {
|
|
64
|
+
return { _id: "TxSubscriptionRef" }
|
|
65
|
+
},
|
|
66
|
+
toString() {
|
|
67
|
+
return "TxSubscriptionRef"
|
|
68
|
+
},
|
|
69
|
+
pipe() {
|
|
70
|
+
return pipeArguments(this, arguments)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// =============================================================================
|
|
75
|
+
// Constructors
|
|
76
|
+
// =============================================================================
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Creates a new TxSubscriptionRef with the specified initial value.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
84
|
+
*
|
|
85
|
+
* const program = Effect.gen(function*() {
|
|
86
|
+
* const ref = yield* TxSubscriptionRef.make(42)
|
|
87
|
+
* const value = yield* TxSubscriptionRef.get(ref)
|
|
88
|
+
* console.log(value) // 42
|
|
89
|
+
* })
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @since 4.0.0
|
|
93
|
+
* @category constructors
|
|
94
|
+
*/
|
|
95
|
+
export const make = <A>(value: A): Effect.Effect<TxSubscriptionRef<A>, never, Effect.Transaction> =>
|
|
96
|
+
Effect.gen(function*() {
|
|
97
|
+
const ref = yield* TxRef.make(value)
|
|
98
|
+
const pubsub = yield* TxPubSub.unbounded<A>()
|
|
99
|
+
const self = Object.create(TxSubscriptionRefProto)
|
|
100
|
+
self[TypeId] = TypeId
|
|
101
|
+
self.ref = ref
|
|
102
|
+
self.pubsub = pubsub
|
|
103
|
+
return self
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
// =============================================================================
|
|
107
|
+
// Getters
|
|
108
|
+
// =============================================================================
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Reads the current value of the TxSubscriptionRef.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
116
|
+
*
|
|
117
|
+
* const program = Effect.gen(function*() {
|
|
118
|
+
* const ref = yield* TxSubscriptionRef.make("hello")
|
|
119
|
+
* const value = yield* TxSubscriptionRef.get(ref)
|
|
120
|
+
* console.log(value) // "hello"
|
|
121
|
+
* })
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @since 4.0.0
|
|
125
|
+
* @category getters
|
|
126
|
+
*/
|
|
127
|
+
export const get = <A>(self: TxSubscriptionRef<A>): Effect.Effect<A, never, Effect.Transaction> => TxRef.get(self.ref)
|
|
128
|
+
|
|
129
|
+
// =============================================================================
|
|
130
|
+
// Mutations
|
|
131
|
+
// =============================================================================
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Modifies the value of the TxSubscriptionRef using a function that returns both a
|
|
135
|
+
* result and the new value. The new value is published to all subscribers atomically.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
140
|
+
*
|
|
141
|
+
* const program = Effect.gen(function*() {
|
|
142
|
+
* const ref = yield* TxSubscriptionRef.make(10)
|
|
143
|
+
* const result = yield* TxSubscriptionRef.modify(ref, (n) => [`was ${n}`, n + 1])
|
|
144
|
+
* console.log(result) // "was 10"
|
|
145
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 11
|
|
146
|
+
* })
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* @since 4.0.0
|
|
150
|
+
* @category mutations
|
|
151
|
+
*/
|
|
152
|
+
export const modify: {
|
|
153
|
+
// =============================================================================
|
|
154
|
+
// Mutations
|
|
155
|
+
// =============================================================================
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Modifies the value of the TxSubscriptionRef using a function that returns both a
|
|
159
|
+
* result and the new value. The new value is published to all subscribers atomically.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
164
|
+
*
|
|
165
|
+
* const program = Effect.gen(function*() {
|
|
166
|
+
* const ref = yield* TxSubscriptionRef.make(10)
|
|
167
|
+
* const result = yield* TxSubscriptionRef.modify(ref, (n) => [`was ${n}`, n + 1])
|
|
168
|
+
* console.log(result) // "was 10"
|
|
169
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 11
|
|
170
|
+
* })
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* @since 4.0.0
|
|
174
|
+
* @category mutations
|
|
175
|
+
*/
|
|
176
|
+
<A, B>(f: (current: A) => [returnValue: B, newValue: A]): (self: TxSubscriptionRef<A>) => Effect.Effect<B, never, Effect.Transaction>
|
|
177
|
+
// =============================================================================
|
|
178
|
+
// Mutations
|
|
179
|
+
// =============================================================================
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Modifies the value of the TxSubscriptionRef using a function that returns both a
|
|
183
|
+
* result and the new value. The new value is published to all subscribers atomically.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
188
|
+
*
|
|
189
|
+
* const program = Effect.gen(function*() {
|
|
190
|
+
* const ref = yield* TxSubscriptionRef.make(10)
|
|
191
|
+
* const result = yield* TxSubscriptionRef.modify(ref, (n) => [`was ${n}`, n + 1])
|
|
192
|
+
* console.log(result) // "was 10"
|
|
193
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 11
|
|
194
|
+
* })
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @since 4.0.0
|
|
198
|
+
* @category mutations
|
|
199
|
+
*/
|
|
200
|
+
<A, B>(
|
|
201
|
+
self: TxSubscriptionRef<A>,
|
|
202
|
+
f: (current: A) => [returnValue: B, newValue: A]
|
|
203
|
+
): Effect.Effect<B, never, Effect.Transaction>
|
|
204
|
+
} = dual(
|
|
205
|
+
2,
|
|
206
|
+
<A, B>(
|
|
207
|
+
self: TxSubscriptionRef<A>,
|
|
208
|
+
f: (current: A) => [returnValue: B, newValue: A]
|
|
209
|
+
): Effect.Effect<B, never, Effect.Transaction> =>
|
|
210
|
+
Effect.gen(function*() {
|
|
211
|
+
const current = yield* TxRef.get(self.ref)
|
|
212
|
+
const [returnValue, newValue] = f(current)
|
|
213
|
+
yield* TxRef.set(self.ref, newValue)
|
|
214
|
+
yield* TxPubSub.publish(self.pubsub, newValue)
|
|
215
|
+
return returnValue
|
|
216
|
+
})
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Sets the value of the TxSubscriptionRef and publishes the new value to all subscribers.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
225
|
+
*
|
|
226
|
+
* const program = Effect.gen(function*() {
|
|
227
|
+
* const ref = yield* TxSubscriptionRef.make(0)
|
|
228
|
+
* yield* TxSubscriptionRef.set(ref, 42)
|
|
229
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 42
|
|
230
|
+
* })
|
|
231
|
+
* ```
|
|
232
|
+
*
|
|
233
|
+
* @since 4.0.0
|
|
234
|
+
* @category mutations
|
|
235
|
+
*/
|
|
236
|
+
export const set: {
|
|
237
|
+
/**
|
|
238
|
+
* Sets the value of the TxSubscriptionRef and publishes the new value to all subscribers.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
243
|
+
*
|
|
244
|
+
* const program = Effect.gen(function*() {
|
|
245
|
+
* const ref = yield* TxSubscriptionRef.make(0)
|
|
246
|
+
* yield* TxSubscriptionRef.set(ref, 42)
|
|
247
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 42
|
|
248
|
+
* })
|
|
249
|
+
* ```
|
|
250
|
+
*
|
|
251
|
+
* @since 4.0.0
|
|
252
|
+
* @category mutations
|
|
253
|
+
*/
|
|
254
|
+
<A>(value: A): (self: TxSubscriptionRef<A>) => Effect.Effect<void, never, Effect.Transaction>
|
|
255
|
+
/**
|
|
256
|
+
* Sets the value of the TxSubscriptionRef and publishes the new value to all subscribers.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
261
|
+
*
|
|
262
|
+
* const program = Effect.gen(function*() {
|
|
263
|
+
* const ref = yield* TxSubscriptionRef.make(0)
|
|
264
|
+
* yield* TxSubscriptionRef.set(ref, 42)
|
|
265
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 42
|
|
266
|
+
* })
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* @since 4.0.0
|
|
270
|
+
* @category mutations
|
|
271
|
+
*/
|
|
272
|
+
<A>(self: TxSubscriptionRef<A>, value: A): Effect.Effect<void, never, Effect.Transaction>
|
|
273
|
+
} = dual(
|
|
274
|
+
2,
|
|
275
|
+
<A>(self: TxSubscriptionRef<A>, value: A): Effect.Effect<void, never, Effect.Transaction> =>
|
|
276
|
+
modify(self, () => [void 0, value])
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Updates the value of the TxSubscriptionRef using a function and publishes the new
|
|
281
|
+
* value to all subscribers.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```ts
|
|
285
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
286
|
+
*
|
|
287
|
+
* const program = Effect.gen(function*() {
|
|
288
|
+
* const ref = yield* TxSubscriptionRef.make(5)
|
|
289
|
+
* yield* TxSubscriptionRef.update(ref, (n) => n * 2)
|
|
290
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 10
|
|
291
|
+
* })
|
|
292
|
+
* ```
|
|
293
|
+
*
|
|
294
|
+
* @since 4.0.0
|
|
295
|
+
* @category mutations
|
|
296
|
+
*/
|
|
297
|
+
export const update: {
|
|
298
|
+
/**
|
|
299
|
+
* Updates the value of the TxSubscriptionRef using a function and publishes the new
|
|
300
|
+
* value to all subscribers.
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```ts
|
|
304
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
305
|
+
*
|
|
306
|
+
* const program = Effect.gen(function*() {
|
|
307
|
+
* const ref = yield* TxSubscriptionRef.make(5)
|
|
308
|
+
* yield* TxSubscriptionRef.update(ref, (n) => n * 2)
|
|
309
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 10
|
|
310
|
+
* })
|
|
311
|
+
* ```
|
|
312
|
+
*
|
|
313
|
+
* @since 4.0.0
|
|
314
|
+
* @category mutations
|
|
315
|
+
*/
|
|
316
|
+
<A>(f: (current: A) => A): (self: TxSubscriptionRef<A>) => Effect.Effect<void, never, Effect.Transaction>
|
|
317
|
+
/**
|
|
318
|
+
* Updates the value of the TxSubscriptionRef using a function and publishes the new
|
|
319
|
+
* value to all subscribers.
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
* ```ts
|
|
323
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
324
|
+
*
|
|
325
|
+
* const program = Effect.gen(function*() {
|
|
326
|
+
* const ref = yield* TxSubscriptionRef.make(5)
|
|
327
|
+
* yield* TxSubscriptionRef.update(ref, (n) => n * 2)
|
|
328
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 10
|
|
329
|
+
* })
|
|
330
|
+
* ```
|
|
331
|
+
*
|
|
332
|
+
* @since 4.0.0
|
|
333
|
+
* @category mutations
|
|
334
|
+
*/
|
|
335
|
+
<A>(self: TxSubscriptionRef<A>, f: (current: A) => A): Effect.Effect<void, never, Effect.Transaction>
|
|
336
|
+
} = dual(
|
|
337
|
+
2,
|
|
338
|
+
<A>(self: TxSubscriptionRef<A>, f: (current: A) => A): Effect.Effect<void, never, Effect.Transaction> =>
|
|
339
|
+
modify(self, (current) => [void 0, f(current)])
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Gets the current value and sets a new value atomically. Publishes the new value
|
|
344
|
+
* to all subscribers.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```ts
|
|
348
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
349
|
+
*
|
|
350
|
+
* const program = Effect.gen(function*() {
|
|
351
|
+
* const ref = yield* TxSubscriptionRef.make("a")
|
|
352
|
+
* const old = yield* TxSubscriptionRef.getAndSet(ref, "b")
|
|
353
|
+
* console.log(old) // "a"
|
|
354
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // "b"
|
|
355
|
+
* })
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
358
|
+
* @since 4.0.0
|
|
359
|
+
* @category mutations
|
|
360
|
+
*/
|
|
361
|
+
export const getAndSet: {
|
|
362
|
+
/**
|
|
363
|
+
* Gets the current value and sets a new value atomically. Publishes the new value
|
|
364
|
+
* to all subscribers.
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
369
|
+
*
|
|
370
|
+
* const program = Effect.gen(function*() {
|
|
371
|
+
* const ref = yield* TxSubscriptionRef.make("a")
|
|
372
|
+
* const old = yield* TxSubscriptionRef.getAndSet(ref, "b")
|
|
373
|
+
* console.log(old) // "a"
|
|
374
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // "b"
|
|
375
|
+
* })
|
|
376
|
+
* ```
|
|
377
|
+
*
|
|
378
|
+
* @since 4.0.0
|
|
379
|
+
* @category mutations
|
|
380
|
+
*/
|
|
381
|
+
<A>(value: A): (self: TxSubscriptionRef<A>) => Effect.Effect<A, never, Effect.Transaction>
|
|
382
|
+
/**
|
|
383
|
+
* Gets the current value and sets a new value atomically. Publishes the new value
|
|
384
|
+
* to all subscribers.
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```ts
|
|
388
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
389
|
+
*
|
|
390
|
+
* const program = Effect.gen(function*() {
|
|
391
|
+
* const ref = yield* TxSubscriptionRef.make("a")
|
|
392
|
+
* const old = yield* TxSubscriptionRef.getAndSet(ref, "b")
|
|
393
|
+
* console.log(old) // "a"
|
|
394
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // "b"
|
|
395
|
+
* })
|
|
396
|
+
* ```
|
|
397
|
+
*
|
|
398
|
+
* @since 4.0.0
|
|
399
|
+
* @category mutations
|
|
400
|
+
*/
|
|
401
|
+
<A>(self: TxSubscriptionRef<A>, value: A): Effect.Effect<A, never, Effect.Transaction>
|
|
402
|
+
} = dual(
|
|
403
|
+
2,
|
|
404
|
+
<A>(self: TxSubscriptionRef<A>, value: A): Effect.Effect<A, never, Effect.Transaction> =>
|
|
405
|
+
modify(self, (current) => [current, value])
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Gets the current value and updates it using a function atomically. Publishes
|
|
410
|
+
* the new value to all subscribers.
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```ts
|
|
414
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
415
|
+
*
|
|
416
|
+
* const program = Effect.gen(function*() {
|
|
417
|
+
* const ref = yield* TxSubscriptionRef.make(1)
|
|
418
|
+
* const old = yield* TxSubscriptionRef.getAndUpdate(ref, (n) => n + 10)
|
|
419
|
+
* console.log(old) // 1
|
|
420
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 11
|
|
421
|
+
* })
|
|
422
|
+
* ```
|
|
423
|
+
*
|
|
424
|
+
* @since 4.0.0
|
|
425
|
+
* @category mutations
|
|
426
|
+
*/
|
|
427
|
+
export const getAndUpdate: {
|
|
428
|
+
/**
|
|
429
|
+
* Gets the current value and updates it using a function atomically. Publishes
|
|
430
|
+
* the new value to all subscribers.
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* ```ts
|
|
434
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
435
|
+
*
|
|
436
|
+
* const program = Effect.gen(function*() {
|
|
437
|
+
* const ref = yield* TxSubscriptionRef.make(1)
|
|
438
|
+
* const old = yield* TxSubscriptionRef.getAndUpdate(ref, (n) => n + 10)
|
|
439
|
+
* console.log(old) // 1
|
|
440
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 11
|
|
441
|
+
* })
|
|
442
|
+
* ```
|
|
443
|
+
*
|
|
444
|
+
* @since 4.0.0
|
|
445
|
+
* @category mutations
|
|
446
|
+
*/
|
|
447
|
+
<A>(f: (current: A) => A): (self: TxSubscriptionRef<A>) => Effect.Effect<A, never, Effect.Transaction>
|
|
448
|
+
/**
|
|
449
|
+
* Gets the current value and updates it using a function atomically. Publishes
|
|
450
|
+
* the new value to all subscribers.
|
|
451
|
+
*
|
|
452
|
+
* @example
|
|
453
|
+
* ```ts
|
|
454
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
455
|
+
*
|
|
456
|
+
* const program = Effect.gen(function*() {
|
|
457
|
+
* const ref = yield* TxSubscriptionRef.make(1)
|
|
458
|
+
* const old = yield* TxSubscriptionRef.getAndUpdate(ref, (n) => n + 10)
|
|
459
|
+
* console.log(old) // 1
|
|
460
|
+
* console.log(yield* TxSubscriptionRef.get(ref)) // 11
|
|
461
|
+
* })
|
|
462
|
+
* ```
|
|
463
|
+
*
|
|
464
|
+
* @since 4.0.0
|
|
465
|
+
* @category mutations
|
|
466
|
+
*/
|
|
467
|
+
<A>(self: TxSubscriptionRef<A>, f: (current: A) => A): Effect.Effect<A, never, Effect.Transaction>
|
|
468
|
+
} = dual(
|
|
469
|
+
2,
|
|
470
|
+
<A>(self: TxSubscriptionRef<A>, f: (current: A) => A): Effect.Effect<A, never, Effect.Transaction> =>
|
|
471
|
+
modify(self, (current) => [current, f(current)])
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Updates the value using a function and returns the new value. Publishes the
|
|
476
|
+
* new value to all subscribers.
|
|
477
|
+
*
|
|
478
|
+
* @example
|
|
479
|
+
* ```ts
|
|
480
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
481
|
+
*
|
|
482
|
+
* const program = Effect.gen(function*() {
|
|
483
|
+
* const ref = yield* TxSubscriptionRef.make(3)
|
|
484
|
+
* const result = yield* TxSubscriptionRef.updateAndGet(ref, (n) => n * 3)
|
|
485
|
+
* console.log(result) // 9
|
|
486
|
+
* })
|
|
487
|
+
* ```
|
|
488
|
+
*
|
|
489
|
+
* @since 4.0.0
|
|
490
|
+
* @category mutations
|
|
491
|
+
*/
|
|
492
|
+
export const updateAndGet: {
|
|
493
|
+
/**
|
|
494
|
+
* Updates the value using a function and returns the new value. Publishes the
|
|
495
|
+
* new value to all subscribers.
|
|
496
|
+
*
|
|
497
|
+
* @example
|
|
498
|
+
* ```ts
|
|
499
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
500
|
+
*
|
|
501
|
+
* const program = Effect.gen(function*() {
|
|
502
|
+
* const ref = yield* TxSubscriptionRef.make(3)
|
|
503
|
+
* const result = yield* TxSubscriptionRef.updateAndGet(ref, (n) => n * 3)
|
|
504
|
+
* console.log(result) // 9
|
|
505
|
+
* })
|
|
506
|
+
* ```
|
|
507
|
+
*
|
|
508
|
+
* @since 4.0.0
|
|
509
|
+
* @category mutations
|
|
510
|
+
*/
|
|
511
|
+
<A>(f: (current: A) => A): (self: TxSubscriptionRef<A>) => Effect.Effect<A, never, Effect.Transaction>
|
|
512
|
+
/**
|
|
513
|
+
* Updates the value using a function and returns the new value. Publishes the
|
|
514
|
+
* new value to all subscribers.
|
|
515
|
+
*
|
|
516
|
+
* @example
|
|
517
|
+
* ```ts
|
|
518
|
+
* import { Effect, TxSubscriptionRef } from "effect"
|
|
519
|
+
*
|
|
520
|
+
* const program = Effect.gen(function*() {
|
|
521
|
+
* const ref = yield* TxSubscriptionRef.make(3)
|
|
522
|
+
* const result = yield* TxSubscriptionRef.updateAndGet(ref, (n) => n * 3)
|
|
523
|
+
* console.log(result) // 9
|
|
524
|
+
* })
|
|
525
|
+
* ```
|
|
526
|
+
*
|
|
527
|
+
* @since 4.0.0
|
|
528
|
+
* @category mutations
|
|
529
|
+
*/
|
|
530
|
+
<A>(self: TxSubscriptionRef<A>, f: (current: A) => A): Effect.Effect<A, never, Effect.Transaction>
|
|
531
|
+
} = dual(
|
|
532
|
+
2,
|
|
533
|
+
<A>(self: TxSubscriptionRef<A>, f: (current: A) => A): Effect.Effect<A, never, Effect.Transaction> =>
|
|
534
|
+
modify(self, (current) => {
|
|
535
|
+
const newValue = f(current)
|
|
536
|
+
return [newValue, newValue]
|
|
537
|
+
})
|
|
538
|
+
)
|
|
539
|
+
|
|
540
|
+
// =============================================================================
|
|
541
|
+
// Subscriptions
|
|
542
|
+
// =============================================================================
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Subscribes to all changes of the TxSubscriptionRef. Returns a scoped TxDequeue
|
|
546
|
+
* that first yields the current value, then every subsequent update.
|
|
547
|
+
*
|
|
548
|
+
* @example
|
|
549
|
+
* ```ts
|
|
550
|
+
* import { Effect, TxSubscriptionRef, TxQueue } from "effect"
|
|
551
|
+
*
|
|
552
|
+
* const program = Effect.gen(function*() {
|
|
553
|
+
* const ref = yield* TxSubscriptionRef.make(0)
|
|
554
|
+
*
|
|
555
|
+
* yield* Effect.scoped(
|
|
556
|
+
* Effect.gen(function*() {
|
|
557
|
+
* const sub = yield* TxSubscriptionRef.changes(ref)
|
|
558
|
+
* const initial = yield* TxQueue.take(sub)
|
|
559
|
+
* console.log(initial) // 0
|
|
560
|
+
*
|
|
561
|
+
* yield* TxSubscriptionRef.set(ref, 1)
|
|
562
|
+
* const next = yield* TxQueue.take(sub)
|
|
563
|
+
* console.log(next) // 1
|
|
564
|
+
* })
|
|
565
|
+
* )
|
|
566
|
+
* })
|
|
567
|
+
* ```
|
|
568
|
+
*
|
|
569
|
+
* @since 4.0.0
|
|
570
|
+
* @category subscriptions
|
|
571
|
+
*/
|
|
572
|
+
export const changes = <A>(
|
|
573
|
+
self: TxSubscriptionRef<A>
|
|
574
|
+
): Effect.Effect<TxQueue.TxQueue<A>, never, Scope.Scope> =>
|
|
575
|
+
Effect.acquireRelease(
|
|
576
|
+
Effect.transaction(
|
|
577
|
+
Effect.gen(function*() {
|
|
578
|
+
const sub = yield* TxPubSub.acquireSubscriber(self.pubsub)
|
|
579
|
+
const current = yield* TxRef.get(self.ref)
|
|
580
|
+
yield* TxQueue.offer(sub, current)
|
|
581
|
+
return sub
|
|
582
|
+
})
|
|
583
|
+
),
|
|
584
|
+
(queue) => Effect.transaction(TxPubSub.releaseSubscriber(self.pubsub, queue))
|
|
585
|
+
)
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Returns a Stream of all changes to the TxSubscriptionRef, starting with the
|
|
589
|
+
* current value followed by every subsequent update.
|
|
590
|
+
*
|
|
591
|
+
* @example
|
|
592
|
+
* ```ts
|
|
593
|
+
* import { Effect, Stream, TxSubscriptionRef } from "effect"
|
|
594
|
+
*
|
|
595
|
+
* const program = Effect.gen(function*() {
|
|
596
|
+
* const ref = yield* TxSubscriptionRef.make(0)
|
|
597
|
+
* yield* TxSubscriptionRef.set(ref, 1)
|
|
598
|
+
* yield* TxSubscriptionRef.set(ref, 2)
|
|
599
|
+
*
|
|
600
|
+
* const values = yield* Stream.runCollect(
|
|
601
|
+
* TxSubscriptionRef.changesStream(ref).pipe(Stream.take(1))
|
|
602
|
+
* )
|
|
603
|
+
* console.log(values) // [2]
|
|
604
|
+
* })
|
|
605
|
+
* ```
|
|
606
|
+
*
|
|
607
|
+
* @since 4.0.0
|
|
608
|
+
* @category subscriptions
|
|
609
|
+
*/
|
|
610
|
+
export const changesStream = <A>(self: TxSubscriptionRef<A>): Stream.Stream<A, never, never> =>
|
|
611
|
+
Stream.unwrap(
|
|
612
|
+
Effect.map(
|
|
613
|
+
changes(self),
|
|
614
|
+
(sub) => Stream.fromEffectRepeat(Effect.transaction(TxQueue.take(sub)))
|
|
615
|
+
)
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
// =============================================================================
|
|
619
|
+
// Guards
|
|
620
|
+
// =============================================================================
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Checks if the given value is a TxSubscriptionRef.
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* ```ts
|
|
627
|
+
* import { TxSubscriptionRef } from "effect"
|
|
628
|
+
*
|
|
629
|
+
* declare const someValue: unknown
|
|
630
|
+
*
|
|
631
|
+
* if (TxSubscriptionRef.isTxSubscriptionRef(someValue)) {
|
|
632
|
+
* console.log("This is a TxSubscriptionRef")
|
|
633
|
+
* }
|
|
634
|
+
* ```
|
|
635
|
+
*
|
|
636
|
+
* @since 4.0.0
|
|
637
|
+
* @category guards
|
|
638
|
+
*/
|
|
639
|
+
export const isTxSubscriptionRef = (u: unknown): u is TxSubscriptionRef<unknown> => hasProperty(u, TypeId)
|
package/src/index.ts
CHANGED
|
@@ -3920,6 +3920,14 @@ export * as Tuple from "./Tuple.ts"
|
|
|
3920
3920
|
*/
|
|
3921
3921
|
export * as TxChunk from "./TxChunk.ts"
|
|
3922
3922
|
|
|
3923
|
+
/**
|
|
3924
|
+
* A transactional deferred value — a write-once cell that can be read within transactions.
|
|
3925
|
+
* Readers retry until a value is set; once set, the value is immutable.
|
|
3926
|
+
*
|
|
3927
|
+
* @since 4.0.0
|
|
3928
|
+
*/
|
|
3929
|
+
export * as TxDeferred from "./TxDeferred.ts"
|
|
3930
|
+
|
|
3923
3931
|
/**
|
|
3924
3932
|
* @since 2.0.0
|
|
3925
3933
|
*/
|
|
@@ -3930,6 +3938,25 @@ export * as TxHashMap from "./TxHashMap.ts"
|
|
|
3930
3938
|
*/
|
|
3931
3939
|
export * as TxHashSet from "./TxHashSet.ts"
|
|
3932
3940
|
|
|
3941
|
+
/**
|
|
3942
|
+
* A transactional priority queue. Elements are dequeued in order determined by the
|
|
3943
|
+
* provided `Order` instance. All operations participate in the STM transaction system.
|
|
3944
|
+
*
|
|
3945
|
+
* @since 4.0.0
|
|
3946
|
+
*/
|
|
3947
|
+
export * as TxPriorityQueue from "./TxPriorityQueue.ts"
|
|
3948
|
+
|
|
3949
|
+
/**
|
|
3950
|
+
* TxPubSub is a transactional publish/subscribe hub that provides Software Transactional Memory
|
|
3951
|
+
* (STM) semantics for message broadcasting. Publishers broadcast messages to all current
|
|
3952
|
+
* subscribers, with each subscriber receiving its own copy of every published message.
|
|
3953
|
+
*
|
|
3954
|
+
* Supports multiple queue strategies: bounded, unbounded, dropping, and sliding.
|
|
3955
|
+
*
|
|
3956
|
+
* @since 4.0.0
|
|
3957
|
+
*/
|
|
3958
|
+
export * as TxPubSub from "./TxPubSub.ts"
|
|
3959
|
+
|
|
3933
3960
|
/**
|
|
3934
3961
|
* TxQueue is a transactional queue data structure that provides Software Transactional Memory (STM)
|
|
3935
3962
|
* semantics for queue operations. It uses TxRef for transactional state management and supports
|
|
@@ -3943,6 +3970,16 @@ export * as TxHashSet from "./TxHashSet.ts"
|
|
|
3943
3970
|
*/
|
|
3944
3971
|
export * as TxQueue from "./TxQueue.ts"
|
|
3945
3972
|
|
|
3973
|
+
/**
|
|
3974
|
+
* TxReentrantLock is a transactional read/write lock with reentrant semantics using Software
|
|
3975
|
+
* Transactional Memory (STM). Multiple readers can hold the lock concurrently, OR a single
|
|
3976
|
+
* writer can hold exclusive access. A fiber holding a write lock may acquire additional
|
|
3977
|
+
* read or write locks (reentrancy).
|
|
3978
|
+
*
|
|
3979
|
+
* @since 4.0.0
|
|
3980
|
+
*/
|
|
3981
|
+
export * as TxReentrantLock from "./TxReentrantLock.ts"
|
|
3982
|
+
|
|
3946
3983
|
/**
|
|
3947
3984
|
* TxRef is a transactional value, it can be read and modified within the body of a transaction.
|
|
3948
3985
|
*
|
|
@@ -3960,6 +3997,14 @@ export * as TxRef from "./TxRef.ts"
|
|
|
3960
3997
|
*/
|
|
3961
3998
|
export * as TxSemaphore from "./TxSemaphore.ts"
|
|
3962
3999
|
|
|
4000
|
+
/**
|
|
4001
|
+
* TxSubscriptionRef is a TxRef that allows subscribing to all committed changes. Subscribers
|
|
4002
|
+
* receive the current value followed by every subsequent update via a transactional queue.
|
|
4003
|
+
*
|
|
4004
|
+
* @since 4.0.0
|
|
4005
|
+
*/
|
|
4006
|
+
export * as TxSubscriptionRef from "./TxSubscriptionRef.ts"
|
|
4007
|
+
|
|
3963
4008
|
/**
|
|
3964
4009
|
* Type-level utility types for TypeScript.
|
|
3965
4010
|
*
|