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.
Files changed (164) hide show
  1. package/dist/Array.d.ts +127 -299
  2. package/dist/Array.d.ts.map +1 -1
  3. package/dist/Array.js +102 -62
  4. package/dist/Array.js.map +1 -1
  5. package/dist/Cache.d.ts.map +1 -1
  6. package/dist/Cache.js +5 -4
  7. package/dist/Cache.js.map +1 -1
  8. package/dist/Channel.d.ts +97 -11
  9. package/dist/Channel.d.ts.map +1 -1
  10. package/dist/Channel.js +72 -29
  11. package/dist/Channel.js.map +1 -1
  12. package/dist/Chunk.d.ts +54 -247
  13. package/dist/Chunk.d.ts.map +1 -1
  14. package/dist/Chunk.js +36 -67
  15. package/dist/Chunk.js.map +1 -1
  16. package/dist/Effect.d.ts +337 -437
  17. package/dist/Effect.d.ts.map +1 -1
  18. package/dist/Effect.js +118 -134
  19. package/dist/Effect.js.map +1 -1
  20. package/dist/Filter.d.ts +0 -33
  21. package/dist/Filter.d.ts.map +1 -1
  22. package/dist/Filter.js +0 -13
  23. package/dist/Filter.js.map +1 -1
  24. package/dist/HashMap.d.ts +15 -14
  25. package/dist/HashMap.d.ts.map +1 -1
  26. package/dist/HashMap.js +4 -4
  27. package/dist/HashMap.js.map +1 -1
  28. package/dist/Iterable.d.ts +40 -39
  29. package/dist/Iterable.d.ts.map +1 -1
  30. package/dist/Iterable.js +94 -22
  31. package/dist/Iterable.js.map +1 -1
  32. package/dist/Option.d.ts +22 -15
  33. package/dist/Option.d.ts.map +1 -1
  34. package/dist/Option.js +14 -7
  35. package/dist/Option.js.map +1 -1
  36. package/dist/Pull.d.ts.map +1 -1
  37. package/dist/Pull.js +1 -1
  38. package/dist/Pull.js.map +1 -1
  39. package/dist/Record.d.ts +24 -120
  40. package/dist/Record.d.ts.map +1 -1
  41. package/dist/Record.js +21 -41
  42. package/dist/Record.js.map +1 -1
  43. package/dist/Sink.d.ts +11 -11
  44. package/dist/Sink.d.ts.map +1 -1
  45. package/dist/Sink.js +53 -6
  46. package/dist/Sink.js.map +1 -1
  47. package/dist/Stream.d.ts +198 -386
  48. package/dist/Stream.d.ts.map +1 -1
  49. package/dist/Stream.js +103 -59
  50. package/dist/Stream.js.map +1 -1
  51. package/dist/Trie.d.ts +18 -17
  52. package/dist/Trie.d.ts.map +1 -1
  53. package/dist/Trie.js +5 -5
  54. package/dist/Trie.js.map +1 -1
  55. package/dist/TxChunk.d.ts +37 -37
  56. package/dist/TxChunk.d.ts.map +1 -1
  57. package/dist/TxChunk.js +3 -3
  58. package/dist/TxChunk.js.map +1 -1
  59. package/dist/TxDeferred.d.ts +328 -0
  60. package/dist/TxDeferred.d.ts.map +1 -0
  61. package/dist/TxDeferred.js +196 -0
  62. package/dist/TxDeferred.js.map +1 -0
  63. package/dist/TxHashMap.d.ts +84 -83
  64. package/dist/TxHashMap.d.ts.map +1 -1
  65. package/dist/TxHashMap.js +24 -24
  66. package/dist/TxHashMap.js.map +1 -1
  67. package/dist/TxHashSet.d.ts +35 -35
  68. package/dist/TxHashSet.d.ts.map +1 -1
  69. package/dist/TxHashSet.js +14 -14
  70. package/dist/TxHashSet.js.map +1 -1
  71. package/dist/TxPriorityQueue.d.ts +609 -0
  72. package/dist/TxPriorityQueue.d.ts.map +1 -0
  73. package/dist/TxPriorityQueue.js +415 -0
  74. package/dist/TxPriorityQueue.js.map +1 -0
  75. package/dist/TxPubSub.d.ts +585 -0
  76. package/dist/TxPubSub.d.ts.map +1 -0
  77. package/dist/TxPubSub.js +521 -0
  78. package/dist/TxPubSub.js.map +1 -0
  79. package/dist/TxQueue.d.ts +32 -32
  80. package/dist/TxQueue.d.ts.map +1 -1
  81. package/dist/TxQueue.js +26 -26
  82. package/dist/TxQueue.js.map +1 -1
  83. package/dist/TxReentrantLock.d.ts +523 -0
  84. package/dist/TxReentrantLock.d.ts.map +1 -0
  85. package/dist/TxReentrantLock.js +504 -0
  86. package/dist/TxReentrantLock.js.map +1 -0
  87. package/dist/TxRef.d.ts +34 -34
  88. package/dist/TxRef.d.ts.map +1 -1
  89. package/dist/TxRef.js +21 -14
  90. package/dist/TxRef.js.map +1 -1
  91. package/dist/TxSemaphore.d.ts +8 -8
  92. package/dist/TxSemaphore.d.ts.map +1 -1
  93. package/dist/TxSemaphore.js +7 -7
  94. package/dist/TxSemaphore.js.map +1 -1
  95. package/dist/TxSubscriptionRef.d.ts +508 -0
  96. package/dist/TxSubscriptionRef.d.ts.map +1 -0
  97. package/dist/TxSubscriptionRef.js +293 -0
  98. package/dist/TxSubscriptionRef.js.map +1 -0
  99. package/dist/index.d.ts +40 -0
  100. package/dist/index.d.ts.map +1 -1
  101. package/dist/index.js +40 -0
  102. package/dist/index.js.map +1 -1
  103. package/dist/internal/effect.js +99 -42
  104. package/dist/internal/effect.js.map +1 -1
  105. package/dist/internal/hashMap.js +3 -2
  106. package/dist/internal/hashMap.js.map +1 -1
  107. package/dist/internal/trie.js +5 -4
  108. package/dist/internal/trie.js.map +1 -1
  109. package/dist/unstable/ai/Tool.d.ts.map +1 -1
  110. package/dist/unstable/ai/Tool.js +0 -9
  111. package/dist/unstable/ai/Tool.js.map +1 -1
  112. package/dist/unstable/cli/Command.d.ts +1 -1
  113. package/dist/unstable/cli/Command.d.ts.map +1 -1
  114. package/dist/unstable/cli/Command.js +1 -1
  115. package/dist/unstable/cli/Command.js.map +1 -1
  116. package/dist/unstable/cluster/K8sHttpClient.js +4 -4
  117. package/dist/unstable/cluster/K8sHttpClient.js.map +1 -1
  118. package/dist/unstable/cluster/Sharding.js +1 -1
  119. package/dist/unstable/cluster/Sharding.js.map +1 -1
  120. package/dist/unstable/encoding/Sse.js +1 -1
  121. package/dist/unstable/encoding/Sse.js.map +1 -1
  122. package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
  123. package/dist/unstable/rpc/RpcServer.js +1 -2
  124. package/dist/unstable/rpc/RpcServer.js.map +1 -1
  125. package/dist/unstable/socket/Socket.d.ts.map +1 -1
  126. package/dist/unstable/socket/Socket.js +3 -3
  127. package/dist/unstable/socket/Socket.js.map +1 -1
  128. package/package.json +1 -1
  129. package/src/Array.ts +190 -342
  130. package/src/Cache.ts +6 -5
  131. package/src/Channel.ts +506 -102
  132. package/src/Chunk.ts +81 -268
  133. package/src/Effect.ts +437 -518
  134. package/src/Filter.ts +0 -57
  135. package/src/HashMap.ts +15 -14
  136. package/src/Iterable.ts +105 -50
  137. package/src/Option.ts +30 -20
  138. package/src/Pull.ts +1 -1
  139. package/src/Record.ts +43 -152
  140. package/src/Sink.ts +75 -23
  141. package/src/Stream.ts +442 -502
  142. package/src/Trie.ts +18 -17
  143. package/src/TxChunk.ts +72 -53
  144. package/src/TxDeferred.ts +394 -0
  145. package/src/TxHashMap.ts +332 -285
  146. package/src/TxHashSet.ts +111 -116
  147. package/src/TxPriorityQueue.ts +767 -0
  148. package/src/TxPubSub.ts +789 -0
  149. package/src/TxQueue.ts +241 -251
  150. package/src/TxReentrantLock.ts +753 -0
  151. package/src/TxRef.ts +50 -38
  152. package/src/TxSemaphore.ts +29 -32
  153. package/src/TxSubscriptionRef.ts +639 -0
  154. package/src/index.ts +45 -0
  155. package/src/internal/effect.ts +368 -163
  156. package/src/internal/hashMap.ts +7 -5
  157. package/src/internal/trie.ts +16 -9
  158. package/src/unstable/ai/Tool.ts +0 -9
  159. package/src/unstable/cli/Command.ts +6 -4
  160. package/src/unstable/cluster/K8sHttpClient.ts +4 -4
  161. package/src/unstable/cluster/Sharding.ts +1 -1
  162. package/src/unstable/encoding/Sse.ts +1 -1
  163. package/src/unstable/rpc/RpcServer.ts +1 -7
  164. package/src/unstable/socket/Socket.ts +9 -11
@@ -0,0 +1,767 @@
1
+ /**
2
+ * A transactional priority queue. Elements are dequeued in order determined by the
3
+ * provided `Order` instance. All operations participate in the STM transaction system.
4
+ *
5
+ * @since 4.0.0
6
+ */
7
+
8
+ import type { Chunk } from "./Chunk.ts"
9
+ import * as C from "./Chunk.ts"
10
+ import * as Effect from "./Effect.ts"
11
+ import { dual } from "./Function.ts"
12
+ import type { Inspectable } from "./Inspectable.ts"
13
+ import { NodeInspectSymbol, toJson } from "./Inspectable.ts"
14
+ import type { Option } from "./Option.ts"
15
+ import * as O from "./Option.ts"
16
+ import type { Order } from "./Order.ts"
17
+ import type { Pipeable } from "./Pipeable.ts"
18
+ import { pipeArguments } from "./Pipeable.ts"
19
+ import type { Predicate } from "./Predicate.ts"
20
+ import * as TxRef from "./TxRef.ts"
21
+
22
+ const TypeId = "~effect/transactions/TxPriorityQueue"
23
+
24
+ /**
25
+ * A transactional priority queue backed by a sorted `Chunk`.
26
+ *
27
+ * Elements are stored in ascending order according to the `Order` provided at
28
+ * construction time. `take` returns the smallest element, `peek` observes it
29
+ * without removing.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * import { Effect, Order, TxPriorityQueue } from "effect"
34
+ *
35
+ * const program = Effect.gen(function*() {
36
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
37
+ * yield* TxPriorityQueue.offer(pq, 3)
38
+ * yield* TxPriorityQueue.offer(pq, 1)
39
+ * yield* TxPriorityQueue.offer(pq, 2)
40
+ * const first = yield* TxPriorityQueue.take(pq)
41
+ * console.log(first) // 1
42
+ * })
43
+ * ```
44
+ *
45
+ * @since 4.0.0
46
+ * @category models
47
+ */
48
+ export interface TxPriorityQueue<in out A> extends Inspectable, Pipeable {
49
+ readonly [TypeId]: typeof TypeId
50
+ readonly ref: TxRef.TxRef<Chunk<A>>
51
+ readonly ord: Order<A>
52
+ }
53
+
54
+ const TxPriorityQueueProto: Omit<TxPriorityQueue<unknown>, typeof TypeId | "ref" | "ord"> = {
55
+ [NodeInspectSymbol](this: TxPriorityQueue<unknown>) {
56
+ return toJson(this)
57
+ },
58
+ toJSON(this: TxPriorityQueue<unknown>) {
59
+ return {
60
+ _id: "TxPriorityQueue"
61
+ }
62
+ },
63
+ pipe() {
64
+ return pipeArguments(this, arguments)
65
+ }
66
+ }
67
+
68
+ const makeTxPriorityQueue = <A>(ref: TxRef.TxRef<Chunk<A>>, ord: Order<A>): TxPriorityQueue<A> => {
69
+ const self = Object.create(TxPriorityQueueProto)
70
+ self[TypeId] = TypeId
71
+ self.ref = ref
72
+ self.ord = ord
73
+ return self
74
+ }
75
+
76
+ const insertSorted = <A>(chunk: Chunk<A>, value: A, ord: Order<A>): Chunk<A> => {
77
+ const arr = C.toArray(chunk) as Array<A>
78
+ let lo = 0
79
+ let hi = arr.length
80
+ while (lo < hi) {
81
+ const mid = (lo + hi) >>> 1
82
+ if (ord(arr[mid], value) <= 0) {
83
+ lo = mid + 1
84
+ } else {
85
+ hi = mid
86
+ }
87
+ }
88
+ const out = Array(arr.length + 1) as Array<A>
89
+ for (let i = 0; i < lo; i++) out[i] = arr[i]
90
+ out[lo] = value
91
+ for (let i = lo; i < arr.length; i++) out[i + 1] = arr[i]
92
+ return C.fromIterable(out)
93
+ }
94
+
95
+ /**
96
+ * Creates an empty `TxPriorityQueue` with the given ordering.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * import { Effect, Order, TxPriorityQueue } from "effect"
101
+ *
102
+ * const program = Effect.gen(function*() {
103
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
104
+ * const empty = yield* TxPriorityQueue.isEmpty(pq)
105
+ * console.log(empty) // true
106
+ * })
107
+ * ```
108
+ *
109
+ * @since 4.0.0
110
+ * @category constructors
111
+ */
112
+ export const empty = <A>(order: Order<A>): Effect.Effect<TxPriorityQueue<A>, never, Effect.Transaction> =>
113
+ Effect.map(TxRef.make<Chunk<A>>(C.empty()), (ref) => makeTxPriorityQueue(ref, order))
114
+
115
+ /**
116
+ * Creates a `TxPriorityQueue` from an iterable of elements.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * import { Effect, Order, TxPriorityQueue } from "effect"
121
+ *
122
+ * const program = Effect.gen(function*() {
123
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [3, 1, 2])
124
+ * const first = yield* TxPriorityQueue.take(pq)
125
+ * console.log(first) // 1
126
+ * })
127
+ * ```
128
+ *
129
+ * @since 4.0.0
130
+ * @category constructors
131
+ */
132
+ export const fromIterable: {
133
+ /**
134
+ * Creates a `TxPriorityQueue` from an iterable of elements.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * import { Effect, Order, TxPriorityQueue } from "effect"
139
+ *
140
+ * const program = Effect.gen(function*() {
141
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [3, 1, 2])
142
+ * const first = yield* TxPriorityQueue.take(pq)
143
+ * console.log(first) // 1
144
+ * })
145
+ * ```
146
+ *
147
+ * @since 4.0.0
148
+ * @category constructors
149
+ */
150
+ <A>(order: Order<A>): (iterable: Iterable<A>) => Effect.Effect<TxPriorityQueue<A>, never, Effect.Transaction>
151
+ /**
152
+ * Creates a `TxPriorityQueue` from an iterable of elements.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * import { Effect, Order, TxPriorityQueue } from "effect"
157
+ *
158
+ * const program = Effect.gen(function*() {
159
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [3, 1, 2])
160
+ * const first = yield* TxPriorityQueue.take(pq)
161
+ * console.log(first) // 1
162
+ * })
163
+ * ```
164
+ *
165
+ * @since 4.0.0
166
+ * @category constructors
167
+ */
168
+ <A>(order: Order<A>, iterable: Iterable<A>): Effect.Effect<TxPriorityQueue<A>, never, Effect.Transaction>
169
+ } = dual(
170
+ 2,
171
+ <A>(order: Order<A>, iterable: Iterable<A>): Effect.Effect<TxPriorityQueue<A>, never, Effect.Transaction> => {
172
+ const arr = Array.from(iterable).sort((a, b) => order(a, b))
173
+ return Effect.map(
174
+ TxRef.make</**
175
+ * Creates a `TxPriorityQueue` from an iterable of elements.
176
+ *
177
+ * @example
178
+ * ```ts
179
+ * import { Effect, Order, TxPriorityQueue } from "effect"
180
+ *
181
+ * const program = Effect.gen(function*() {
182
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [3, 1, 2])
183
+ * const first = yield* TxPriorityQueue.take(pq)
184
+ * console.log(first) // 1
185
+ * })
186
+ * ```
187
+ *
188
+ * @since 4.0.0
189
+ * @category constructors
190
+ */
191
+ Chunk<A>>(C.fromIterable(arr)),
192
+ (ref) => makeTxPriorityQueue(ref, order)
193
+ );
194
+ }
195
+ )
196
+
197
+ /**
198
+ * Creates a `TxPriorityQueue` from variadic elements.
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * import { Effect, Order, TxPriorityQueue } from "effect"
203
+ *
204
+ * const program = Effect.gen(function*() {
205
+ * const pq = yield* TxPriorityQueue.make(Order.Number)(3, 1, 2)
206
+ * const first = yield* TxPriorityQueue.take(pq)
207
+ * console.log(first) // 1
208
+ * })
209
+ * ```
210
+ *
211
+ * @since 4.0.0
212
+ * @category constructors
213
+ */
214
+ export const make =
215
+ <A>(order: Order<A>) => (...elements: Array<A>): Effect.Effect<TxPriorityQueue<A>, never, Effect.Transaction> =>
216
+ fromIterable(order, elements)
217
+
218
+ /**
219
+ * Returns the number of elements in the queue.
220
+ *
221
+ * @example
222
+ * ```ts
223
+ * import { Effect, Order, TxPriorityQueue } from "effect"
224
+ *
225
+ * const program = Effect.gen(function*() {
226
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [1, 2, 3])
227
+ * const s = yield* TxPriorityQueue.size(pq)
228
+ * console.log(s) // 3
229
+ * })
230
+ * ```
231
+ *
232
+ * @since 4.0.0
233
+ * @category getters
234
+ */
235
+ export const size = <A>(self: TxPriorityQueue<A>): Effect.Effect<number, never, Effect.Transaction> =>
236
+ Effect.map(TxRef.get(self.ref), C.size)
237
+
238
+ /**
239
+ * Returns `true` if the queue is empty.
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * import { Effect, Order, TxPriorityQueue } from "effect"
244
+ *
245
+ * const program = Effect.gen(function*() {
246
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
247
+ * const empty = yield* TxPriorityQueue.isEmpty(pq)
248
+ * console.log(empty) // true
249
+ * })
250
+ * ```
251
+ *
252
+ * @since 4.0.0
253
+ * @category getters
254
+ */
255
+ export const isEmpty = <A>(self: TxPriorityQueue<A>): Effect.Effect<boolean, never, Effect.Transaction> =>
256
+ Effect.map(size(self), (n) => n === 0)
257
+
258
+ /**
259
+ * Returns `true` if the queue has at least one element.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * import { Effect, Order, TxPriorityQueue } from "effect"
264
+ *
265
+ * const program = Effect.gen(function*() {
266
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [1])
267
+ * const nonEmpty = yield* TxPriorityQueue.isNonEmpty(pq)
268
+ * console.log(nonEmpty) // true
269
+ * })
270
+ * ```
271
+ *
272
+ * @since 4.0.0
273
+ * @category getters
274
+ */
275
+ export const isNonEmpty = <A>(self: TxPriorityQueue<A>): Effect.Effect<boolean, never, Effect.Transaction> =>
276
+ Effect.map(size(self), (n) => n > 0)
277
+
278
+ /**
279
+ * Observes the smallest element without removing it. Retries if the queue is
280
+ * empty.
281
+ *
282
+ * @example
283
+ * ```ts
284
+ * import { Effect, Order, TxPriorityQueue } from "effect"
285
+ *
286
+ * const program = Effect.gen(function*() {
287
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [3, 1, 2])
288
+ * const top = yield* TxPriorityQueue.peek(pq)
289
+ * console.log(top) // 1
290
+ * })
291
+ * ```
292
+ *
293
+ * @since 4.0.0
294
+ * @category getters
295
+ */
296
+ export const peek = <A>(self: TxPriorityQueue<A>): Effect.Effect<A, never, Effect.Transaction> =>
297
+ Effect.gen(function*() {
298
+ const chunk = yield* TxRef.get(self.ref)
299
+ const head = C.head(chunk)
300
+ if (O.isNone(head)) {
301
+ return yield* Effect.retryTransaction
302
+ }
303
+ return head.value
304
+ })
305
+
306
+ /**
307
+ * Observes the smallest element without removing it. Returns `None` if the
308
+ * queue is empty.
309
+ *
310
+ * @example
311
+ * ```ts
312
+ * import { Effect, Option, Order, TxPriorityQueue } from "effect"
313
+ *
314
+ * const program = Effect.gen(function*() {
315
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
316
+ * const result = yield* TxPriorityQueue.peekOption(pq)
317
+ * console.log(Option.isNone(result)) // true
318
+ * })
319
+ * ```
320
+ *
321
+ * @since 4.0.0
322
+ * @category getters
323
+ */
324
+ export const peekOption = <A>(self: TxPriorityQueue<A>): Effect.Effect<Option<A>, never, Effect.Transaction> =>
325
+ Effect.map(TxRef.get(self.ref), C.head)
326
+
327
+ /**
328
+ * Inserts an element into the queue in sorted position.
329
+ *
330
+ * @example
331
+ * ```ts
332
+ * import { Effect, Order, TxPriorityQueue } from "effect"
333
+ *
334
+ * const program = Effect.gen(function*() {
335
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
336
+ * yield* TxPriorityQueue.offer(pq, 2)
337
+ * yield* TxPriorityQueue.offer(pq, 1)
338
+ * const first = yield* TxPriorityQueue.take(pq)
339
+ * console.log(first) // 1
340
+ * })
341
+ * ```
342
+ *
343
+ * @since 4.0.0
344
+ * @category mutations
345
+ */
346
+ export const offer: {
347
+ /**
348
+ * Inserts an element into the queue in sorted position.
349
+ *
350
+ * @example
351
+ * ```ts
352
+ * import { Effect, Order, TxPriorityQueue } from "effect"
353
+ *
354
+ * const program = Effect.gen(function*() {
355
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
356
+ * yield* TxPriorityQueue.offer(pq, 2)
357
+ * yield* TxPriorityQueue.offer(pq, 1)
358
+ * const first = yield* TxPriorityQueue.take(pq)
359
+ * console.log(first) // 1
360
+ * })
361
+ * ```
362
+ *
363
+ * @since 4.0.0
364
+ * @category mutations
365
+ */
366
+ <A>(value: A): (self: TxPriorityQueue<A>) => Effect.Effect<void, never, Effect.Transaction>
367
+ /**
368
+ * Inserts an element into the queue in sorted position.
369
+ *
370
+ * @example
371
+ * ```ts
372
+ * import { Effect, Order, TxPriorityQueue } from "effect"
373
+ *
374
+ * const program = Effect.gen(function*() {
375
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
376
+ * yield* TxPriorityQueue.offer(pq, 2)
377
+ * yield* TxPriorityQueue.offer(pq, 1)
378
+ * const first = yield* TxPriorityQueue.take(pq)
379
+ * console.log(first) // 1
380
+ * })
381
+ * ```
382
+ *
383
+ * @since 4.0.0
384
+ * @category mutations
385
+ */
386
+ <A>(self: TxPriorityQueue<A>, value: A): Effect.Effect<void, never, Effect.Transaction>
387
+ } = dual(
388
+ 2,
389
+ <A>(self: TxPriorityQueue<A>, value: A): Effect.Effect<void, never, Effect.Transaction> =>
390
+ TxRef.update(self.ref, (chunk) => insertSorted(chunk, value, self.ord))
391
+ )
392
+
393
+ /**
394
+ * Inserts all elements from an iterable into the queue.
395
+ *
396
+ * @example
397
+ * ```ts
398
+ * import { Effect, Order, TxPriorityQueue } from "effect"
399
+ *
400
+ * const program = Effect.gen(function*() {
401
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
402
+ * yield* TxPriorityQueue.offerAll(pq, [3, 1, 2])
403
+ * const first = yield* TxPriorityQueue.take(pq)
404
+ * console.log(first) // 1
405
+ * })
406
+ * ```
407
+ *
408
+ * @since 4.0.0
409
+ * @category mutations
410
+ */
411
+ export const offerAll: {
412
+ /**
413
+ * Inserts all elements from an iterable into the queue.
414
+ *
415
+ * @example
416
+ * ```ts
417
+ * import { Effect, Order, TxPriorityQueue } from "effect"
418
+ *
419
+ * const program = Effect.gen(function*() {
420
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
421
+ * yield* TxPriorityQueue.offerAll(pq, [3, 1, 2])
422
+ * const first = yield* TxPriorityQueue.take(pq)
423
+ * console.log(first) // 1
424
+ * })
425
+ * ```
426
+ *
427
+ * @since 4.0.0
428
+ * @category mutations
429
+ */
430
+ <A>(values: Iterable<A>): (self: TxPriorityQueue<A>) => Effect.Effect<void, never, Effect.Transaction>
431
+ /**
432
+ * Inserts all elements from an iterable into the queue.
433
+ *
434
+ * @example
435
+ * ```ts
436
+ * import { Effect, Order, TxPriorityQueue } from "effect"
437
+ *
438
+ * const program = Effect.gen(function*() {
439
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
440
+ * yield* TxPriorityQueue.offerAll(pq, [3, 1, 2])
441
+ * const first = yield* TxPriorityQueue.take(pq)
442
+ * console.log(first) // 1
443
+ * })
444
+ * ```
445
+ *
446
+ * @since 4.0.0
447
+ * @category mutations
448
+ */
449
+ <A>(self: TxPriorityQueue<A>, values: Iterable<A>): Effect.Effect<void, never, Effect.Transaction>
450
+ } = dual(
451
+ 2,
452
+ <A>(self: TxPriorityQueue<A>, values: Iterable<A>): Effect.Effect<void, never, Effect.Transaction> =>
453
+ TxRef.update(self.ref, (chunk) => {
454
+ const arr = [...C.toArray(chunk), ...values].sort((a, b) => self.ord(a, b))
455
+ return C.fromIterable(arr)
456
+ })
457
+ )
458
+
459
+ /**
460
+ * Takes the smallest element from the queue. Retries if the queue is empty.
461
+ *
462
+ * @example
463
+ * ```ts
464
+ * import { Effect, Order, TxPriorityQueue } from "effect"
465
+ *
466
+ * const program = Effect.gen(function*() {
467
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [3, 1, 2])
468
+ * const first = yield* TxPriorityQueue.take(pq)
469
+ * console.log(first) // 1
470
+ * })
471
+ * ```
472
+ *
473
+ * @since 4.0.0
474
+ * @category mutations
475
+ */
476
+ export const take = <A>(self: TxPriorityQueue<A>): Effect.Effect<A, never, Effect.Transaction> =>
477
+ Effect.gen(function*() {
478
+ const chunk = yield* TxRef.get(self.ref)
479
+ const head = C.head(chunk)
480
+ if (O.isNone(head)) {
481
+ return yield* Effect.retryTransaction
482
+ }
483
+ yield* TxRef.set(self.ref, C.drop(chunk, 1))
484
+ return head.value
485
+ })
486
+
487
+ /**
488
+ * Takes all elements from the queue, returning them in priority order.
489
+ *
490
+ * @example
491
+ * ```ts
492
+ * import { Effect, Order, TxPriorityQueue } from "effect"
493
+ *
494
+ * const program = Effect.gen(function*() {
495
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [3, 1, 2])
496
+ * const all = yield* TxPriorityQueue.takeAll(pq)
497
+ * console.log(all) // [1, 2, 3]
498
+ * })
499
+ * ```
500
+ *
501
+ * @since 4.0.0
502
+ * @category mutations
503
+ */
504
+ export const takeAll = <A>(self: TxPriorityQueue<A>): Effect.Effect<Array<A>, never, Effect.Transaction> =>
505
+ Effect.map(
506
+ TxRef.modify(self.ref, (chunk) => [chunk, C.empty()]),
507
+ C.toArray
508
+ )
509
+
510
+ /**
511
+ * Tries to take the smallest element. Returns `None` if the queue is empty.
512
+ *
513
+ * @example
514
+ * ```ts
515
+ * import { Effect, Option, Order, TxPriorityQueue } from "effect"
516
+ *
517
+ * const program = Effect.gen(function*() {
518
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
519
+ * const result = yield* TxPriorityQueue.takeOption(pq)
520
+ * console.log(Option.isNone(result)) // true
521
+ * })
522
+ * ```
523
+ *
524
+ * @since 4.0.0
525
+ * @category mutations
526
+ */
527
+ export const takeOption = <A>(self: TxPriorityQueue<A>): Effect.Effect<Option<A>, never, Effect.Transaction> =>
528
+ TxRef.modify(self.ref, (chunk) => {
529
+ const head = C.head(chunk)
530
+ if (O.isNone(head)) {
531
+ return [O.none<A>(), chunk]
532
+ }
533
+ return [O.some(head.value), C.drop(chunk, 1)]
534
+ })
535
+
536
+ /**
537
+ * Takes up to `n` elements from the queue in priority order.
538
+ *
539
+ * @example
540
+ * ```ts
541
+ * import { Effect, Order, TxPriorityQueue } from "effect"
542
+ *
543
+ * const program = Effect.gen(function*() {
544
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [5, 3, 1, 4, 2])
545
+ * const top2 = yield* TxPriorityQueue.takeUpTo(pq, 2)
546
+ * console.log(top2) // [1, 2]
547
+ * })
548
+ * ```
549
+ *
550
+ * @since 4.0.0
551
+ * @category mutations
552
+ */
553
+ export const takeUpTo: {
554
+ /**
555
+ * Takes up to `n` elements from the queue in priority order.
556
+ *
557
+ * @example
558
+ * ```ts
559
+ * import { Effect, Order, TxPriorityQueue } from "effect"
560
+ *
561
+ * const program = Effect.gen(function*() {
562
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [5, 3, 1, 4, 2])
563
+ * const top2 = yield* TxPriorityQueue.takeUpTo(pq, 2)
564
+ * console.log(top2) // [1, 2]
565
+ * })
566
+ * ```
567
+ *
568
+ * @since 4.0.0
569
+ * @category mutations
570
+ */
571
+ (n: number): <A>(self: TxPriorityQueue<A>) => Effect.Effect<Array<A>, never, Effect.Transaction>
572
+ /**
573
+ * Takes up to `n` elements from the queue in priority order.
574
+ *
575
+ * @example
576
+ * ```ts
577
+ * import { Effect, Order, TxPriorityQueue } from "effect"
578
+ *
579
+ * const program = Effect.gen(function*() {
580
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [5, 3, 1, 4, 2])
581
+ * const top2 = yield* TxPriorityQueue.takeUpTo(pq, 2)
582
+ * console.log(top2) // [1, 2]
583
+ * })
584
+ * ```
585
+ *
586
+ * @since 4.0.0
587
+ * @category mutations
588
+ */
589
+ <A>(self: TxPriorityQueue<A>, n: number): Effect.Effect<Array<A>, never, Effect.Transaction>
590
+ } = dual(
591
+ 2,
592
+ <A>(self: TxPriorityQueue<A>, n: number): Effect.Effect<Array<A>, never, Effect.Transaction> =>
593
+ Effect.map(
594
+ TxRef.modify(self.ref, (chunk) => {
595
+ const taken = C.take(chunk, n)
596
+ const rest = C.drop(chunk, n)
597
+ return [taken, rest]
598
+ }),
599
+ C.toArray
600
+ )
601
+ )
602
+
603
+ /**
604
+ * Removes elements matching the predicate.
605
+ *
606
+ * @example
607
+ * ```ts
608
+ * import { Effect, Order, TxPriorityQueue } from "effect"
609
+ *
610
+ * const program = Effect.gen(function*() {
611
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [1, 2, 3, 4, 5])
612
+ * yield* TxPriorityQueue.removeIf(pq, (n) => n % 2 === 0)
613
+ * const all = yield* TxPriorityQueue.takeAll(pq)
614
+ * console.log(all) // [1, 3, 5]
615
+ * })
616
+ * ```
617
+ *
618
+ * @since 4.0.0
619
+ * @category filtering
620
+ */
621
+ export const removeIf: {
622
+ /**
623
+ * Removes elements matching the predicate.
624
+ *
625
+ * @example
626
+ * ```ts
627
+ * import { Effect, Order, TxPriorityQueue } from "effect"
628
+ *
629
+ * const program = Effect.gen(function*() {
630
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [1, 2, 3, 4, 5])
631
+ * yield* TxPriorityQueue.removeIf(pq, (n) => n % 2 === 0)
632
+ * const all = yield* TxPriorityQueue.takeAll(pq)
633
+ * console.log(all) // [1, 3, 5]
634
+ * })
635
+ * ```
636
+ *
637
+ * @since 4.0.0
638
+ * @category filtering
639
+ */
640
+ <A>(predicate: Predicate<A>): (self: TxPriorityQueue<A>) => Effect.Effect<void, never, Effect.Transaction>
641
+ /**
642
+ * Removes elements matching the predicate.
643
+ *
644
+ * @example
645
+ * ```ts
646
+ * import { Effect, Order, TxPriorityQueue } from "effect"
647
+ *
648
+ * const program = Effect.gen(function*() {
649
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [1, 2, 3, 4, 5])
650
+ * yield* TxPriorityQueue.removeIf(pq, (n) => n % 2 === 0)
651
+ * const all = yield* TxPriorityQueue.takeAll(pq)
652
+ * console.log(all) // [1, 3, 5]
653
+ * })
654
+ * ```
655
+ *
656
+ * @since 4.0.0
657
+ * @category filtering
658
+ */
659
+ <A>(self: TxPriorityQueue<A>, predicate: Predicate<A>): Effect.Effect<void, never, Effect.Transaction>
660
+ } = dual(
661
+ 2,
662
+ <A>(self: TxPriorityQueue<A>, predicate: Predicate<A>): Effect.Effect<void, never, Effect.Transaction> =>
663
+ TxRef.update(self.ref, (chunk) => C.filter(chunk, (a) => !predicate(a)))
664
+ )
665
+
666
+ /**
667
+ * Retains only elements matching the predicate.
668
+ *
669
+ * @example
670
+ * ```ts
671
+ * import { Effect, Order, TxPriorityQueue } from "effect"
672
+ *
673
+ * const program = Effect.gen(function*() {
674
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [1, 2, 3, 4, 5])
675
+ * yield* TxPriorityQueue.retainIf(pq, (n) => n % 2 === 0)
676
+ * const all = yield* TxPriorityQueue.takeAll(pq)
677
+ * console.log(all) // [2, 4]
678
+ * })
679
+ * ```
680
+ *
681
+ * @since 4.0.0
682
+ * @category filtering
683
+ */
684
+ export const retainIf: {
685
+ /**
686
+ * Retains only elements matching the predicate.
687
+ *
688
+ * @example
689
+ * ```ts
690
+ * import { Effect, Order, TxPriorityQueue } from "effect"
691
+ *
692
+ * const program = Effect.gen(function*() {
693
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [1, 2, 3, 4, 5])
694
+ * yield* TxPriorityQueue.retainIf(pq, (n) => n % 2 === 0)
695
+ * const all = yield* TxPriorityQueue.takeAll(pq)
696
+ * console.log(all) // [2, 4]
697
+ * })
698
+ * ```
699
+ *
700
+ * @since 4.0.0
701
+ * @category filtering
702
+ */
703
+ <A>(predicate: Predicate<A>): (self: TxPriorityQueue<A>) => Effect.Effect<void, never, Effect.Transaction>
704
+ /**
705
+ * Retains only elements matching the predicate.
706
+ *
707
+ * @example
708
+ * ```ts
709
+ * import { Effect, Order, TxPriorityQueue } from "effect"
710
+ *
711
+ * const program = Effect.gen(function*() {
712
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [1, 2, 3, 4, 5])
713
+ * yield* TxPriorityQueue.retainIf(pq, (n) => n % 2 === 0)
714
+ * const all = yield* TxPriorityQueue.takeAll(pq)
715
+ * console.log(all) // [2, 4]
716
+ * })
717
+ * ```
718
+ *
719
+ * @since 4.0.0
720
+ * @category filtering
721
+ */
722
+ <A>(self: TxPriorityQueue<A>, predicate: Predicate<A>): Effect.Effect<void, never, Effect.Transaction>
723
+ } = dual(
724
+ 2,
725
+ <A>(self: TxPriorityQueue<A>, predicate: Predicate<A>): Effect.Effect<void, never, Effect.Transaction> =>
726
+ TxRef.update(self.ref, (chunk) => C.filter(chunk, predicate))
727
+ )
728
+
729
+ /**
730
+ * Returns all elements in priority order without removing them.
731
+ *
732
+ * @example
733
+ * ```ts
734
+ * import { Effect, Order, TxPriorityQueue } from "effect"
735
+ *
736
+ * const program = Effect.gen(function*() {
737
+ * const pq = yield* TxPriorityQueue.fromIterable(Order.Number, [3, 1, 2])
738
+ * const all = yield* TxPriorityQueue.toArray(pq)
739
+ * console.log(all) // [1, 2, 3]
740
+ * })
741
+ * ```
742
+ *
743
+ * @since 4.0.0
744
+ * @category conversions
745
+ */
746
+ export const toArray = <A>(self: TxPriorityQueue<A>): Effect.Effect<Array<A>, never, Effect.Transaction> =>
747
+ Effect.map(TxRef.get(self.ref), C.toArray)
748
+
749
+ /**
750
+ * Determines if the provided value is a `TxPriorityQueue`.
751
+ *
752
+ * @example
753
+ * ```ts
754
+ * import { Effect, Order, TxPriorityQueue } from "effect"
755
+ *
756
+ * const program = Effect.gen(function*() {
757
+ * const pq = yield* TxPriorityQueue.empty<number>(Order.Number)
758
+ * console.log(TxPriorityQueue.isTxPriorityQueue(pq)) // true
759
+ * console.log(TxPriorityQueue.isTxPriorityQueue("nope")) // false
760
+ * })
761
+ * ```
762
+ *
763
+ * @since 4.0.0
764
+ * @category guards
765
+ */
766
+ export const isTxPriorityQueue = (u: unknown): u is TxPriorityQueue<unknown> =>
767
+ typeof u === "object" && u !== null && TypeId in u