@typed/fx 2.0.0-beta.4 → 2.0.0-beta.6

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 (663) hide show
  1. package/README.md +34 -20
  2. package/dist/Fx/Fx.d.ts +362 -10
  3. package/dist/Fx/Fx.d.ts.map +1 -1
  4. package/dist/Fx/Fx.js +32 -1
  5. package/dist/Fx/TypeId.d.ts +49 -3
  6. package/dist/Fx/TypeId.d.ts.map +1 -1
  7. package/dist/Fx/TypeId.js +39 -2
  8. package/dist/Fx/combinators/additive.d.ts +156 -5
  9. package/dist/Fx/combinators/additive.d.ts.map +1 -1
  10. package/dist/Fx/combinators/additive.js +156 -5
  11. package/dist/Fx/combinators/catch.d.ts +195 -22
  12. package/dist/Fx/combinators/catch.d.ts.map +1 -1
  13. package/dist/Fx/combinators/catch.js +186 -16
  14. package/dist/Fx/combinators/causes.d.ts +24 -1
  15. package/dist/Fx/combinators/causes.d.ts.map +1 -1
  16. package/dist/Fx/combinators/causes.js +24 -1
  17. package/dist/Fx/combinators/changesWithEffect.d.ts +20 -0
  18. package/dist/Fx/combinators/changesWithEffect.d.ts.map +1 -1
  19. package/dist/Fx/combinators/changesWithEffect.js +20 -0
  20. package/dist/Fx/combinators/compact.d.ts +17 -0
  21. package/dist/Fx/combinators/compact.d.ts.map +1 -1
  22. package/dist/Fx/combinators/compact.js +17 -0
  23. package/dist/Fx/combinators/concatMap.d.ts +86 -0
  24. package/dist/Fx/combinators/concatMap.d.ts.map +1 -0
  25. package/dist/Fx/combinators/concatMap.js +86 -0
  26. package/dist/Fx/combinators/continueWith.d.ts +109 -1
  27. package/dist/Fx/combinators/continueWith.d.ts.map +1 -1
  28. package/dist/Fx/combinators/continueWith.js +109 -1
  29. package/dist/Fx/combinators/debounce.d.ts +34 -0
  30. package/dist/Fx/combinators/debounce.d.ts.map +1 -0
  31. package/dist/Fx/combinators/debounce.js +35 -0
  32. package/dist/Fx/combinators/delay.d.ts +34 -0
  33. package/dist/Fx/combinators/delay.d.ts.map +1 -0
  34. package/dist/Fx/combinators/delay.js +31 -0
  35. package/dist/Fx/combinators/dropUntil.d.ts +42 -0
  36. package/dist/Fx/combinators/dropUntil.d.ts.map +1 -1
  37. package/dist/Fx/combinators/dropUntil.js +42 -0
  38. package/dist/Fx/combinators/during.d.ts +38 -0
  39. package/dist/Fx/combinators/during.d.ts.map +1 -0
  40. package/dist/Fx/combinators/during.js +85 -0
  41. package/dist/Fx/combinators/ensuring.d.ts +33 -3
  42. package/dist/Fx/combinators/ensuring.d.ts.map +1 -1
  43. package/dist/Fx/combinators/ensuring.js +33 -3
  44. package/dist/Fx/combinators/exhaustLatestMap.d.ts +40 -4
  45. package/dist/Fx/combinators/exhaustLatestMap.d.ts.map +1 -1
  46. package/dist/Fx/combinators/exhaustLatestMap.js +40 -4
  47. package/dist/Fx/combinators/exhaustLatestMapEffect.d.ts +39 -3
  48. package/dist/Fx/combinators/exhaustLatestMapEffect.d.ts.map +1 -1
  49. package/dist/Fx/combinators/exhaustLatestMapEffect.js +39 -3
  50. package/dist/Fx/combinators/exhaustMap.d.ts +37 -0
  51. package/dist/Fx/combinators/exhaustMap.d.ts.map +1 -1
  52. package/dist/Fx/combinators/exhaustMap.js +37 -0
  53. package/dist/Fx/combinators/exhaustMapEffect.d.ts +32 -0
  54. package/dist/Fx/combinators/exhaustMapEffect.d.ts.map +1 -1
  55. package/dist/Fx/combinators/exhaustMapEffect.js +32 -0
  56. package/dist/Fx/combinators/exit.d.ts +22 -1
  57. package/dist/Fx/combinators/exit.d.ts.map +1 -1
  58. package/dist/Fx/combinators/exit.js +22 -1
  59. package/dist/Fx/combinators/filter.d.ts +16 -0
  60. package/dist/Fx/combinators/filter.d.ts.map +1 -1
  61. package/dist/Fx/combinators/filter.js +16 -0
  62. package/dist/Fx/combinators/filterEffect.d.ts +21 -0
  63. package/dist/Fx/combinators/filterEffect.d.ts.map +1 -1
  64. package/dist/Fx/combinators/filterEffect.js +21 -0
  65. package/dist/Fx/combinators/filterMap.d.ts +19 -0
  66. package/dist/Fx/combinators/filterMap.d.ts.map +1 -1
  67. package/dist/Fx/combinators/filterMap.js +19 -0
  68. package/dist/Fx/combinators/filterMapEffect.d.ts +20 -0
  69. package/dist/Fx/combinators/filterMapEffect.d.ts.map +1 -1
  70. package/dist/Fx/combinators/filterMapEffect.js +20 -0
  71. package/dist/Fx/combinators/filterMapLoop.d.ts +19 -0
  72. package/dist/Fx/combinators/filterMapLoop.d.ts.map +1 -1
  73. package/dist/Fx/combinators/filterMapLoop.js +19 -0
  74. package/dist/Fx/combinators/filterMapLoopCause.d.ts +20 -0
  75. package/dist/Fx/combinators/filterMapLoopCause.d.ts.map +1 -1
  76. package/dist/Fx/combinators/filterMapLoopCause.js +20 -0
  77. package/dist/Fx/combinators/filterMapLoopCauseEffect.d.ts +20 -0
  78. package/dist/Fx/combinators/filterMapLoopCauseEffect.d.ts.map +1 -1
  79. package/dist/Fx/combinators/filterMapLoopCauseEffect.js +20 -0
  80. package/dist/Fx/combinators/filterMapLoopEffect.d.ts +20 -0
  81. package/dist/Fx/combinators/filterMapLoopEffect.d.ts.map +1 -1
  82. package/dist/Fx/combinators/filterMapLoopEffect.js +20 -0
  83. package/dist/Fx/combinators/flatMap.d.ts +48 -3
  84. package/dist/Fx/combinators/flatMap.d.ts.map +1 -1
  85. package/dist/Fx/combinators/flatMap.js +35 -2
  86. package/dist/Fx/combinators/flatMapConcurrently.d.ts +50 -3
  87. package/dist/Fx/combinators/flatMapConcurrently.d.ts.map +1 -1
  88. package/dist/Fx/combinators/flatMapConcurrently.js +47 -1
  89. package/dist/Fx/combinators/flatMapConcurrentlyEffect.d.ts +47 -3
  90. package/dist/Fx/combinators/flatMapConcurrentlyEffect.d.ts.map +1 -1
  91. package/dist/Fx/combinators/flatMapConcurrentlyEffect.js +39 -1
  92. package/dist/Fx/combinators/flatMapEffect.d.ts +44 -2
  93. package/dist/Fx/combinators/flatMapEffect.d.ts.map +1 -1
  94. package/dist/Fx/combinators/flatMapEffect.js +31 -1
  95. package/dist/Fx/combinators/flip.d.ts +33 -1
  96. package/dist/Fx/combinators/flip.d.ts.map +1 -1
  97. package/dist/Fx/combinators/flip.js +33 -1
  98. package/dist/Fx/combinators/gen.d.ts +28 -9
  99. package/dist/Fx/combinators/gen.d.ts.map +1 -1
  100. package/dist/Fx/combinators/gen.js +27 -3
  101. package/dist/Fx/combinators/genScoped.d.ts +32 -9
  102. package/dist/Fx/combinators/genScoped.d.ts.map +1 -1
  103. package/dist/Fx/combinators/genScoped.js +30 -2
  104. package/dist/Fx/combinators/grouped.d.ts +89 -0
  105. package/dist/Fx/combinators/grouped.d.ts.map +1 -0
  106. package/dist/Fx/combinators/grouped.js +146 -0
  107. package/dist/Fx/combinators/index.d.ts +35 -0
  108. package/dist/Fx/combinators/index.d.ts.map +1 -1
  109. package/dist/Fx/combinators/index.js +35 -0
  110. package/dist/Fx/combinators/keyed.d.ts +121 -7
  111. package/dist/Fx/combinators/keyed.d.ts.map +1 -1
  112. package/dist/Fx/combinators/keyed.js +64 -6
  113. package/dist/Fx/combinators/loop.d.ts +16 -0
  114. package/dist/Fx/combinators/loop.d.ts.map +1 -1
  115. package/dist/Fx/combinators/loop.js +16 -0
  116. package/dist/Fx/combinators/loopCause.d.ts +17 -0
  117. package/dist/Fx/combinators/loopCause.d.ts.map +1 -1
  118. package/dist/Fx/combinators/loopCause.js +17 -0
  119. package/dist/Fx/combinators/loopCauseEffect.d.ts +18 -0
  120. package/dist/Fx/combinators/loopCauseEffect.d.ts.map +1 -1
  121. package/dist/Fx/combinators/loopCauseEffect.js +18 -0
  122. package/dist/Fx/combinators/loopEffect.d.ts +23 -0
  123. package/dist/Fx/combinators/loopEffect.d.ts.map +1 -1
  124. package/dist/Fx/combinators/loopEffect.js +23 -0
  125. package/dist/Fx/combinators/map.d.ts +45 -0
  126. package/dist/Fx/combinators/map.d.ts.map +1 -1
  127. package/dist/Fx/combinators/map.js +42 -0
  128. package/dist/Fx/combinators/mapBoth.d.ts +19 -0
  129. package/dist/Fx/combinators/mapBoth.d.ts.map +1 -1
  130. package/dist/Fx/combinators/mapBoth.js +19 -0
  131. package/dist/Fx/combinators/mapEffect.d.ts +24 -0
  132. package/dist/Fx/combinators/mapEffect.d.ts.map +1 -1
  133. package/dist/Fx/combinators/mapEffect.js +24 -0
  134. package/dist/Fx/combinators/mapError.d.ts +20 -4
  135. package/dist/Fx/combinators/mapError.d.ts.map +1 -1
  136. package/dist/Fx/combinators/mapError.js +20 -4
  137. package/dist/Fx/combinators/mergeAll.d.ts +34 -0
  138. package/dist/Fx/combinators/mergeAll.d.ts.map +1 -1
  139. package/dist/Fx/combinators/mergeAll.js +34 -0
  140. package/dist/Fx/combinators/mergeOrdered.d.ts +35 -3
  141. package/dist/Fx/combinators/mergeOrdered.d.ts.map +1 -1
  142. package/dist/Fx/combinators/mergeOrdered.js +35 -3
  143. package/dist/Fx/combinators/onError.d.ts +33 -3
  144. package/dist/Fx/combinators/onError.d.ts.map +1 -1
  145. package/dist/Fx/combinators/onError.js +33 -3
  146. package/dist/Fx/combinators/onExit.d.ts +28 -2
  147. package/dist/Fx/combinators/onExit.d.ts.map +1 -1
  148. package/dist/Fx/combinators/onExit.js +28 -2
  149. package/dist/Fx/combinators/onInterrupt.d.ts +48 -3
  150. package/dist/Fx/combinators/onInterrupt.d.ts.map +1 -1
  151. package/dist/Fx/combinators/onInterrupt.js +48 -3
  152. package/dist/Fx/combinators/pairwise.d.ts +28 -0
  153. package/dist/Fx/combinators/pairwise.d.ts.map +1 -0
  154. package/dist/Fx/combinators/pairwise.js +31 -0
  155. package/dist/Fx/combinators/provide.d.ts +130 -9
  156. package/dist/Fx/combinators/provide.d.ts.map +1 -1
  157. package/dist/Fx/combinators/provide.js +130 -9
  158. package/dist/Fx/combinators/race.d.ts +92 -0
  159. package/dist/Fx/combinators/race.d.ts.map +1 -0
  160. package/dist/Fx/combinators/race.js +149 -0
  161. package/dist/Fx/combinators/repeat.d.ts +41 -0
  162. package/dist/Fx/combinators/repeat.d.ts.map +1 -0
  163. package/dist/Fx/combinators/repeat.js +57 -0
  164. package/dist/Fx/combinators/result.d.ts +22 -0
  165. package/dist/Fx/combinators/result.d.ts.map +1 -1
  166. package/dist/Fx/combinators/result.js +26 -12
  167. package/dist/Fx/combinators/retry.d.ts +53 -0
  168. package/dist/Fx/combinators/retry.d.ts.map +1 -0
  169. package/dist/Fx/combinators/retry.js +71 -0
  170. package/dist/Fx/combinators/sample.d.ts +31 -0
  171. package/dist/Fx/combinators/sample.d.ts.map +1 -0
  172. package/dist/Fx/combinators/sample.js +42 -0
  173. package/dist/Fx/combinators/scan.d.ts +39 -0
  174. package/dist/Fx/combinators/scan.d.ts.map +1 -1
  175. package/dist/Fx/combinators/scan.js +39 -0
  176. package/dist/Fx/combinators/since.d.ts +30 -0
  177. package/dist/Fx/combinators/since.d.ts.map +1 -0
  178. package/dist/Fx/combinators/since.js +39 -0
  179. package/dist/Fx/combinators/skip.d.ts +31 -0
  180. package/dist/Fx/combinators/skip.d.ts.map +1 -1
  181. package/dist/Fx/combinators/skip.js +31 -0
  182. package/dist/Fx/combinators/skipRepeats.d.ts +16 -0
  183. package/dist/Fx/combinators/skipRepeats.d.ts.map +1 -1
  184. package/dist/Fx/combinators/skipRepeats.js +16 -0
  185. package/dist/Fx/combinators/skipRepeatsWith.d.ts +17 -0
  186. package/dist/Fx/combinators/skipRepeatsWith.d.ts.map +1 -1
  187. package/dist/Fx/combinators/skipRepeatsWith.js +17 -0
  188. package/dist/Fx/combinators/skipWhile.d.ts +68 -0
  189. package/dist/Fx/combinators/skipWhile.d.ts.map +1 -1
  190. package/dist/Fx/combinators/skipWhile.js +68 -0
  191. package/dist/Fx/combinators/slice.d.ts +82 -0
  192. package/dist/Fx/combinators/slice.d.ts.map +1 -1
  193. package/dist/Fx/combinators/slice.js +33 -0
  194. package/dist/Fx/combinators/switchMap.d.ts +38 -0
  195. package/dist/Fx/combinators/switchMap.d.ts.map +1 -1
  196. package/dist/Fx/combinators/switchMap.js +38 -0
  197. package/dist/Fx/combinators/switchMapEffect.d.ts +37 -0
  198. package/dist/Fx/combinators/switchMapEffect.d.ts.map +1 -1
  199. package/dist/Fx/combinators/switchMapEffect.js +37 -0
  200. package/dist/Fx/combinators/take.d.ts +32 -0
  201. package/dist/Fx/combinators/take.d.ts.map +1 -1
  202. package/dist/Fx/combinators/take.js +32 -0
  203. package/dist/Fx/combinators/takeUntil.d.ts +46 -0
  204. package/dist/Fx/combinators/takeUntil.d.ts.map +1 -1
  205. package/dist/Fx/combinators/takeUntil.js +46 -0
  206. package/dist/Fx/combinators/takeWhile.d.ts +31 -0
  207. package/dist/Fx/combinators/takeWhile.d.ts.map +1 -1
  208. package/dist/Fx/combinators/takeWhile.js +31 -0
  209. package/dist/Fx/combinators/tapEffect.d.ts +16 -0
  210. package/dist/Fx/combinators/tapEffect.d.ts.map +1 -1
  211. package/dist/Fx/combinators/tapEffect.js +16 -0
  212. package/dist/Fx/combinators/throttle.d.ts +79 -0
  213. package/dist/Fx/combinators/throttle.d.ts.map +1 -0
  214. package/dist/Fx/combinators/throttle.js +99 -0
  215. package/dist/Fx/combinators/timeout.d.ts +76 -0
  216. package/dist/Fx/combinators/timeout.d.ts.map +1 -0
  217. package/dist/Fx/combinators/timeout.js +110 -0
  218. package/dist/Fx/combinators/tuple.d.ts +30 -0
  219. package/dist/Fx/combinators/tuple.d.ts.map +1 -1
  220. package/dist/Fx/combinators/tuple.js +34 -3
  221. package/dist/Fx/combinators/until.d.ts +27 -0
  222. package/dist/Fx/combinators/until.d.ts.map +1 -0
  223. package/dist/Fx/combinators/until.js +56 -0
  224. package/dist/Fx/combinators/unwrap.d.ts +29 -0
  225. package/dist/Fx/combinators/unwrap.d.ts.map +1 -1
  226. package/dist/Fx/combinators/unwrap.js +29 -0
  227. package/dist/Fx/combinators/unwrapScoped.d.ts +33 -0
  228. package/dist/Fx/combinators/unwrapScoped.d.ts.map +1 -1
  229. package/dist/Fx/combinators/unwrapScoped.js +33 -0
  230. package/dist/Fx/combinators/when.d.ts +35 -0
  231. package/dist/Fx/combinators/when.d.ts.map +1 -1
  232. package/dist/Fx/combinators/when.js +36 -1
  233. package/dist/Fx/combinators/withLatestFrom.d.ts +58 -0
  234. package/dist/Fx/combinators/withLatestFrom.d.ts.map +1 -0
  235. package/dist/Fx/combinators/withLatestFrom.js +68 -0
  236. package/dist/Fx/combinators/withSpan.d.ts +35 -1
  237. package/dist/Fx/combinators/withSpan.d.ts.map +1 -1
  238. package/dist/Fx/combinators/withSpan.js +34 -3
  239. package/dist/Fx/combinators/zip.d.ts +60 -0
  240. package/dist/Fx/combinators/zip.d.ts.map +1 -1
  241. package/dist/Fx/combinators/zip.js +60 -0
  242. package/dist/Fx/constructors/at.d.ts +20 -0
  243. package/dist/Fx/constructors/at.d.ts.map +1 -1
  244. package/dist/Fx/constructors/at.js +20 -0
  245. package/dist/Fx/constructors/die.d.ts +21 -0
  246. package/dist/Fx/constructors/die.d.ts.map +1 -1
  247. package/dist/Fx/constructors/die.js +21 -0
  248. package/dist/Fx/constructors/empty.d.ts +18 -0
  249. package/dist/Fx/constructors/empty.d.ts.map +1 -1
  250. package/dist/Fx/constructors/empty.js +18 -0
  251. package/dist/Fx/constructors/fail.d.ts +21 -0
  252. package/dist/Fx/constructors/fail.d.ts.map +1 -1
  253. package/dist/Fx/constructors/fail.js +21 -0
  254. package/dist/Fx/constructors/failCause.d.ts +22 -0
  255. package/dist/Fx/constructors/failCause.d.ts.map +1 -1
  256. package/dist/Fx/constructors/failCause.js +22 -0
  257. package/dist/Fx/constructors/fn.d.ts +113 -13
  258. package/dist/Fx/constructors/fn.d.ts.map +1 -1
  259. package/dist/Fx/constructors/fn.js +36 -0
  260. package/dist/Fx/constructors/fromEffect.d.ts +43 -1
  261. package/dist/Fx/constructors/fromEffect.d.ts.map +1 -1
  262. package/dist/Fx/constructors/fromEffect.js +43 -1
  263. package/dist/Fx/constructors/fromFailures.d.ts +24 -0
  264. package/dist/Fx/constructors/fromFailures.d.ts.map +1 -1
  265. package/dist/Fx/constructors/fromFailures.js +24 -0
  266. package/dist/Fx/constructors/fromIterable.d.ts +22 -0
  267. package/dist/Fx/constructors/fromIterable.d.ts.map +1 -1
  268. package/dist/Fx/constructors/fromIterable.js +22 -0
  269. package/dist/Fx/constructors/fromSchedule.d.ts +23 -0
  270. package/dist/Fx/constructors/fromSchedule.d.ts.map +1 -1
  271. package/dist/Fx/constructors/fromSchedule.js +25 -2
  272. package/dist/Fx/constructors/index.d.ts +2 -1
  273. package/dist/Fx/constructors/index.d.ts.map +1 -1
  274. package/dist/Fx/constructors/index.js +2 -1
  275. package/dist/Fx/constructors/interrupt.d.ts +21 -1
  276. package/dist/Fx/constructors/interrupt.d.ts.map +1 -1
  277. package/dist/Fx/constructors/interrupt.js +21 -1
  278. package/dist/Fx/constructors/make.d.ts +146 -1
  279. package/dist/Fx/constructors/make.d.ts.map +1 -1
  280. package/dist/Fx/constructors/make.js +54 -3
  281. package/dist/Fx/constructors/periodic.d.ts +29 -0
  282. package/dist/Fx/constructors/periodic.d.ts.map +1 -1
  283. package/dist/Fx/constructors/periodic.js +29 -0
  284. package/dist/Fx/constructors/succeed.d.ts +75 -3
  285. package/dist/Fx/constructors/succeed.d.ts.map +1 -1
  286. package/dist/Fx/constructors/succeed.js +75 -3
  287. package/dist/Fx/constructors/suspend.d.ts +28 -0
  288. package/dist/Fx/constructors/suspend.d.ts.map +1 -1
  289. package/dist/Fx/constructors/suspend.js +28 -0
  290. package/dist/Fx/constructors/sync.d.ts +32 -0
  291. package/dist/Fx/constructors/sync.d.ts.map +1 -0
  292. package/dist/Fx/constructors/sync.js +32 -0
  293. package/dist/Fx/internal/DeferredRef.d.ts +259 -7
  294. package/dist/Fx/internal/DeferredRef.d.ts.map +1 -1
  295. package/dist/Fx/internal/DeferredRef.js +260 -9
  296. package/dist/Fx/internal/UnionToTuple.d.ts +22 -0
  297. package/dist/Fx/internal/UnionToTuple.d.ts.map +1 -1
  298. package/dist/Fx/internal/diff.d.ts +586 -0
  299. package/dist/Fx/internal/diff.d.ts.map +1 -1
  300. package/dist/Fx/internal/diff.js +120 -9
  301. package/dist/Fx/internal/effectableEvaluateKey.d.ts +30 -0
  302. package/dist/Fx/internal/effectableEvaluateKey.d.ts.map +1 -0
  303. package/dist/Fx/internal/effectableEvaluateKey.js +29 -0
  304. package/dist/Fx/internal/effectableWithOverride.d.ts +55 -0
  305. package/dist/Fx/internal/effectableWithOverride.d.ts.map +1 -0
  306. package/dist/Fx/internal/effectableWithOverride.js +46 -0
  307. package/dist/Fx/internal/equivalence.d.ts +29 -0
  308. package/dist/Fx/internal/equivalence.d.ts.map +1 -1
  309. package/dist/Fx/internal/equivalence.js +29 -0
  310. package/dist/Fx/internal/multicast.d.ts +145 -2
  311. package/dist/Fx/internal/multicast.d.ts.map +1 -1
  312. package/dist/Fx/internal/multicast.js +146 -4
  313. package/dist/Fx/internal/ring-buffer.d.ts +149 -0
  314. package/dist/Fx/internal/ring-buffer.d.ts.map +1 -1
  315. package/dist/Fx/internal/ring-buffer.js +155 -0
  316. package/dist/Fx/internal/scope.d.ts +184 -0
  317. package/dist/Fx/internal/scope.d.ts.map +1 -1
  318. package/dist/Fx/internal/scope.js +150 -0
  319. package/dist/Fx/internal/yieldable.d.ts +117 -2
  320. package/dist/Fx/internal/yieldable.d.ts.map +1 -1
  321. package/dist/Fx/internal/yieldable.js +86 -3
  322. package/dist/Fx/run/collect.d.ts +89 -0
  323. package/dist/Fx/run/collect.d.ts.map +1 -1
  324. package/dist/Fx/run/collect.js +90 -1
  325. package/dist/Fx/run/first.d.ts +22 -0
  326. package/dist/Fx/run/first.d.ts.map +1 -1
  327. package/dist/Fx/run/first.js +22 -0
  328. package/dist/Fx/run/fork.d.ts +50 -0
  329. package/dist/Fx/run/fork.d.ts.map +1 -1
  330. package/dist/Fx/run/fork.js +50 -0
  331. package/dist/Fx/run/observe.d.ts +106 -0
  332. package/dist/Fx/run/observe.d.ts.map +1 -1
  333. package/dist/Fx/run/observe.js +106 -0
  334. package/dist/Fx/run/runPromise.d.ts +43 -0
  335. package/dist/Fx/run/runPromise.d.ts.map +1 -1
  336. package/dist/Fx/run/runPromise.js +43 -0
  337. package/dist/Fx/stream.d.ts +92 -0
  338. package/dist/Fx/stream.d.ts.map +1 -1
  339. package/dist/Fx/stream.js +60 -1
  340. package/dist/Push/Push.d.ts +1216 -91
  341. package/dist/Push/Push.d.ts.map +1 -1
  342. package/dist/Push/Push.js +1012 -83
  343. package/dist/RefArray.d.ts +2 -0
  344. package/dist/RefArray.d.ts.map +1 -0
  345. package/dist/RefArray.js +1 -0
  346. package/dist/RefBigDecimal.d.ts +2 -0
  347. package/dist/RefBigDecimal.d.ts.map +1 -0
  348. package/dist/RefBigDecimal.js +1 -0
  349. package/dist/RefBigInt.d.ts +2 -0
  350. package/dist/RefBigInt.d.ts.map +1 -0
  351. package/dist/RefBigInt.js +1 -0
  352. package/dist/RefBoolean.d.ts +2 -0
  353. package/dist/RefBoolean.d.ts.map +1 -0
  354. package/dist/RefBoolean.js +1 -0
  355. package/dist/RefCause.d.ts +2 -0
  356. package/dist/RefCause.d.ts.map +1 -0
  357. package/dist/RefCause.js +1 -0
  358. package/dist/RefChunk.d.ts +2 -0
  359. package/dist/RefChunk.d.ts.map +1 -0
  360. package/dist/RefChunk.js +1 -0
  361. package/dist/RefDateTime.d.ts +2 -0
  362. package/dist/RefDateTime.d.ts.map +1 -0
  363. package/dist/RefDateTime.js +1 -0
  364. package/dist/RefDuration.d.ts +2 -0
  365. package/dist/RefDuration.d.ts.map +1 -0
  366. package/dist/RefDuration.js +1 -0
  367. package/dist/RefGraph.d.ts +2 -0
  368. package/dist/RefGraph.d.ts.map +1 -0
  369. package/dist/RefGraph.js +1 -0
  370. package/dist/RefHashMap.d.ts +2 -0
  371. package/dist/RefHashMap.d.ts.map +1 -0
  372. package/dist/RefHashMap.js +1 -0
  373. package/dist/RefHashRing.d.ts +2 -0
  374. package/dist/RefHashRing.d.ts.map +1 -0
  375. package/dist/RefHashRing.js +1 -0
  376. package/dist/RefHashSet.d.ts +2 -0
  377. package/dist/RefHashSet.d.ts.map +1 -0
  378. package/dist/RefHashSet.js +1 -0
  379. package/dist/RefIterable.d.ts +2 -0
  380. package/dist/RefIterable.d.ts.map +1 -0
  381. package/dist/RefIterable.js +1 -0
  382. package/dist/RefOption.d.ts +2 -0
  383. package/dist/RefOption.d.ts.map +1 -0
  384. package/dist/RefOption.js +1 -0
  385. package/dist/RefRecord.d.ts +2 -0
  386. package/dist/RefRecord.d.ts.map +1 -0
  387. package/dist/RefRecord.js +1 -0
  388. package/dist/RefResult.d.ts +2 -0
  389. package/dist/RefResult.d.ts.map +1 -0
  390. package/dist/RefResult.js +1 -0
  391. package/dist/RefString.d.ts +2 -0
  392. package/dist/RefString.d.ts.map +1 -0
  393. package/dist/RefString.js +1 -0
  394. package/dist/RefStruct.d.ts +2 -0
  395. package/dist/RefStruct.d.ts.map +1 -0
  396. package/dist/RefStruct.js +1 -0
  397. package/dist/RefSubject/Hydration.d.ts +504 -0
  398. package/dist/RefSubject/Hydration.d.ts.map +1 -0
  399. package/dist/RefSubject/Hydration.js +300 -0
  400. package/dist/RefSubject/RefArray.d.ts +351 -4
  401. package/dist/RefSubject/RefArray.d.ts.map +1 -1
  402. package/dist/RefSubject/RefArray.js +339 -3
  403. package/dist/RefSubject/RefBigDecimal.d.ts +221 -1
  404. package/dist/RefSubject/RefBigDecimal.d.ts.map +1 -1
  405. package/dist/RefSubject/RefBigDecimal.js +210 -1
  406. package/dist/RefSubject/RefBigInt.d.ts +166 -1
  407. package/dist/RefSubject/RefBigInt.d.ts.map +1 -1
  408. package/dist/RefSubject/RefBigInt.js +155 -1
  409. package/dist/RefSubject/RefBoolean.d.ts +166 -1
  410. package/dist/RefSubject/RefBoolean.d.ts.map +1 -1
  411. package/dist/RefSubject/RefBoolean.js +155 -1
  412. package/dist/RefSubject/RefCause.d.ts +122 -1
  413. package/dist/RefSubject/RefCause.d.ts.map +1 -1
  414. package/dist/RefSubject/RefCause.js +111 -1
  415. package/dist/RefSubject/RefChunk.d.ts +406 -1
  416. package/dist/RefSubject/RefChunk.d.ts.map +1 -1
  417. package/dist/RefSubject/RefChunk.js +394 -0
  418. package/dist/RefSubject/RefDateTime.d.ts +122 -1
  419. package/dist/RefSubject/RefDateTime.d.ts.map +1 -1
  420. package/dist/RefSubject/RefDateTime.js +111 -1
  421. package/dist/RefSubject/RefDuration.d.ts +155 -1
  422. package/dist/RefSubject/RefDuration.d.ts.map +1 -1
  423. package/dist/RefSubject/RefDuration.js +144 -1
  424. package/dist/RefSubject/RefGraph.d.ts +320 -6
  425. package/dist/RefSubject/RefGraph.d.ts.map +1 -1
  426. package/dist/RefSubject/RefGraph.js +303 -0
  427. package/dist/RefSubject/RefHashMap.d.ts +296 -1
  428. package/dist/RefSubject/RefHashMap.d.ts.map +1 -1
  429. package/dist/RefSubject/RefHashMap.js +284 -0
  430. package/dist/RefSubject/RefHashRing.d.ts +146 -2
  431. package/dist/RefSubject/RefHashRing.d.ts.map +1 -1
  432. package/dist/RefSubject/RefHashRing.js +133 -0
  433. package/dist/RefSubject/RefHashSet.d.ts +230 -1
  434. package/dist/RefSubject/RefHashSet.d.ts.map +1 -1
  435. package/dist/RefSubject/RefHashSet.js +218 -0
  436. package/dist/RefSubject/RefIterable.d.ts +327 -1
  437. package/dist/RefSubject/RefIterable.d.ts.map +1 -1
  438. package/dist/RefSubject/RefIterable.js +315 -0
  439. package/dist/RefSubject/RefOption.d.ts +144 -1
  440. package/dist/RefSubject/RefOption.d.ts.map +1 -1
  441. package/dist/RefSubject/RefOption.js +133 -1
  442. package/dist/RefSubject/RefRecord.d.ts +339 -1
  443. package/dist/RefSubject/RefRecord.d.ts.map +1 -1
  444. package/dist/RefSubject/RefRecord.js +327 -0
  445. package/dist/RefSubject/RefResult.d.ts +155 -23
  446. package/dist/RefSubject/RefResult.d.ts.map +1 -1
  447. package/dist/RefSubject/RefResult.js +122 -1
  448. package/dist/RefSubject/RefString.d.ts +188 -1
  449. package/dist/RefSubject/RefString.d.ts.map +1 -1
  450. package/dist/RefSubject/RefString.js +177 -1
  451. package/dist/RefSubject/RefStruct.d.ts +156 -1
  452. package/dist/RefSubject/RefStruct.d.ts.map +1 -1
  453. package/dist/RefSubject/RefStruct.js +145 -1
  454. package/dist/RefSubject/RefSubject.d.ts +1166 -13
  455. package/dist/RefSubject/RefSubject.d.ts.map +1 -1
  456. package/dist/RefSubject/RefSubject.js +843 -51
  457. package/dist/RefSubject/RefTrie.d.ts +290 -4
  458. package/dist/RefSubject/RefTrie.d.ts.map +1 -1
  459. package/dist/RefSubject/RefTrie.js +278 -3
  460. package/dist/RefSubject/RefTuple.d.ts +117 -1
  461. package/dist/RefSubject/RefTuple.d.ts.map +1 -1
  462. package/dist/RefSubject/RefTuple.js +106 -1
  463. package/dist/RefSubject/index.d.ts +1 -0
  464. package/dist/RefSubject/index.d.ts.map +1 -1
  465. package/dist/RefSubject/index.js +1 -0
  466. package/dist/RefSubject.d.ts +2 -1
  467. package/dist/RefSubject.d.ts.map +1 -1
  468. package/dist/RefSubject.js +2 -1
  469. package/dist/RefTrie.d.ts +2 -0
  470. package/dist/RefTrie.d.ts.map +1 -0
  471. package/dist/RefTrie.js +1 -0
  472. package/dist/RefTuple.d.ts +2 -0
  473. package/dist/RefTuple.d.ts.map +1 -0
  474. package/dist/RefTuple.js +1 -0
  475. package/dist/Sink/Sink.d.ts +381 -10
  476. package/dist/Sink/Sink.d.ts.map +1 -1
  477. package/dist/Sink/Sink.js +49 -2
  478. package/dist/Sink/combinators.d.ts +731 -2
  479. package/dist/Sink/combinators.d.ts.map +1 -1
  480. package/dist/Sink/combinators.js +706 -4
  481. package/dist/Subject/Subject.d.ts +1013 -19
  482. package/dist/Subject/Subject.d.ts.map +1 -1
  483. package/dist/Subject/Subject.js +901 -36
  484. package/dist/Versioned/Versioned.d.ts +424 -7
  485. package/dist/Versioned/Versioned.d.ts.map +1 -1
  486. package/dist/Versioned/Versioned.js +238 -12
  487. package/dist/index.d.ts +22 -3
  488. package/dist/index.d.ts.map +1 -1
  489. package/dist/index.js +22 -3
  490. package/package.json +123 -14
  491. package/dist/Fx/constructors/fromYieldable.d.ts +0 -12
  492. package/dist/Fx/constructors/fromYieldable.d.ts.map +0 -1
  493. package/dist/Fx/constructors/fromYieldable.js +0 -12
  494. package/src/Fx/Fx.ts +0 -164
  495. package/src/Fx/TypeId.ts +0 -25
  496. package/src/Fx/combinators/additive.ts +0 -142
  497. package/src/Fx/combinators/catch.ts +0 -416
  498. package/src/Fx/combinators/causes.ts +0 -23
  499. package/src/Fx/combinators/changesWithEffect.ts +0 -66
  500. package/src/Fx/combinators/compact.ts +0 -15
  501. package/src/Fx/combinators/continueWith.ts +0 -78
  502. package/src/Fx/combinators/dropUntil.ts +0 -47
  503. package/src/Fx/combinators/ensuring.ts +0 -23
  504. package/src/Fx/combinators/exhaustLatestMap.ts +0 -74
  505. package/src/Fx/combinators/exhaustLatestMapEffect.ts +0 -25
  506. package/src/Fx/combinators/exhaustMap.ts +0 -36
  507. package/src/Fx/combinators/exhaustMapEffect.ts +0 -23
  508. package/src/Fx/combinators/exit.ts +0 -15
  509. package/src/Fx/combinators/filter.ts +0 -22
  510. package/src/Fx/combinators/filterEffect.ts +0 -31
  511. package/src/Fx/combinators/filterMap.ts +0 -23
  512. package/src/Fx/combinators/filterMapEffect.ts +0 -32
  513. package/src/Fx/combinators/filterMapLoop.ts +0 -35
  514. package/src/Fx/combinators/filterMapLoopCause.ts +0 -36
  515. package/src/Fx/combinators/filterMapLoopCauseEffect.ts +0 -46
  516. package/src/Fx/combinators/filterMapLoopEffect.ts +0 -36
  517. package/src/Fx/combinators/flatMap.ts +0 -51
  518. package/src/Fx/combinators/flatMapConcurrently.ts +0 -39
  519. package/src/Fx/combinators/flatMapConcurrentlyEffect.ts +0 -26
  520. package/src/Fx/combinators/flatMapEffect.ts +0 -42
  521. package/src/Fx/combinators/flip.ts +0 -14
  522. package/src/Fx/combinators/gen.ts +0 -24
  523. package/src/Fx/combinators/genScoped.ts +0 -24
  524. package/src/Fx/combinators/index.ts +0 -61
  525. package/src/Fx/combinators/keyed.ts +0 -369
  526. package/src/Fx/combinators/loop.ts +0 -27
  527. package/src/Fx/combinators/loopCause.ts +0 -34
  528. package/src/Fx/combinators/loopCauseEffect.ts +0 -36
  529. package/src/Fx/combinators/loopEffect.ts +0 -34
  530. package/src/Fx/combinators/map.ts +0 -22
  531. package/src/Fx/combinators/mapBoth.ts +0 -40
  532. package/src/Fx/combinators/mapEffect.ts +0 -32
  533. package/src/Fx/combinators/mapError.ts +0 -28
  534. package/src/Fx/combinators/mergeAll.ts +0 -22
  535. package/src/Fx/combinators/mergeOrdered.ts +0 -123
  536. package/src/Fx/combinators/onError.ts +0 -40
  537. package/src/Fx/combinators/onExit.ts +0 -82
  538. package/src/Fx/combinators/onInterrupt.ts +0 -87
  539. package/src/Fx/combinators/provide.ts +0 -124
  540. package/src/Fx/combinators/result.ts +0 -39
  541. package/src/Fx/combinators/scan.ts +0 -82
  542. package/src/Fx/combinators/skip.ts +0 -41
  543. package/src/Fx/combinators/skipRepeats.ts +0 -15
  544. package/src/Fx/combinators/skipRepeatsWith.ts +0 -40
  545. package/src/Fx/combinators/skipWhile.ts +0 -100
  546. package/src/Fx/combinators/slice.ts +0 -55
  547. package/src/Fx/combinators/switchMap.ts +0 -55
  548. package/src/Fx/combinators/switchMapEffect.ts +0 -25
  549. package/src/Fx/combinators/take.ts +0 -38
  550. package/src/Fx/combinators/takeUntil.ts +0 -88
  551. package/src/Fx/combinators/takeWhile.ts +0 -47
  552. package/src/Fx/combinators/tapEffect.ts +0 -39
  553. package/src/Fx/combinators/tuple.ts +0 -79
  554. package/src/Fx/combinators/unwrap.ts +0 -21
  555. package/src/Fx/combinators/unwrapScoped.ts +0 -26
  556. package/src/Fx/combinators/when.ts +0 -64
  557. package/src/Fx/combinators/withSpan.ts +0 -24
  558. package/src/Fx/combinators/zip.ts +0 -175
  559. package/src/Fx/constructors/at.ts +0 -23
  560. package/src/Fx/constructors/die.ts +0 -17
  561. package/src/Fx/constructors/empty.ts +0 -10
  562. package/src/Fx/constructors/fail.ts +0 -14
  563. package/src/Fx/constructors/failCause.ts +0 -14
  564. package/src/Fx/constructors/fn.ts +0 -245
  565. package/src/Fx/constructors/fromEffect.ts +0 -24
  566. package/src/Fx/constructors/fromFailures.ts +0 -19
  567. package/src/Fx/constructors/fromIterable.ts +0 -15
  568. package/src/Fx/constructors/fromSchedule.ts +0 -18
  569. package/src/Fx/constructors/fromYieldable.ts +0 -17
  570. package/src/Fx/constructors/index.ts +0 -15
  571. package/src/Fx/constructors/interrupt.ts +0 -13
  572. package/src/Fx/constructors/make.ts +0 -103
  573. package/src/Fx/constructors/periodic.ts +0 -15
  574. package/src/Fx/constructors/succeed.ts +0 -37
  575. package/src/Fx/constructors/suspend.ts +0 -6
  576. package/src/Fx/index.ts +0 -6
  577. package/src/Fx/internal/DeferredRef.ts +0 -87
  578. package/src/Fx/internal/UnionToTuple.ts +0 -12
  579. package/src/Fx/internal/diff.ts +0 -191
  580. package/src/Fx/internal/equivalence.ts +0 -12
  581. package/src/Fx/internal/multicast.ts +0 -51
  582. package/src/Fx/internal/ring-buffer.ts +0 -60
  583. package/src/Fx/internal/scope.ts +0 -58
  584. package/src/Fx/internal/yieldable.ts +0 -31
  585. package/src/Fx/run/collect.ts +0 -79
  586. package/src/Fx/run/first.ts +0 -20
  587. package/src/Fx/run/fork.ts +0 -40
  588. package/src/Fx/run/index.ts +0 -5
  589. package/src/Fx/run/observe.ts +0 -114
  590. package/src/Fx/run/runPromise.ts +0 -32
  591. package/src/Fx/stream.ts +0 -42
  592. package/src/Fx.additive-combinators.test.ts +0 -126
  593. package/src/Fx.catch-additive.test.ts +0 -206
  594. package/src/Fx.catch.test.ts +0 -60
  595. package/src/Fx.dropUntil.test.ts +0 -61
  596. package/src/Fx.fn.test.ts +0 -51
  597. package/src/Fx.lifecycle.test.ts +0 -79
  598. package/src/Fx.mapError-mapBoth.test.ts +0 -101
  599. package/src/Fx.provide-combinators.test.ts +0 -94
  600. package/src/Fx.result-changesWithEffect.test.ts +0 -112
  601. package/src/Fx.scan.test.ts +0 -73
  602. package/src/Fx.takeWhile-skipWhile.test.ts +0 -84
  603. package/src/Fx.test.ts +0 -71
  604. package/src/Fx.ts +0 -1
  605. package/src/Fx.zip-merge-additive.test.ts +0 -171
  606. package/src/Fx.zip.test.ts +0 -133
  607. package/src/Push/Push.ts +0 -1093
  608. package/src/Push/index.ts +0 -1
  609. package/src/Push.additive.test.ts +0 -256
  610. package/src/Push.test.ts +0 -26
  611. package/src/Push.ts +0 -1
  612. package/src/RefSubject/RefArray.ts +0 -516
  613. package/src/RefSubject/RefBigDecimal.test.ts +0 -56
  614. package/src/RefSubject/RefBigDecimal.ts +0 -295
  615. package/src/RefSubject/RefBigInt.test.ts +0 -56
  616. package/src/RefSubject/RefBigInt.ts +0 -189
  617. package/src/RefSubject/RefBoolean.test.ts +0 -57
  618. package/src/RefSubject/RefBoolean.ts +0 -185
  619. package/src/RefSubject/RefCause.test.ts +0 -53
  620. package/src/RefSubject/RefCause.ts +0 -146
  621. package/src/RefSubject/RefChunk.ts +0 -516
  622. package/src/RefSubject/RefDateTime.test.ts +0 -43
  623. package/src/RefSubject/RefDateTime.ts +0 -261
  624. package/src/RefSubject/RefDuration.test.ts +0 -49
  625. package/src/RefSubject/RefDuration.ts +0 -188
  626. package/src/RefSubject/RefGraph.ts +0 -649
  627. package/src/RefSubject/RefHashMap.ts +0 -474
  628. package/src/RefSubject/RefHashRing.ts +0 -262
  629. package/src/RefSubject/RefHashSet.ts +0 -308
  630. package/src/RefSubject/RefIterable.ts +0 -454
  631. package/src/RefSubject/RefOption.test.ts +0 -67
  632. package/src/RefSubject/RefOption.ts +0 -193
  633. package/src/RefSubject/RefRecord.ts +0 -621
  634. package/src/RefSubject/RefResult.test.ts +0 -63
  635. package/src/RefSubject/RefResult.ts +0 -209
  636. package/src/RefSubject/RefString.test.ts +0 -61
  637. package/src/RefSubject/RefString.ts +0 -256
  638. package/src/RefSubject/RefStruct.test.ts +0 -60
  639. package/src/RefSubject/RefStruct.ts +0 -253
  640. package/src/RefSubject/RefSubject.ts +0 -2642
  641. package/src/RefSubject/RefTrie.ts +0 -365
  642. package/src/RefSubject/RefTuple.test.ts +0 -60
  643. package/src/RefSubject/RefTuple.ts +0 -231
  644. package/src/RefSubject/index.ts +0 -21
  645. package/src/RefSubject.additive-parity.test.ts +0 -101
  646. package/src/RefSubject.test.ts +0 -65
  647. package/src/RefSubject.ts +0 -1
  648. package/src/Sink/Sink.ts +0 -159
  649. package/src/Sink/combinators.ts +0 -1115
  650. package/src/Sink/index.ts +0 -2
  651. package/src/Sink.combinators.test.ts +0 -88
  652. package/src/Sink.reduce-collect-head-last.test.ts +0 -107
  653. package/src/Sink.ts +0 -1
  654. package/src/Subject/Subject.ts +0 -440
  655. package/src/Subject/index.ts +0 -1
  656. package/src/Subject.test.ts +0 -47
  657. package/src/Subject.ts +0 -1
  658. package/src/Versioned/Versioned.ts +0 -595
  659. package/src/Versioned/index.ts +0 -1
  660. package/src/Versioned.filterMap.test.ts +0 -91
  661. package/src/Versioned.test.ts +0 -23
  662. package/src/Versioned.ts +0 -1
  663. package/src/index.ts +0 -7
@@ -1,2642 +0,0 @@
1
- /** @effect-diagnostics missingEffectError:skip-file */
2
- /** @effect-diagnostics missingEffectContext:skip-file */
3
-
4
- import * as Array from "effect/Array";
5
- import type * as Cause from "effect/Cause";
6
- import * as Effect from "effect/Effect";
7
- import * as Semaphore from "effect/Semaphore";
8
- import { equals } from "effect/Equal";
9
- import type { Equivalence } from "effect/Equivalence";
10
- import * as Exit from "effect/Exit";
11
- import * as Fiber from "effect/Fiber";
12
- import { dual, identity } from "effect/Function";
13
- import * as Layer from "effect/Layer";
14
- import * as MutableRef from "effect/MutableRef";
15
- import { sum } from "effect/Number";
16
- import * as Option from "effect/Option";
17
- import { pipeArguments } from "effect/Pipeable";
18
- import * as Scope from "effect/Scope";
19
- import * as Context from "effect/Context";
20
- import * as Stream from "effect/Stream";
21
- import { compact as fxCompact } from "../Fx/combinators/compact.js";
22
- import { continueWith } from "../Fx/combinators/continueWith.js";
23
- import { filterMapEffect as fxFilterMapEffect } from "../Fx/combinators/filterMapEffect.js";
24
- import { mapEffect as fxMapEffect } from "../Fx/combinators/mapEffect.js";
25
- import { skipRepeats } from "../Fx/combinators/skipRepeats.js";
26
- import type { Bounds } from "../Fx/combinators/slice.js";
27
- import { slice as fxSlice } from "../Fx/combinators/slice.js";
28
- import { unwrap } from "../Fx/combinators/unwrap.js";
29
- import { fromEffect as fxFromEffect } from "../Fx/constructors/fromEffect.js";
30
- import { fromYieldable } from "../Fx/constructors/fromYieldable.js";
31
- import type { Error as FxError, Fx } from "../Fx/index.js";
32
- import * as DeferredRef from "../Fx/internal/DeferredRef.js";
33
- import { getExitEquivalence } from "../Fx/internal/equivalence.js";
34
- import type { UnionToTuple } from "../Fx/internal/UnionToTuple.js";
35
- import { YieldableFx } from "../Fx/internal/yieldable.js";
36
- import { FxTypeId, isFx } from "../Fx/TypeId.js";
37
- import * as Sink from "../Sink/Sink.js";
38
- import * as Subject from "../Subject/Subject.js";
39
- import * as Versioned from "../Versioned/Versioned.js";
40
-
41
- export const RefSubjectTypeId = Symbol.for("@typed/fx/RefSubject");
42
- export type RefSubjectTypeId = typeof RefSubjectTypeId;
43
-
44
- export const ComputedTypeId = Symbol.for("@typed/fx/Computed");
45
- export type ComputedTypeId = typeof ComputedTypeId;
46
-
47
- export const FilteredTypeId = Symbol.for("@typed/fx/Filtered");
48
- export type FilteredTypeId = typeof FilteredTypeId;
49
-
50
- /**
51
- * A `Computed` is a read-only view of a value that can change over time.
52
- * It is an `Fx` that emits the current value and subsequent updates.
53
- * It is also an `Effect` that samples the current value.
54
- *
55
- * @example
56
- * ```ts
57
- * import { Effect } from "effect"
58
- * import * as RefSubject from "@typed/fx/RefSubject"
59
- * import { Fx } from "@typed/fx"
60
- *
61
- * // Create a RefSubject and derive a Computed from it
62
- * const program = Effect.gen(function* () {
63
- * const count = yield* RefSubject.make(0)
64
- *
65
- * // Create a computed that doubles the count
66
- * const doubled = RefSubject.map(count, (n) => n * 2)
67
- *
68
- * // Sample the computed value
69
- * const value = yield* doubled
70
- * console.log(value) // 0
71
- *
72
- * // Update the source
73
- * yield* RefSubject.set(count, 5)
74
- *
75
- * // The computed automatically reflects the change
76
- * const newValue = yield* doubled
77
- * console.log(newValue) // 10
78
- * })
79
- * ```
80
- *
81
- * @since 1.0.0
82
- * @category models
83
- */
84
- export interface Computed<out A, out E = never, out R = never> extends Versioned.Versioned<
85
- R,
86
- E,
87
- A,
88
- E,
89
- R | Scope.Scope,
90
- A,
91
- E,
92
- R
93
- > {
94
- readonly [ComputedTypeId]: ComputedTypeId;
95
- }
96
-
97
- export declare namespace Computed {
98
- export type Any =
99
- | Computed<any, any, any>
100
- | Computed<never, any, any>
101
- | Computed<any, never, any>
102
- | Computed<never, never, any>;
103
- }
104
-
105
- /**
106
- * A `Filtered` is a `Computed` that may not always have a value.
107
- * It is essentially a `Computed<Option<A>>` with helper methods.
108
- *
109
- * @example
110
- * ```ts
111
- * import { Effect, Option } from "effect"
112
- * import * as RefSubject from "@typed/fx/RefSubject"
113
- *
114
- * // Create a RefSubject and filter it
115
- * const program = Effect.gen(function* () {
116
- * const numbers = yield* RefSubject.make([1, 2, 3, 4, 5])
117
- *
118
- * // Get the first even number (filtered)
119
- * const firstEven = RefSubject.filterMap(
120
- * numbers,
121
- * (arr) => Option.fromNullable(arr.find((n) => n % 2 === 0))
122
- * )
123
- *
124
- * // Try to get the value (may fail with NoSuchElementError)
125
- * const value = yield* firstEven
126
- * console.log(value) // 2
127
- *
128
- * // Or convert back to Option
129
- * const option = firstEven.asComputed()
130
- * const maybeValue = yield* option
131
- * console.log(Option.isSome(maybeValue)) // true
132
- * })
133
- * ```
134
- *
135
- * @since 1.0.0
136
- * @category models
137
- */
138
- export interface Filtered<out A, out E = never, out R = never> extends Versioned.Versioned<
139
- R,
140
- E,
141
- A,
142
- E,
143
- R | Scope.Scope,
144
- A,
145
- E | Cause.NoSuchElementError,
146
- R
147
- > {
148
- readonly [FilteredTypeId]: FilteredTypeId;
149
-
150
- /**
151
- * Converts the Filtered back to a Computed of Option.
152
- *
153
- * @example
154
- * ```ts
155
- * import { Effect, Option } from "effect"
156
- * import * as RefSubject from "@typed/fx/RefSubject"
157
- *
158
- * const program = Effect.gen(function* () {
159
- * const filtered = RefSubject.filterMap(
160
- * yield* RefSubject.make([1, 2, 3]),
161
- * (arr) => Option.fromNullable(arr.find((n) => n > 5))
162
- * )
163
- *
164
- * // Convert to Computed<Option<number>>
165
- * const computed = filtered.asComputed()
166
- * const option = yield* computed
167
- * console.log(Option.isNone(option)) // true (no number > 5)
168
- * })
169
- * ```
170
- */
171
- asComputed(): Computed<Option.Option<A>, E, R>;
172
- }
173
-
174
- export declare namespace Filtered {
175
- export type Any =
176
- | Filtered<any, any, any>
177
- | Filtered<never, any, any>
178
- | Filtered<any, never, any>
179
- | Filtered<never, never, any>;
180
- }
181
-
182
- /**
183
- * Interface for basic RefSubject operations: get, set, delete.
184
- * @since 1.0.0
185
- * @category models
186
- */
187
- export interface GetSetDelete<A, E = never, R = never> {
188
- readonly get: Effect.Effect<A, E, R>;
189
- readonly set: (a: A) => Effect.Effect<A, E, R>;
190
- readonly delete: Effect.Effect<Option.Option<A>, E, R>;
191
- }
192
-
193
- /**
194
- * A `RefSubject` is a mutable reference that can be observed as an Fx.
195
- * It combines the capabilities of a `Ref` (get/set/update) with a `Subject` (subscribe).
196
- *
197
- * @example
198
- * ```ts
199
- * import { Effect } from "effect"
200
- * import * as RefSubject from "@typed/fx/RefSubject"
201
- * import { Fx } from "@typed/fx"
202
- *
203
- * // Create a RefSubject with an initial value
204
- * const program = Effect.gen(function* () {
205
- * const count = yield* RefSubject.make(0)
206
- *
207
- * // Get the current value
208
- * const current = yield* count
209
- * console.log(current) // 0
210
- *
211
- * // Update the value
212
- * yield* RefSubject.set(count, 5)
213
- * const updated = yield* count
214
- * console.log(updated) // 5
215
- *
216
- * // Use as an Fx to observe changes
217
- * yield* Fx.observe(
218
- * count,
219
- * (value) => Effect.sync(() => console.log("Count changed:", value))
220
- * )
221
- *
222
- * // Increment
223
- * yield* RefSubject.increment(count)
224
- * // Output: "Count changed: 6"
225
- * })
226
- * ```
227
- *
228
- * @since 1.0.0
229
- * @category models
230
- */
231
- export interface RefSubject<A, E = never, R = never>
232
- extends Computed<A, E, R>, Subject.Subject<A, E, R> {
233
- readonly [RefSubjectTypeId]: RefSubjectTypeId;
234
-
235
- /**
236
- * Runs an effect that can modify the RefSubject transactionally.
237
- * All operations within the transaction are atomic and serialized.
238
- *
239
- * @example
240
- * ```ts
241
- * import { Effect } from "effect"
242
- * import * as RefSubject from "@typed/fx/RefSubject"
243
- *
244
- * const program = Effect.gen(function* () {
245
- * const balance = yield* RefSubject.make(100)
246
- *
247
- * // Transfer money atomically
248
- * yield* balance.updates((ref) =>
249
- * Effect.gen(function* () {
250
- * const current = yield* ref.get
251
- * if (current >= 50) {
252
- * yield* ref.set(current - 50)
253
- * return "Transfer successful"
254
- * }
255
- * return "Insufficient funds"
256
- * })
257
- * )
258
- * })
259
- * ```
260
- */
261
- readonly updates: <B, E2, R2>(
262
- f: (ref: GetSetDelete<A, E, R>) => Effect.Effect<B, E2, R2>,
263
- ) => Effect.Effect<B, E | E2, R | R2>;
264
-
265
- /**
266
- * Interrupts the RefSubject, stopping all subscriptions and cleaning up resources.
267
- *
268
- * @example
269
- * ```ts
270
- * import { Effect } from "effect"
271
- * import * as RefSubject from "@typed/fx/RefSubject"
272
- *
273
- * const program = Effect.gen(function* () {
274
- * const ref = yield* RefSubject.make(0)
275
- *
276
- * // Later, clean up
277
- * yield* ref.interrupt
278
- * })
279
- * ```
280
- */
281
- readonly interrupt: Effect.Effect<void, never, R>;
282
- }
283
-
284
- export declare namespace RefSubject {
285
- export type Any =
286
- | RefSubject<any, any, any>
287
- | RefSubject<any, any>
288
- | RefSubject<any, never, any>
289
- | RefSubject<any>;
290
-
291
- export interface Service<Self, Id extends string, A, E> extends RefSubject<A, E, Self> {
292
- readonly id: Id;
293
-
294
- readonly service: Context.Service<Self, RefSubject<A, E>>;
295
-
296
- readonly make: <R = never>(
297
- value: A | Effect.Effect<A, E, R> | Fx<A, E, R>,
298
- options?: RefSubjectOptions<A> & { readonly skip?: number; readonly take?: number },
299
- ) => Layer.Layer<Self, never, Exclude<R, Scope.Scope>>;
300
-
301
- readonly layer: <E2, R2>(
302
- make: Effect.Effect<RefSubject<A, E>, E2, R2 | Scope.Scope>,
303
- ) => Layer.Layer<Self, E2, Exclude<R2, Scope.Scope>>;
304
- }
305
-
306
- export interface Class<Self, Id extends string, A, E> extends RefSubject.Service<Self, Id, A, E> {
307
- new (): RefSubject.Service<Self, Id, A, E>;
308
- }
309
- }
310
- export const CurrentComputedBehavior = Context.Reference("@typed/fx/CurrentComputedBehavior", {
311
- defaultValue: (): "one" | "multiple" => "multiple",
312
- });
313
-
314
- const checkIsMultiple = (ctx: Context.Context<any>): ctx is Context.Context<"multiple"> =>
315
- Context.getReferenceUnsafe(ctx, CurrentComputedBehavior) === "multiple";
316
-
317
- class ComputedImpl<R0, E0, A, E, R, E2, R2, C, E3, R3>
318
- extends Versioned.VersionedTransform<
319
- R0,
320
- E0,
321
- A,
322
- E,
323
- R,
324
- A,
325
- E2,
326
- R2,
327
- C,
328
- E0 | E | E2 | E3,
329
- R0 | Exclude<R, Scope.Scope> | R2 | R3 | Scope.Scope,
330
- C,
331
- E0 | E | E2 | E3,
332
- R0 | Exclude<R, Scope.Scope> | R2 | R3
333
- >
334
- implements Computed<C, E0 | E | E2 | E3, R0 | Exclude<R, Scope.Scope> | R2 | R3>
335
- {
336
- readonly [ComputedTypeId]: ComputedTypeId = ComputedTypeId;
337
- private _computed: Fx<C, E0 | E | E2 | E3, R0 | R | Scope.Scope | R2 | R3>;
338
-
339
- override input: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>;
340
- readonly f: (a: A) => Effect.Effect<C, E3, R3>;
341
-
342
- constructor(
343
- input: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
344
- f: (a: A) => Effect.Effect<C, E3, R3>,
345
- ) {
346
- super(input, (fx) => fxMapEffect(fx, f) as any, Effect.flatMap(f));
347
-
348
- this.input = input;
349
- this.f = f;
350
-
351
- this._computed = Subject.hold(
352
- unwrap(
353
- Effect.map(Effect.context(), (ctx) => {
354
- if (checkIsMultiple(ctx)) {
355
- return fromYieldable(input).pipe(
356
- continueWith(() => input),
357
- skipRepeats,
358
- fxMapEffect(f),
359
- );
360
- }
361
-
362
- return fxFromEffect(Effect.flatMap(input.asEffect(), f));
363
- }),
364
- ),
365
- );
366
- }
367
-
368
- override run<RSink>(sink: Sink.Sink<C, E0 | E | E2 | E3, RSink>) {
369
- return this._computed.run(sink) as any;
370
- }
371
- }
372
-
373
- export function makeComputed<R0, E0, A, E, R, E2, R2, C, E3, R3>(
374
- input: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
375
- f: (a: A) => Effect.Effect<C, E3, R3>,
376
- ): Computed<C, E0 | E | E2 | E3, R0 | R2 | R3 | Exclude<R, Scope.Scope>> {
377
- return new ComputedImpl(input, f);
378
- }
379
-
380
- export function makeFiltered<R0, E0, A, E, R, E2, R2, C, E3, R3>(
381
- input: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
382
- f: (a: A) => Effect.Effect<Option.Option<C>, E3, R3>,
383
- ): Filtered<C, E0 | E | E2 | E3, R0 | Exclude<R, Scope.Scope> | R2 | R3> {
384
- return new FilteredImpl(input, f);
385
- }
386
-
387
- class FilteredImpl<R0, E0, A, E, R, E2, R2, C, E3, R3>
388
- extends Versioned.VersionedTransform<
389
- R0,
390
- E0,
391
- A,
392
- E,
393
- R,
394
- A,
395
- E2,
396
- R2,
397
- C,
398
- E0 | E | E2 | E3,
399
- R0 | Exclude<R, Scope.Scope> | R2 | R3 | Scope.Scope,
400
- C,
401
- E0 | E | E2 | E3 | Cause.NoSuchElementError,
402
- R0 | Exclude<R, Scope.Scope> | R2 | R3
403
- >
404
- implements Filtered<C, E0 | E | E2 | E3, R0 | Exclude<R, Scope.Scope> | R2 | R3>
405
- {
406
- readonly [FilteredTypeId]: FilteredTypeId = FilteredTypeId;
407
- private _computed: Fx<C, E0 | E | E2 | E3, R0 | R | Scope.Scope | R2 | R3>;
408
-
409
- override input: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>;
410
- readonly f: (a: A) => Effect.Effect<Option.Option<C>, E3, R3>;
411
-
412
- constructor(
413
- input: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
414
- f: (a: A) => Effect.Effect<Option.Option<C>, E3, R3>,
415
- ) {
416
- super(
417
- input,
418
- (fx) => fxFilterMapEffect(fx, f) as any,
419
- (effect) => Effect.flatMap(Effect.flatMap(effect, f), (option) => option.asEffect()),
420
- );
421
-
422
- this.input = input;
423
- this.f = f;
424
-
425
- this._computed = Subject.hold(
426
- unwrap(
427
- Effect.map(Effect.context(), (ctx) => {
428
- if (checkIsMultiple(ctx)) {
429
- return fromYieldable(input).pipe(
430
- continueWith(() => input),
431
- skipRepeats,
432
- fxFilterMapEffect(f),
433
- );
434
- }
435
-
436
- return fxCompact(fxFromEffect(Effect.flatMap(input.asEffect(), f)));
437
- }),
438
- ),
439
- );
440
- }
441
-
442
- override run<RSink>(sink: Sink.Sink<C, E0 | E | E2 | E3, RSink>) {
443
- return this._computed.run(sink) as any;
444
- }
445
-
446
- asComputed(): Computed<
447
- Option.Option<C>,
448
- E0 | E | E2 | E3,
449
- R0 | R2 | R3 | Exclude<R, Scope.Scope>
450
- > {
451
- return new ComputedImpl(this.input, this.f);
452
- }
453
- }
454
-
455
- class RefSubjectCore<A, E, R, R2> {
456
- readonly initial: Effect.Effect<A, E, R>;
457
- readonly subject: Subject.HoldSubjectImpl<A, E>;
458
- readonly services: Context.Context<R2>;
459
- readonly scope: Scope.Closeable;
460
- readonly deferredRef: DeferredRef.DeferredRef<E, A>;
461
- readonly semaphore: Semaphore.Semaphore;
462
- constructor(
463
- initial: Effect.Effect<A, E, R>,
464
- subject: Subject.HoldSubjectImpl<A, E>,
465
- services: Context.Context<R2>,
466
- scope: Scope.Closeable,
467
- deferredRef: DeferredRef.DeferredRef<E, A>,
468
- semaphore: Semaphore.Semaphore,
469
- ) {
470
- this.initial = initial;
471
- this.subject = subject;
472
- this.services = services;
473
- this.scope = scope;
474
- this.deferredRef = deferredRef;
475
- this.semaphore = semaphore;
476
- }
477
-
478
- public _fiber: Fiber.Fiber<A, E> | undefined = undefined;
479
- }
480
-
481
- export interface RefSubjectOptions<A> {
482
- readonly eq?: Equivalence<A>;
483
- }
484
-
485
- function getSetDelete<A, E, R, R2>(
486
- ref: RefSubjectCore<A, E, R, R2>,
487
- ): GetSetDelete<A, E, Exclude<R, R2>> {
488
- return {
489
- get: getOrInitializeCore(ref, false),
490
- set: (a) => setCore(ref, a),
491
- delete: deleteCore(ref),
492
- };
493
- }
494
- class RefSubjectImpl<A, E, R, R2>
495
- extends YieldableFx<A, E, Exclude<R, R2> | Scope.Scope, A, E, Exclude<R, R2>>
496
- implements RefSubject<A, E, Exclude<R, R2>>
497
- {
498
- readonly [ComputedTypeId]: ComputedTypeId = ComputedTypeId;
499
- readonly [RefSubjectTypeId]: RefSubjectTypeId = RefSubjectTypeId;
500
-
501
- readonly version: Effect.Effect<number>;
502
- readonly interrupt: Effect.Effect<void, never, Exclude<R, R2>>;
503
- readonly subscriberCount: Effect.Effect<number, never, Exclude<R, R2>>;
504
-
505
- readonly getSetDelete: GetSetDelete<A, E, Exclude<R, R2>>;
506
-
507
- readonly core: RefSubjectCore<A, E, R, R2>;
508
-
509
- constructor(core: RefSubjectCore<A, E, R, R2>) {
510
- super();
511
-
512
- this.core = core;
513
- this.version = Effect.sync(() => core.deferredRef.version);
514
- this.interrupt = Effect.provide(interruptCore(core), core.services);
515
- this.subscriberCount = Effect.provide(core.subject.subscriberCount, core.services);
516
- this.getSetDelete = getSetDelete(core);
517
-
518
- this.updates = this.updates.bind(this);
519
- this.onSuccess = this.onSuccess.bind(this);
520
- this.onFailure = this.onFailure.bind(this);
521
- }
522
-
523
- run<R3>(
524
- sink: Sink.Sink<A, E, R3>,
525
- ): Effect.Effect<unknown, never, Exclude<R, R2> | R3 | Scope.Scope> {
526
- return Effect.matchCauseEffect(getOrInitializeCore(this.core, true), {
527
- onFailure: (cause) => sink.onFailure(cause),
528
- onSuccess: () => Effect.provide(this.core.subject.run(sink), this.core.services),
529
- });
530
- }
531
-
532
- updates<R3, E3, B>(run: (ref: GetSetDelete<A, E, Exclude<R, R2>>) => Effect.Effect<B, E3, R3>) {
533
- return this.core.semaphore.withPermits(1)(run(this.getSetDelete));
534
- }
535
-
536
- onSuccess(value: A): Effect.Effect<unknown, never, Exclude<R, R2>> {
537
- return setCore(this.core, value);
538
- }
539
-
540
- onFailure(cause: Cause.Cause<E>): Effect.Effect<unknown, never, Exclude<R, R2>> {
541
- return onFailureCore(this.core, cause);
542
- }
543
-
544
- toEffect(): Effect.Effect<A, E, Exclude<R, R2>> {
545
- return getOrInitializeCore(this.core, true);
546
- }
547
- }
548
-
549
- /**
550
- * Creates a new `RefSubject` from a value, `Effect`, or `Fx`.
551
- *
552
- * @example
553
- * ```ts
554
- * import { Effect } from "effect"
555
- * import * as RefSubject from "@typed/fx/RefSubject"
556
- * import { Fx } from "@typed/fx"
557
- *
558
- * // From a plain value
559
- * const program1 = Effect.gen(function* () {
560
- * const ref = yield* RefSubject.make(42)
561
- * const value = yield* ref
562
- * console.log(value) // 42
563
- * })
564
- *
565
- * // From an Effect
566
- * const program2 = Effect.gen(function* () {
567
- * const ref = yield* RefSubject.make(
568
- * Effect.succeed("Hello")
569
- * )
570
- * const value = yield* ref
571
- * console.log(value) // "Hello"
572
- * })
573
- *
574
- * // From an Fx (tracks the latest value)
575
- * const program3 = Effect.gen(function* () {
576
- * const ref = yield* RefSubject.make(
577
- * Fx.fromIterable([1, 2, 3])
578
- * )
579
- * const value = yield* ref
580
- * console.log(value) // 3 (latest value)
581
- * })
582
- * ```
583
- *
584
- * @since 1.0.0
585
- * @category constructors
586
- */
587
- export function make<A, E = never, R = never>(
588
- effect: A | Effect.Effect<A, E, R> | Stream.Stream<A, E, R> | Fx<A, E, R>,
589
- options?: RefSubjectOptions<A>,
590
- ): Effect.Effect<RefSubject<A, E>, never, R | Scope.Scope> {
591
- if (isFx(effect)) {
592
- return fromFx(effect, options);
593
- } else if (Effect.isEffect(effect)) {
594
- return fromEffect(effect, options);
595
- } else if (Stream.isStream(effect)) {
596
- return fromStream(effect, options);
597
- } else {
598
- return fromEffect<A, E, R>(Effect.succeed(effect), options);
599
- }
600
- }
601
-
602
- /**
603
- * Creates a `RefSubject` from an `Effect`.
604
- *
605
- * @example
606
- * ```ts
607
- * import { Effect } from "effect"
608
- * import * as RefSubject from "@typed/fx/RefSubject"
609
- *
610
- * const program = Effect.gen(function* () {
611
- * const ref = yield* RefSubject.fromEffect(
612
- * Effect.succeed("Initial value")
613
- * )
614
- *
615
- * const value = yield* ref
616
- * console.log(value) // "Initial value"
617
- * })
618
- * ```
619
- *
620
- * @since 1.0.0
621
- * @category constructors
622
- */
623
- export function fromEffect<A, E, R>(
624
- effect: Effect.Effect<A, E, R>,
625
- options?: RefSubjectOptions<A>,
626
- ): Effect.Effect<RefSubject<A, E>, never, R | Scope.Scope> {
627
- return Effect.map(makeCore(effect, options), (core) => new RefSubjectImpl(core));
628
- }
629
-
630
- /**
631
- * Creates a `RefSubject` from an `Fx`, tracking the latest emitted value.
632
- *
633
- * @example
634
- * ```ts
635
- * import { Effect } from "effect"
636
- * import * as RefSubject from "@typed/fx/RefSubject"
637
- * import { Fx } from "@typed/fx"
638
- *
639
- * const program = Effect.gen(function* () {
640
- * // Create an Fx that emits multiple values
641
- * const numbers = Fx.fromIterable([1, 2, 3, 4, 5])
642
- *
643
- * // Create a RefSubject that tracks the latest value
644
- * const ref = yield* RefSubject.fromFx(numbers)
645
- *
646
- * // Get the latest value
647
- * const latest = yield* ref
648
- * console.log(latest) // 5
649
- * })
650
- * ```
651
- *
652
- * @since 1.0.0
653
- * @category constructors
654
- */
655
- export function fromFx<A, E, R>(
656
- fx: Fx<A, E, R>,
657
- options?: RefSubjectOptions<A>,
658
- ): Effect.Effect<RefSubject<A, E>, never, R | Scope.Scope> {
659
- return Effect.gen(function* () {
660
- const core = yield* makeDeferredCore<A, E, R>(options);
661
- const ref = new RefSubjectImpl(core);
662
- yield* Effect.forkIn(
663
- fx.run(
664
- Sink.make(
665
- (cause) => onFailureCore(core, cause),
666
- (value) => setCore(core, value),
667
- ),
668
- ),
669
- core.scope,
670
- { startImmediately: true },
671
- );
672
- return ref;
673
- });
674
- }
675
-
676
- export function fromStream<A, E, R>(
677
- stream: Stream.Stream<A, E, R>,
678
- options?: RefSubjectOptions<A>,
679
- ): Effect.Effect<RefSubject<A, E>, never, R | Scope.Scope> {
680
- return Effect.gen(function* () {
681
- const core = yield* makeDeferredCore<A, E, R>(options);
682
- const ref = new RefSubjectImpl(core);
683
- yield* Effect.forkIn(
684
- stream.pipe(
685
- redirectCause(core),
686
- Stream.runForEach((value) => setCore(core, value)),
687
- ),
688
- core.scope,
689
- { startImmediately: true },
690
- );
691
- return ref;
692
- });
693
- }
694
-
695
- /**
696
- * Creates a `RefSubject` from an `Option` value.
697
- *
698
- * @example
699
- * ```ts
700
- * import { Effect, Option } from "effect"
701
- * import * as RefSubject from "@typed/fx/RefSubject"
702
- *
703
- * const program = Effect.gen(function* () {
704
- * const ref = yield* RefSubject.fromOption(Option.some(42))
705
- * const value = yield* ref
706
- * console.log(Option.isSome(value)) // true
707
- * })
708
- * ```
709
- *
710
- * @since 1.0.0
711
- * @category constructors
712
- */
713
- export function fromOption<A>(
714
- option: Option.Option<A>,
715
- options?: RefSubjectOptions<Option.Option<A>>,
716
- ): Effect.Effect<RefSubject<Option.Option<A>>, never, Scope.Scope> {
717
- return make(option, options);
718
- }
719
-
720
- /**
721
- * Creates a `RefSubject` from a nullable value (null/undefined become `Option.none()`).
722
- *
723
- * @example
724
- * ```ts
725
- * import { Effect, Option } from "effect"
726
- * import * as RefSubject from "@typed/fx/RefSubject"
727
- *
728
- * const program = Effect.gen(function* () {
729
- * const ref = yield* RefSubject.fromNullable("hello")
730
- * const value = yield* ref
731
- * console.log(Option.isSome(value)) // true
732
- *
733
- * const empty = yield* RefSubject.fromNullable(null)
734
- * const none = yield* empty
735
- * console.log(Option.isNone(none)) // true
736
- * })
737
- * ```
738
- *
739
- * @since 1.0.0
740
- * @category constructors
741
- */
742
- function optionFromNullable<A>(value: A | null | undefined): Option.Option<NonNullable<A>> {
743
- return value === null || value === undefined
744
- ? Option.none()
745
- : Option.some(value as NonNullable<A>);
746
- }
747
-
748
- export function fromNullable<A>(
749
- value: A | null | undefined,
750
- options?: RefSubjectOptions<Option.Option<NonNullable<A>>>,
751
- ): Effect.Effect<RefSubject<Option.Option<NonNullable<A>>>, never, Scope.Scope> {
752
- return make(optionFromNullable(value), options);
753
- }
754
-
755
- function redirectCause<A, E, R>(core: RefSubjectCore<A, E, R, R | Scope.Scope>) {
756
- return Stream.catchCause((cause: Cause.Cause<E>) =>
757
- Stream.unwrap(Effect.as(onFailureCore(core, cause), Stream.empty)),
758
- );
759
- }
760
-
761
- function makeCore<A, E, R>(
762
- initial: Effect.Effect<A, E, R>,
763
- options?: RefSubjectOptions<A>,
764
- deferredRef?: DeferredRef.DeferredRef<E, A>,
765
- ) {
766
- return Effect.gen(function* () {
767
- const services = yield* Effect.context<R | Scope.Scope>();
768
- const scope = yield* Scope.fork(Context.get(services, Scope.Scope));
769
- const id = yield* Effect.withFiber((fiber) => Effect.succeed(fiber.id));
770
- const subject = new Subject.HoldSubjectImpl<A, E>();
771
- const core = new RefSubjectCore(
772
- initial,
773
- subject,
774
- services,
775
- scope,
776
- deferredRef ??
777
- DeferredRef.unsafeMake(id, getExitEquivalence(options?.eq ?? equals), subject.lastValue),
778
- Semaphore.makeUnsafe(1),
779
- );
780
- yield* Scope.addFinalizer(scope, core.subject.interrupt);
781
- return core;
782
- });
783
- }
784
-
785
- function makeDeferredCore<A, E = never, R = never>(options?: RefSubjectOptions<A>) {
786
- return Effect.gen(function* () {
787
- const deferredRef = yield* DeferredRef.make<E, A>(getExitEquivalence(options?.eq ?? equals));
788
- return yield* makeCore<A, E, R>(deferredRef.asEffect(), options, deferredRef);
789
- });
790
- }
791
-
792
- function getOrInitializeCore<A, E, R, R2>(
793
- core: RefSubjectCore<A, E, R, R2>,
794
- lockInitialize: boolean,
795
- ): Effect.Effect<A, E, Exclude<R, R2>> {
796
- return Effect.suspend(() => {
797
- if (core._fiber === undefined && Option.isNone(MutableRef.get(core.deferredRef.current))) {
798
- return initializeCoreAndTap(core, lockInitialize);
799
- } else {
800
- return core.deferredRef.asEffect();
801
- }
802
- });
803
- }
804
-
805
- function initializeCoreEffect<A, E, R, R2>(
806
- core: RefSubjectCore<A, E, R, R2>,
807
- lock: boolean,
808
- ): Effect.Effect<Fiber.Fiber<A, E>, never, Exclude<R, R2>> {
809
- const initialize = Effect.onExit(Effect.provide(core.initial, core.services), (exit) =>
810
- Effect.sync(() => {
811
- core._fiber = undefined;
812
- core.deferredRef.done(exit);
813
- }),
814
- );
815
-
816
- return Effect.flatMap(
817
- Effect.forkIn(lock ? core.semaphore.withPermits(1)(initialize) : initialize, core.scope),
818
- (fiber) => Effect.sync(() => (core._fiber = fiber)),
819
- );
820
- }
821
-
822
- function initializeCoreAndTap<A, E, R, R2>(
823
- core: RefSubjectCore<A, E, R, R2>,
824
- lock: boolean,
825
- ): Effect.Effect<A, E, Exclude<R, R2>> {
826
- return Effect.flatMapEager(initializeCoreEffect(core, lock), () =>
827
- tapEventCore(core, core.deferredRef.asEffect()),
828
- );
829
- }
830
-
831
- function setCore<A, E, R, R2>(
832
- core: RefSubjectCore<A, E, R, R2>,
833
- a: A,
834
- ): Effect.Effect<A, never, Exclude<R, R2>> {
835
- return Effect.suspend(() => {
836
- const exit = Exit.succeed(a);
837
-
838
- if (core.deferredRef.done(exit)) {
839
- // If the value changed, send an event
840
- return Effect.as(sendEvent(core, exit), a);
841
- } else {
842
- // Otherwise, just return the current value
843
- return Effect.succeed(a);
844
- }
845
- });
846
- }
847
-
848
- function onFailureCore<A, E, R, R2>(core: RefSubjectCore<A, E, R, R2>, cause: Cause.Cause<E>) {
849
- const exit = Exit.failCause(cause);
850
-
851
- return Effect.suspend(() => {
852
- if (core.deferredRef.done(exit)) {
853
- return sendEvent(core, exit);
854
- } else {
855
- return Effect.void;
856
- }
857
- });
858
- }
859
-
860
- function interruptCore<A, E, R, R2>(
861
- core: RefSubjectCore<A, E, R, R2>,
862
- ): Effect.Effect<void, never, R> {
863
- return Effect.withFiber((fiber) => {
864
- core.deferredRef.reset();
865
-
866
- const closeScope = Scope.close(core.scope, Exit.interrupt(fiber.id));
867
- const interruptFiber = core._fiber ? Fiber.interrupt(core._fiber) : Effect.void;
868
- const interruptSubject = core.subject.interrupt;
869
-
870
- return Effect.all([closeScope, interruptFiber, interruptSubject], { discard: true });
871
- });
872
- }
873
-
874
- function deleteCore<A, E, R, R2>(
875
- core: RefSubjectCore<A, E, R, R2>,
876
- ): Effect.Effect<Option.Option<A>, E, Exclude<R, R2>> {
877
- return Effect.suspend(() => {
878
- const current = MutableRef.get(core.deferredRef.current);
879
- core.deferredRef.reset();
880
-
881
- if (Option.isNone(current)) {
882
- return Effect.succeed(Option.none());
883
- }
884
-
885
- return core.subject.subscriberCount.pipe(
886
- Effect.flatMap((count: number) =>
887
- count > 0 && !core._fiber ? initializeCoreEffect(core, false) : Effect.void,
888
- ),
889
- Effect.flatMap(() => Effect.asSome(current.value)),
890
- );
891
- });
892
- }
893
-
894
- function tapEventCore<A, E, R, R2, R3>(
895
- core: RefSubjectCore<A, E, R, R2>,
896
- effect: Effect.Effect<A, E, R3>,
897
- ) {
898
- return effect.pipe(Effect.onExit((exit) => sendEvent(core, exit)));
899
- }
900
-
901
- function sendEvent<A, E, R, R2>(
902
- core: RefSubjectCore<A, E, R, R2>,
903
- exit: Exit.Exit<A, E>,
904
- ): Effect.Effect<unknown, never, Exclude<R, R2>> {
905
- if (Exit.isSuccess(exit)) {
906
- return core.subject.onSuccess(exit.value);
907
- } else {
908
- return core.subject.onFailure(exit.cause);
909
- }
910
- }
911
-
912
- /**
913
- * Sets the value of a `RefSubject`.
914
- *
915
- * @example
916
- * ```ts
917
- * import { Effect } from "effect"
918
- * import * as RefSubject from "@typed/fx/RefSubject"
919
- *
920
- * const program = Effect.gen(function* () {
921
- * const count = yield* RefSubject.make(0)
922
- *
923
- * // Set the value
924
- * yield* RefSubject.set(count, 10)
925
- * const value = yield* count
926
- * console.log(value) // 10
927
- *
928
- * // Can also use pipe syntax
929
- * yield* count.pipe(RefSubject.set(20))
930
- * const newValue = yield* count
931
- * console.log(newValue) // 20
932
- * })
933
- * ```
934
- *
935
- * @since 1.0.0
936
- * @category combinators
937
- */
938
- export const set: {
939
- <A>(value: A): <E, R>(ref: RefSubject<A, E, R>) => Effect.Effect<A, E, R>;
940
- <A, E, R>(ref: RefSubject<A, E, R>, a: A): Effect.Effect<A, E, R>;
941
- } = dual(2, function set<A, E, R>(ref: RefSubject<A, E, R>, a: A): Effect.Effect<A, E, R> {
942
- return ref.updates((ref) => ref.set(a));
943
- });
944
-
945
- /**
946
- * Resets a `RefSubject` to its initial value, returning the previous value if it existed.
947
- *
948
- * @example
949
- * ```ts
950
- * import { Effect, Option } from "effect"
951
- * import * as RefSubject from "@typed/fx/RefSubject"
952
- *
953
- * const program = Effect.gen(function* () {
954
- * const count = yield* RefSubject.make(0)
955
- *
956
- * yield* RefSubject.set(count, 5)
957
- * const before = yield* count
958
- * console.log(before) // 5
959
- *
960
- * // Reset to initial value
961
- * const previous = yield* RefSubject.reset(count)
962
- * console.log(Option.isSome(previous)) // true
963
- * console.log(previous.value) // 5
964
- *
965
- * const after = yield* count
966
- * console.log(after) // 0
967
- * })
968
- * ```
969
- *
970
- * @since 1.0.0
971
- * @category combinators
972
- */
973
- export function reset<A, E, R>(ref: RefSubject<A, E, R>): Effect.Effect<Option.Option<A>, E, R> {
974
- return ref.updates((ref) => ref.delete);
975
- }
976
-
977
- export {
978
- /**
979
- * Deletes the current value of a `RefSubject`, resetting it to its initial state.
980
- *
981
- * @example
982
- * ```ts
983
- * import { Effect, Option } from "effect"
984
- * import * as RefSubject from "@typed/fx/RefSubject"
985
- *
986
- * const program = Effect.gen(function* () {
987
- * const ref = yield* RefSubject.make(10)
988
- * yield* RefSubject.set(ref, 20)
989
- *
990
- * // Delete the current value
991
- * const deleted = yield* RefSubject.delete(ref)
992
- * console.log(Option.isSome(deleted)) // true
993
- * console.log(deleted.value) // 20
994
- *
995
- * // Value is reset to initial
996
- * const current = yield* ref
997
- * console.log(current) // 10
998
- * })
999
- * ```
1000
- *
1001
- * @since 1.20.0
1002
- * @category combinators
1003
- */
1004
- reset as delete,
1005
- };
1006
-
1007
- /**
1008
- * Updates a `RefSubject` using an `Effect`ful function.
1009
- *
1010
- * @example
1011
- * ```ts
1012
- * import { Effect } from "effect"
1013
- * import * as RefSubject from "@typed/fx/RefSubject"
1014
- *
1015
- * const program = Effect.gen(function* () {
1016
- * const count = yield* RefSubject.make(5)
1017
- *
1018
- * // Update with an async operation
1019
- * yield* RefSubject.updateEffect(count, (value) =>
1020
- * Effect.succeed(value * 2)
1021
- * )
1022
- *
1023
- * const result = yield* count
1024
- * console.log(result) // 10
1025
- * })
1026
- * ```
1027
- *
1028
- * @since 1.0.0
1029
- * @category combinators
1030
- */
1031
- export const updateEffect: {
1032
- <A, E2, R2>(
1033
- f: (value: A) => Effect.Effect<A, E2, R2>,
1034
- ): <E, R>(ref: RefSubject<A, E, R>) => Effect.Effect<A, E | E2, R | R2>;
1035
- <A, E, R, E2, R2>(
1036
- ref: RefSubject<A, E, R>,
1037
- f: (value: A) => Effect.Effect<A, E2, R2>,
1038
- ): Effect.Effect<A, E | E2, R | R2>;
1039
- } = dual(2, function updateEffect<
1040
- A,
1041
- E,
1042
- R,
1043
- E2,
1044
- R2,
1045
- >(ref: RefSubject<A, E, R>, f: (value: A) => Effect.Effect<A, E2, R2>) {
1046
- return ref.updates((ref) => Effect.flatMap(Effect.flatMap(ref.get, f), ref.set));
1047
- });
1048
-
1049
- /**
1050
- * Updates a `RefSubject` using a pure function.
1051
- *
1052
- * @example
1053
- * ```ts
1054
- * import { Effect } from "effect"
1055
- * import * as RefSubject from "@typed/fx/RefSubject"
1056
- *
1057
- * const program = Effect.gen(function* () {
1058
- * const count = yield* RefSubject.make(5)
1059
- *
1060
- * // Increment by 1
1061
- * yield* RefSubject.update(count, (n) => n + 1)
1062
- * const value = yield* count
1063
- * console.log(value) // 6
1064
- *
1065
- * // Can also use pipe syntax
1066
- * yield* count.pipe(RefSubject.update((n) => n * 2))
1067
- * const doubled = yield* count
1068
- * console.log(doubled) // 12
1069
- * })
1070
- * ```
1071
- *
1072
- * @since 1.0.0
1073
- * @category combinators
1074
- */
1075
- export const update: {
1076
- <A>(f: (value: A) => A): <E, R>(ref: RefSubject<A, E, R>) => Effect.Effect<A, E, R>;
1077
- <A, E, R>(ref: RefSubject<A, E, R>, f: (value: A) => A): Effect.Effect<A, E, R>;
1078
- } = dual(2, function update<A, E, R>(ref: RefSubject<A, E, R>, f: (value: A) => A) {
1079
- return updateEffect(ref, (value) => Effect.succeed(f(value)));
1080
- });
1081
-
1082
- /**
1083
- * Modifies a `RefSubject` using an `Effect`ful function that returns both a result and a new value.
1084
- *
1085
- * @example
1086
- * ```ts
1087
- * import { Effect } from "effect"
1088
- * import * as RefSubject from "@typed/fx/RefSubject"
1089
- *
1090
- * const program = Effect.gen(function* () {
1091
- * const count = yield* RefSubject.make(5)
1092
- *
1093
- * // Get the old value and set a new one, returning the old value
1094
- * const oldValue = yield* RefSubject.modifyEffect(count, (value) =>
1095
- * Effect.succeed([value, value + 10] as const)
1096
- * )
1097
- *
1098
- * console.log(oldValue) // 5
1099
- * const newValue = yield* count
1100
- * console.log(newValue) // 15
1101
- * })
1102
- * ```
1103
- *
1104
- * @since 1.0.0
1105
- * @category combinators
1106
- */
1107
- export const modifyEffect: {
1108
- <A, B, E2, R2>(
1109
- f: (value: A) => Effect.Effect<readonly [B, A], E2, R2>,
1110
- ): <E, R>(ref: RefSubject<A, E, R>) => Effect.Effect<B, E | E2, R | R2>;
1111
- <A, E, R, B, E2, R2>(
1112
- ref: RefSubject<A, E, R>,
1113
- f: (value: A) => Effect.Effect<readonly [B, A], E2, R2>,
1114
- ): Effect.Effect<B, E | E2, R | R2>;
1115
- } = dual(2, function modifyEffect<
1116
- A,
1117
- E,
1118
- R,
1119
- B,
1120
- E2,
1121
- R2,
1122
- >(ref: RefSubject<A, E, R>, f: (value: A) => Effect.Effect<readonly [B, A], E2, R2>) {
1123
- return ref.updates((ref) =>
1124
- Effect.flatMap(ref.get, (value) =>
1125
- Effect.flatMap(f(value), ([b, a]) => Effect.flatMap(ref.set(a), () => Effect.succeed(b))),
1126
- ),
1127
- );
1128
- });
1129
-
1130
- /**
1131
- * Modifies a `RefSubject` using a pure function that returns both a result and a new value.
1132
- *
1133
- * @example
1134
- * ```ts
1135
- * import { Effect } from "effect"
1136
- * import * as RefSubject from "@typed/fx/RefSubject"
1137
- *
1138
- * const program = Effect.gen(function* () {
1139
- * const count = yield* RefSubject.make(5)
1140
- *
1141
- * // Get the old value and increment, returning the old value
1142
- * const oldValue = yield* RefSubject.modify(count, (value) => [value, value + 1] as const)
1143
- *
1144
- * console.log(oldValue) // 5
1145
- * const newValue = yield* count
1146
- * console.log(newValue) // 6
1147
- * })
1148
- * ```
1149
- *
1150
- * @since 1.0.0
1151
- * @category combinators
1152
- */
1153
- export const modify: {
1154
- <A, B>(
1155
- f: (value: A) => readonly [B, A],
1156
- ): <E, R>(ref: RefSubject<A, E, R>) => Effect.Effect<B, E, R>;
1157
- <A, E, R, B>(ref: RefSubject<A, E, R>, f: (value: A) => readonly [B, A]): Effect.Effect<B, E, R>;
1158
- } = dual(2, function modify<
1159
- A,
1160
- E,
1161
- R,
1162
- B,
1163
- >(ref: RefSubject<A, E, R>, f: (value: A) => readonly [B, A]) {
1164
- return modifyEffect(ref, (value) => Effect.succeed(f(value)));
1165
- });
1166
-
1167
- /**
1168
- * Checks if a value is a `RefSubject`.
1169
- *
1170
- * @example
1171
- * ```ts
1172
- * import { Effect } from "effect"
1173
- * import * as RefSubject from "@typed/fx/RefSubject"
1174
- *
1175
- * const program = Effect.gen(function* () {
1176
- * const ref = yield* RefSubject.make(42)
1177
- * const isRef = RefSubject.isRefSubject(ref)
1178
- * console.log(isRef) // true
1179
- *
1180
- * const notRef = { value: 42 }
1181
- * const isNotRef = RefSubject.isRefSubject(notRef)
1182
- * console.log(isNotRef) // false
1183
- * })
1184
- * ```
1185
- *
1186
- * @since 1.0.0
1187
- * @category guards
1188
- */
1189
- export function isRefSubject(value: any): value is RefSubject<any, any, any> {
1190
- return value && typeof value === "object" && value[RefSubjectTypeId] === RefSubjectTypeId;
1191
- }
1192
-
1193
- const isRefSubjectDataFirst = (args: IArguments) => isRefSubject(args[0]);
1194
-
1195
- /**
1196
- * Runs an effect that can modify a `RefSubject` transactionally, with optional interrupt handling.
1197
- *
1198
- * @example
1199
- * ```ts
1200
- * import { Effect } from "effect"
1201
- * import * as RefSubject from "@typed/fx/RefSubject"
1202
- *
1203
- * const program = Effect.gen(function* () {
1204
- * const balance = yield* RefSubject.make(100)
1205
- *
1206
- * // Transfer money atomically with interrupt handling
1207
- * yield* RefSubject.runUpdates(
1208
- * balance,
1209
- * (ref) =>
1210
- * Effect.gen(function* () {
1211
- * const current = yield* ref.get
1212
- * if (current >= 50) {
1213
- * yield* ref.set(current - 50)
1214
- * return "Transfer successful"
1215
- * }
1216
- * return "Insufficient funds"
1217
- * }),
1218
- * {
1219
- * onInterrupt: (value) => Effect.sync(() => console.log(`Interrupted at balance: ${value}`)),
1220
- * value: "initial"
1221
- * }
1222
- * )
1223
- * })
1224
- * ```
1225
- *
1226
- * @since 1.0.0
1227
- * @category combinators
1228
- */
1229
- export const runUpdates: {
1230
- <A, E, R, B, E2, R2, R3 = never, E3 = never, C = never>(
1231
- f: (ref: GetSetDelete<A, E, R>) => Effect.Effect<B, E2, R2>,
1232
- options?: {
1233
- readonly onInterrupt: (value: A) => Effect.Effect<C, E3, R3>;
1234
- readonly value?: "initial" | "current";
1235
- },
1236
- ): (ref: RefSubject<A, E, R>) => Effect.Effect<B, E | E2 | E3, R | R2 | R3>;
1237
-
1238
- <A, E, R, B, E2, R2, R3 = never, E3 = never, C = never>(
1239
- ref: RefSubject<A, E, R>,
1240
- f: (ref: GetSetDelete<A, E, R>) => Effect.Effect<B, E2, R2>,
1241
- options?: {
1242
- readonly onInterrupt: (value: A) => Effect.Effect<C, E3, R3>;
1243
- readonly value?: "initial" | "current";
1244
- },
1245
- ): Effect.Effect<B, E | E2 | E3, R | R2 | R3>;
1246
- } = dual(
1247
- isRefSubjectDataFirst,
1248
- function runUpdates<A, E, R, B, E2, R2, R3 = never, E3 = never, C = never>(
1249
- ref: RefSubject<A, E, R>,
1250
- f: (ref: GetSetDelete<A, E, R>) => Effect.Effect<B, E2, R2>,
1251
- options?: {
1252
- readonly onInterrupt: (value: A) => Effect.Effect<C, E3, R3>;
1253
- readonly value?: "initial" | "current";
1254
- },
1255
- ) {
1256
- if (options === undefined) {
1257
- return ref.updates(f);
1258
- } else if (options.value === "initial") {
1259
- return ref.updates((ref) =>
1260
- Effect.flatMap(ref.get, (initial) =>
1261
- f(ref).pipe(Effect.onInterrupt(() => options.onInterrupt(initial))),
1262
- ),
1263
- );
1264
- } else {
1265
- return ref.updates((ref) =>
1266
- f(ref).pipe(Effect.onInterrupt(() => Effect.flatMap(ref.get, options.onInterrupt))),
1267
- );
1268
- }
1269
- },
1270
- );
1271
-
1272
- /**
1273
- * Increments a numeric `RefSubject` by 1.
1274
- *
1275
- * @example
1276
- * ```ts
1277
- * import { Effect } from "effect"
1278
- * import * as RefSubject from "@typed/fx/RefSubject"
1279
- *
1280
- * const program = Effect.gen(function* () {
1281
- * const count = yield* RefSubject.make(0)
1282
- *
1283
- * yield* RefSubject.increment(count)
1284
- * const value = yield* count
1285
- * console.log(value) // 1
1286
- *
1287
- * yield* RefSubject.increment(count)
1288
- * const newValue = yield* count
1289
- * console.log(newValue) // 2
1290
- * })
1291
- * ```
1292
- *
1293
- * @since 1.0.0
1294
- * @category combinators
1295
- */
1296
- export function increment<E, R>(ref: RefSubject<number, E, R>): Effect.Effect<number, E, R> {
1297
- return update(ref, (value) => value + 1);
1298
- }
1299
-
1300
- /**
1301
- * Decrements a numeric `RefSubject` by 1.
1302
- *
1303
- * @example
1304
- * ```ts
1305
- * import { Effect } from "effect"
1306
- * import * as RefSubject from "@typed/fx/RefSubject"
1307
- *
1308
- * const program = Effect.gen(function* () {
1309
- * const count = yield* RefSubject.make(10)
1310
- *
1311
- * yield* RefSubject.decrement(count)
1312
- * const value = yield* count
1313
- * console.log(value) // 9
1314
- *
1315
- * yield* RefSubject.decrement(count)
1316
- * const newValue = yield* count
1317
- * console.log(newValue) // 8
1318
- * })
1319
- * ```
1320
- *
1321
- * @since 1.0.0
1322
- * @category combinators
1323
- */
1324
- export function decrement<E, R>(ref: RefSubject<number, E, R>): Effect.Effect<number, E, R> {
1325
- return update(ref, (value) => value - 1);
1326
- }
1327
-
1328
- const Variance: Fx.Variance<any, any, any> = {
1329
- _A: identity,
1330
- _E: identity,
1331
- _R: identity,
1332
- };
1333
-
1334
- export function Service<Self, A, E = never>() {
1335
- return <const Id extends string>(id: Id): RefSubject.Class<Self, Id, A, E> => {
1336
- const service = Context.Service<Self, RefSubject<A, E>>(id);
1337
-
1338
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
1339
- return class RefSubjectService {
1340
- /// Service
1341
-
1342
- static readonly id = id;
1343
- static readonly service = service;
1344
-
1345
- static readonly layer = <E2, R2>(
1346
- make: Effect.Effect<RefSubject<A, E>, E2, R2 | Scope.Scope>,
1347
- ) => Layer.effect(service, make);
1348
-
1349
- static readonly make = <R = never>(
1350
- value: A | Effect.Effect<A, E, R> | Fx<A, E, R>,
1351
- options?: RefSubjectOptions<A> & Partial<Bounds>,
1352
- ): Layer.Layer<Self, never, R> => {
1353
- const bounds = getDefaultBounds(options);
1354
- return make(value, options).pipe(
1355
- Effect.map((ref) => (bounds ? slice(ref, bounds.skip, bounds.take) : ref)),
1356
- this.layer,
1357
- );
1358
- };
1359
-
1360
- // Fx
1361
- static readonly [FxTypeId]: Fx.Variance<A, E, Self> = Variance;
1362
- static readonly run = <RSink>(sink: Sink.Sink<A, E, RSink>) =>
1363
- Effect.flatMap(service.asEffect(), (ref) => ref.run(sink));
1364
-
1365
- // Sink
1366
- static readonly onSuccess = (value: A) =>
1367
- Effect.flatMap(service.asEffect(), (ref) => ref.onSuccess(value));
1368
- static readonly onFailure = (cause: Cause.Cause<E>) =>
1369
- Effect.flatMap(service.asEffect(), (ref) => ref.onFailure(cause));
1370
-
1371
- /// Computed
1372
- static readonly [ComputedTypeId]: ComputedTypeId = ComputedTypeId;
1373
- static readonly version = Effect.flatMap(service.asEffect(), (ref) => ref.version);
1374
-
1375
- // Subject
1376
- static readonly subscriberCount = Effect.flatMap(
1377
- service.asEffect(),
1378
- (ref) => ref.subscriberCount,
1379
- );
1380
- static readonly interrupt = Effect.flatMap(service.asEffect(), (ref) => ref.interrupt);
1381
-
1382
- // RefSubject
1383
- static readonly [RefSubjectTypeId]: RefSubjectTypeId = RefSubjectTypeId;
1384
- static readonly updates = <B, E2, R2>(
1385
- f: (ref: GetSetDelete<A, E, never>) => Effect.Effect<B, E2, R2>,
1386
- ) => Effect.flatMap(service.asEffect(), (ref) => ref.updates(f));
1387
-
1388
- // Yieldable
1389
- static readonly asEffect = () => Effect.flatMap(service.asEffect(), Effect.fromYieldable);
1390
- static readonly [Symbol.iterator] = function* () {
1391
- const ref = yield* service;
1392
- return yield* ref;
1393
- };
1394
- static readonly pipe: RefSubject.Service<Self, Id, A, E>["pipe"] = function pipe(
1395
- this: RefSubject.Service<Self, Id, A, E>,
1396
- ) {
1397
- return pipeArguments(this, arguments);
1398
- };
1399
-
1400
- constructor() {
1401
- return RefSubjectService;
1402
- }
1403
- } as unknown as RefSubject.Class<Self, Id, A, E>;
1404
- };
1405
- }
1406
-
1407
- function getDefaultBounds(options?: Partial<Bounds>): Bounds | undefined {
1408
- if (options === undefined || (options.skip === undefined && options.take === undefined)) {
1409
- return { skip: 0, take: Infinity };
1410
- }
1411
-
1412
- return { skip: options.skip ?? 0, take: options.take ?? Infinity };
1413
- }
1414
-
1415
- /**
1416
- * Extract all values from an object using a Proxy.
1417
- * Allows accessing nested properties of a `Computed` or `Filtered` object/array as individual computed values.
1418
- *
1419
- * @example
1420
- * ```ts
1421
- * import { Effect } from "effect"
1422
- * import * as RefSubject from "@typed/fx/RefSubject"
1423
- *
1424
- * const program = Effect.gen(function* () {
1425
- * const user = yield* RefSubject.make({ name: "Alice", age: 30 })
1426
- *
1427
- * // Create a proxy to access nested properties
1428
- * const proxied = RefSubject.proxy(user)
1429
- *
1430
- * // Access individual properties as Computed values
1431
- * const name = yield* proxied.name
1432
- * console.log(name) // "Alice"
1433
- *
1434
- * const age = yield* proxied.age
1435
- * console.log(age) // 30
1436
- *
1437
- * // Update the source
1438
- * yield* RefSubject.set(user, { name: "Bob", age: 25 })
1439
- *
1440
- * // Proxied values automatically update
1441
- * const newName = yield* proxied.name
1442
- * console.log(newName) // "Bob"
1443
- * })
1444
- * ```
1445
- *
1446
- * @since 2.0.0
1447
- * @category combinators
1448
- */
1449
- export const proxy: {
1450
- <A extends ReadonlyArray<any> | Readonly<Record<PropertyKey, any>>, E, R>(
1451
- source: Computed<A, E, R>,
1452
- ): { readonly [K in keyof A]: Computed<A[K], E, R> };
1453
-
1454
- <A extends ReadonlyArray<any> | Readonly<Record<PropertyKey, any>>, E, R>(
1455
- source: Filtered<A, E, R>,
1456
- ): { readonly [K in keyof A]: Filtered<A[K], E, R> };
1457
- } = <A extends Readonly<Record<PropertyKey, any>> | ReadonlyArray<any>, E, R>(
1458
- source: Computed<A, E, R> | Filtered<A, E, R>,
1459
- ): any => {
1460
- const target: any = {};
1461
- return new Proxy(target, {
1462
- get(self, prop) {
1463
- if (prop in self) return self[prop];
1464
- return (self[prop] = map(source, (a) => a[prop as keyof A]));
1465
- },
1466
- });
1467
- };
1468
-
1469
- export type Services<T> =
1470
- T extends RefSubject<infer _A, infer _E, infer R>
1471
- ? R
1472
- : T extends Computed<infer _A, infer _E, infer R>
1473
- ? R
1474
- : T extends Filtered<infer _A, infer _E, infer R>
1475
- ? R
1476
- : never;
1477
-
1478
- export type Error<T> =
1479
- T extends RefSubject<infer _A, infer E, infer _R>
1480
- ? E
1481
- : T extends Computed<infer _A, infer E, infer _R>
1482
- ? E
1483
- : T extends Filtered<infer _A, infer E, infer _R>
1484
- ? E
1485
- : never;
1486
-
1487
- export type Success<T> =
1488
- T extends RefSubject<infer A, infer _E, infer _R>
1489
- ? A
1490
- : T extends Computed<infer A, infer _E, infer _R>
1491
- ? A
1492
- : T extends Filtered<infer A, infer _E, infer _R>
1493
- ? A
1494
- : never;
1495
-
1496
- export type Identifier<T> =
1497
- T extends RefSubject.Service<infer R, infer _Id, infer _A, infer _E> ? R : never;
1498
-
1499
- /**
1500
- * Transforms a `RefSubject`, `Computed`, or `Filtered` using an `Effect`ful function.
1501
- *
1502
- * @example
1503
- * ```ts
1504
- * import { Effect } from "effect"
1505
- * import * as RefSubject from "@typed/fx/RefSubject"
1506
- *
1507
- * const program = Effect.gen(function* () {
1508
- * const count = yield* RefSubject.make(5)
1509
- *
1510
- * // Transform with an async operation
1511
- * const doubled = RefSubject.mapEffect(count, (n) =>
1512
- * Effect.succeed(n * 2)
1513
- * )
1514
- *
1515
- * const value = yield* doubled
1516
- * console.log(value) // 10
1517
- *
1518
- * // Update source
1519
- * yield* RefSubject.set(count, 7)
1520
- *
1521
- * // Computed automatically updates
1522
- * const newValue = yield* doubled
1523
- * console.log(newValue) // 14
1524
- * })
1525
- * ```
1526
- *
1527
- * @since 1.0.0
1528
- * @category combinators
1529
- */
1530
- export const mapEffect: {
1531
- <T extends RefSubject.Any | Computed.Any | Filtered.Any, B, E2, R2>(
1532
- f: (a: Success<T>) => Effect.Effect<B, E2, R2>,
1533
- ): (
1534
- ref: T,
1535
- ) => T extends Filtered.Any
1536
- ? Filtered<B, Error<T> | E2, Services<T> | R2>
1537
- : Computed<B, Error<T> | E2, Services<T> | R2>;
1538
-
1539
- <A, E, R, B, E2, R2>(
1540
- ref: RefSubject<A, E, R> | Computed<A, E, R>,
1541
- f: (a: A) => Effect.Effect<B, E2, R2>,
1542
- ): Computed<B, E | E2, R | R2>;
1543
-
1544
- <A, E, R, B, E2, R2>(
1545
- ref: Filtered<A, E, R>,
1546
- f: (a: A) => Effect.Effect<B, E2, R2>,
1547
- ): Filtered<B, E | E2, R | R2>;
1548
-
1549
- <R0, E0, A, E, R, E2, R2, C, E3, R3>(
1550
- versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
1551
- f: (a: A) => Effect.Effect<C, E3, R3>,
1552
- ): Computed<C, E0 | E | E2 | E3, R0 | R2 | R3 | Exclude<R, Scope.Scope>>;
1553
- } = dual(2, function mapEffect<
1554
- R0,
1555
- E0,
1556
- A,
1557
- E,
1558
- R,
1559
- E2,
1560
- R2,
1561
- C,
1562
- E3,
1563
- R3,
1564
- >(versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>, f: (a: A) => Effect.Effect<C, E3, R3>):
1565
- | Computed<C, E0 | E | E2 | E3, R0 | Exclude<R, Scope.Scope> | R2 | R3>
1566
- | Filtered<C, E0 | E | E2 | E3, R0 | Exclude<R, Scope.Scope> | R2 | R3> {
1567
- return FilteredTypeId in versioned
1568
- ? new FilteredImpl(versioned, (a) => Effect.asSome(f(a)))
1569
- : new ComputedImpl(versioned, f);
1570
- });
1571
-
1572
- /**
1573
- * Transforms a `RefSubject`, `Computed`, or `Filtered` using a pure function.
1574
- *
1575
- * @example
1576
- * ```ts
1577
- * import { Effect } from "effect"
1578
- * import * as RefSubject from "@typed/fx/RefSubject"
1579
- *
1580
- * const program = Effect.gen(function* () {
1581
- * const count = yield* RefSubject.make(5)
1582
- *
1583
- * // Create a computed that doubles the count
1584
- * const doubled = RefSubject.map(count, (n) => n * 2)
1585
- *
1586
- * const value = yield* doubled
1587
- * console.log(value) // 10
1588
- *
1589
- * // Update source
1590
- * yield* RefSubject.set(count, 7)
1591
- *
1592
- * // Computed automatically updates
1593
- * const newValue = yield* doubled
1594
- * console.log(newValue) // 14
1595
- * })
1596
- * ```
1597
- *
1598
- * @since 1.0.0
1599
- * @category combinators
1600
- */
1601
- export const map: {
1602
- <T extends RefSubject.Any | Computed.Any | Filtered.Any, B>(
1603
- f: (a: Success<T>) => B,
1604
- ): (
1605
- ref: T,
1606
- ) => T extends Filtered.Any
1607
- ? Filtered<B, Error<T>, Services<T>>
1608
- : Computed<B, Error<T>, Services<T>>;
1609
-
1610
- <A, E, R, B>(ref: RefSubject<A, E, R> | Computed<A, E, R>, f: (a: A) => B): Computed<B, E, R>;
1611
- <A, E, R, B>(filtered: Filtered<A, E, R>, f: (a: A) => B): Filtered<B, E, R>;
1612
-
1613
- <R0, E0, A, E, R, B, E2, R2>(
1614
- versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
1615
- f: (a: A) => B,
1616
- ):
1617
- | Computed<B, E0 | E | E2, R0 | R2 | Exclude<R, Scope.Scope>>
1618
- | Filtered<B, E0 | E | E2, R0 | R2 | Exclude<R, Scope.Scope>>;
1619
- } = dual(2, function map<
1620
- R0,
1621
- E0,
1622
- A,
1623
- E,
1624
- R,
1625
- B,
1626
- E2,
1627
- R2,
1628
- >(versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>, f: (a: A) => B):
1629
- | Computed<B, E0 | E | E2, R0 | Exclude<R, Scope.Scope> | R2>
1630
- | Filtered<B, E0 | E | E2, R0 | Exclude<R, Scope.Scope> | R2> {
1631
- return mapEffect(versioned, (a) => Effect.succeed(f(a)));
1632
- });
1633
-
1634
- /**
1635
- * Filters and transforms a `RefSubject`, `Computed`, or `Filtered` using an `Effect`ful function that returns an `Option`.
1636
- *
1637
- * @example
1638
- * ```ts
1639
- * import { Effect, Option } from "effect"
1640
- * import * as RefSubject from "@typed/fx/RefSubject"
1641
- *
1642
- * const program = Effect.gen(function* () {
1643
- * const numbers = yield* RefSubject.make([1, 2, 3, 4, 5])
1644
- *
1645
- * // Find the first even number
1646
- * const firstEven = RefSubject.filterMapEffect(numbers, (arr) =>
1647
- * Effect.succeed(Option.fromNullable(arr.find((n) => n % 2 === 0)))
1648
- * )
1649
- *
1650
- * const value = yield* firstEven
1651
- * console.log(value) // 2
1652
- * })
1653
- * ```
1654
- *
1655
- * @since 1.0.0
1656
- * @category combinators
1657
- */
1658
- export const filterMapEffect: {
1659
- <A, B, E2, R2>(
1660
- f: (a: A) => Effect.Effect<Option.Option<B>, E2, R2>,
1661
- ): {
1662
- <E, R>(ref: RefSubject<A, E, R> | Computed<A, E, R>): Filtered<B, E | E2, R | R2>;
1663
- <E, R>(ref: Filtered<A, E, R>): Filtered<B, E | E2, R | R2>;
1664
- <R0, E0, B, E, R, E2, R2>(
1665
- versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
1666
- f: (a: A) => Effect.Effect<Option.Option<B>, E2, R2>,
1667
- ): Filtered<B, E0 | E | E2, R0 | R2>;
1668
- };
1669
-
1670
- <A, E, R, B, E2, R2>(
1671
- ref: RefSubject<A, E, R> | Computed<A, E, R> | Filtered<A, E, R>,
1672
- f: (a: A) => Effect.Effect<Option.Option<B>, E2, R2>,
1673
- ): Filtered<B, E | E2, R | R2>;
1674
- <R0, E0, A, E, R, B, E2, R2, R3, E3>(
1675
- versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
1676
- f: (a: A) => Effect.Effect<Option.Option<B>, E3, R3>,
1677
- ): Filtered<B, E0 | E | E2 | E3, R0 | R2 | R3 | Exclude<R, Scope.Scope>>;
1678
- } = dual(2, function filterMapEffect<
1679
- R0,
1680
- E0,
1681
- A,
1682
- E,
1683
- R,
1684
- B,
1685
- E2,
1686
- R2,
1687
- R3,
1688
- E3,
1689
- >(versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>, f: (a: A) => Effect.Effect<Option.Option<B>, E3, R3>): Filtered<
1690
- B,
1691
- E0 | E | E2 | E3,
1692
- R0 | Exclude<R, Scope.Scope> | R2 | R3
1693
- > {
1694
- return new FilteredImpl(versioned, f);
1695
- });
1696
-
1697
- /**
1698
- * Filters and transforms a `RefSubject`, `Computed`, or `Filtered` using a pure function that returns an `Option`.
1699
- *
1700
- * @example
1701
- * ```ts
1702
- * import { Effect, Option } from "effect"
1703
- * import * as RefSubject from "@typed/fx/RefSubject"
1704
- *
1705
- * const program = Effect.gen(function* () {
1706
- * const numbers = yield* RefSubject.make([1, 2, 3, 4, 5])
1707
- *
1708
- * // Get the first even number
1709
- * const firstEven = RefSubject.filterMap(numbers, (arr) =>
1710
- * Option.fromNullable(arr.find((n) => n % 2 === 0))
1711
- * )
1712
- *
1713
- * const value = yield* firstEven
1714
- * console.log(value) // 2
1715
- * })
1716
- * ```
1717
- *
1718
- * @since 1.0.0
1719
- * @category combinators
1720
- */
1721
- export const filterMap: {
1722
- <A, B>(
1723
- f: (a: A) => Option.Option<B>,
1724
- ): {
1725
- <E, R>(ref: RefSubject<A, E, R> | Computed<A, E, R> | Filtered<A, E, R>): Filtered<B, E, R>;
1726
- <R0, E0, B, E, R, E2, R2>(
1727
- versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
1728
- f: (a: A) => Option.Option<B>,
1729
- ): Filtered<B, E0 | E | E2, R0 | R2>;
1730
- };
1731
-
1732
- <R0, E0, A, E, R, B, E2, R2>(
1733
- versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>,
1734
- f: (a: A) => Option.Option<B>,
1735
- ): Filtered<B, E0 | E | E2, R0 | R2 | Exclude<R, Scope.Scope>>;
1736
-
1737
- <A, E, R, B>(
1738
- ref: RefSubject<A, E, R> | Computed<A, E, R> | Filtered<A, E, R>,
1739
- f: (a: A) => Option.Option<B>,
1740
- ): Filtered<B, E, R>;
1741
- } = dual(2, function filterMap<
1742
- R0,
1743
- E0,
1744
- A,
1745
- E,
1746
- R,
1747
- B,
1748
- E2,
1749
- R2,
1750
- >(versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>, f: (a: A) => Option.Option<B>): Filtered<
1751
- B,
1752
- E0 | E | E2,
1753
- R0 | Exclude<R, Scope.Scope> | R2
1754
- > {
1755
- return new FilteredImpl(versioned, (a) => Effect.succeed(f(a)));
1756
- });
1757
-
1758
- /**
1759
- * Converts a `Computed` or `Filtered` of `Option<A>` into a `Filtered<A>`, filtering out `None` values.
1760
- *
1761
- * @example
1762
- * ```ts
1763
- * import { Effect, Option } from "effect"
1764
- * import * as RefSubject from "@typed/fx/RefSubject"
1765
- *
1766
- * const program = Effect.gen(function* () {
1767
- * const maybeValue = yield* RefSubject.make(Option.some(42))
1768
- *
1769
- * // Compact the Option
1770
- * const filtered = RefSubject.compact(maybeValue)
1771
- *
1772
- * const value = yield* filtered
1773
- * console.log(value) // 42
1774
- *
1775
- * // If the Option becomes None, the Filtered will fail
1776
- * yield* RefSubject.set(maybeValue, Option.none())
1777
- * // yield* filtered would fail with NoSuchElementError
1778
- * })
1779
- * ```
1780
- *
1781
- * @since 1.0.0
1782
- * @category combinators
1783
- */
1784
- export const compact: {
1785
- <A, E, R>(ref: Computed<Option.Option<A>, E, R>): Filtered<A>;
1786
- <A, E, R>(ref: Filtered<Option.Option<A>, E, R>): Filtered<A>;
1787
-
1788
- <R0, E0, A, E, R, E2, R2>(
1789
- versioned: Versioned.Versioned<R0, E0, Option.Option<A>, E, R, Option.Option<A>, E2, R2>,
1790
- ): Filtered<
1791
- A,
1792
- E0 | E | Exclude<E, Cause.NoSuchElementError> | Exclude<E2, Cause.NoSuchElementError>,
1793
- R0 | R2 | Exclude<R, Scope.Scope>
1794
- >;
1795
- } = function compact<R0, E0, A, E, R, E2, R2>(
1796
- versioned: Versioned.Versioned<R0, E0, Option.Option<A>, E, R, Option.Option<A>, E2, R2>,
1797
- ): any {
1798
- return new FilteredImpl(versioned, Effect.succeed);
1799
- };
1800
-
1801
- /**
1802
- * Returns a `Computed` that yields the value inside the `Option`, or the fallback when `None`.
1803
- * Works with `Computed<Option<A>>` (e.g. from `fromOption` / `fromNullable`) and with `Filtered<A>`.
1804
- *
1805
- * @example
1806
- * ```ts
1807
- * import { Effect, Option } from "effect"
1808
- * import * as RefSubject from "@typed/fx/RefSubject"
1809
- *
1810
- * const program = Effect.gen(function* () {
1811
- * const ref = yield* RefSubject.fromOption(Option.some(42))
1812
- * const withDefault = RefSubject.getOrElse(ref, () => 0)
1813
- * expect(yield* withDefault).toBe(42)
1814
- *
1815
- * const empty = yield* RefSubject.fromNullable(null)
1816
- * const fallback = RefSubject.getOrElse(empty, () => 99)
1817
- * expect(yield* fallback).toBe(99)
1818
- * })
1819
- * ```
1820
- *
1821
- * @since 1.0.0
1822
- * @category combinators
1823
- */
1824
- export const getOrElse: {
1825
- <A>(
1826
- fallback: () => A,
1827
- ): <E, R>(ref: Computed<Option.Option<A>, E, R> | Filtered<A, E, R>) => Computed<A, E, R>;
1828
- <A, E, R>(
1829
- ref: Computed<Option.Option<A>, E, R> | Filtered<A, E, R>,
1830
- fallback: () => A,
1831
- ): Computed<A, E, R>;
1832
- } = dual(2, function getOrElse<
1833
- A,
1834
- E,
1835
- R,
1836
- >(ref: Computed<Option.Option<A>, E, R> | Filtered<A, E, R>, fallback: () => A): Computed<A, E, R> {
1837
- const computed = FilteredTypeId in ref ? (ref as Filtered<A, E, R>).asComputed() : ref;
1838
- return map(computed, (opt) => Option.getOrElse(opt, fallback));
1839
- });
1840
-
1841
- class RefSubjectSimpleTransform<A, E, R, R2, R3>
1842
- extends YieldableFx<A, E, R | R2 | Scope.Scope, A, E, R | R3>
1843
- implements RefSubject<A, E, R | R2 | R3>
1844
- {
1845
- readonly [ComputedTypeId]: ComputedTypeId = ComputedTypeId;
1846
- readonly [RefSubjectTypeId]: RefSubjectTypeId = RefSubjectTypeId;
1847
-
1848
- readonly version: Effect.Effect<number, E, R>;
1849
- readonly interrupt: Effect.Effect<void, never, R>;
1850
- readonly subscriberCount: Effect.Effect<number, never, R>;
1851
- private _fx: Fx<A, E, Scope.Scope | R | R2>;
1852
-
1853
- readonly ref: RefSubject<A, E, R>;
1854
- readonly transformFx: (fx: Fx<A, E, Scope.Scope | R>) => Fx<A, E, Scope.Scope | R | R2>;
1855
- readonly transformEffect: (effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | R3>;
1856
-
1857
- constructor(
1858
- ref: RefSubject<A, E, R>,
1859
- transformFx: (fx: Fx<A, E, Scope.Scope | R>) => Fx<A, E, Scope.Scope | R | R2>,
1860
- transformEffect: (effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | R3>,
1861
- ) {
1862
- super();
1863
-
1864
- this.ref = ref;
1865
- this.transformFx = transformFx;
1866
- this.transformEffect = transformEffect;
1867
- this.version = ref.version;
1868
- this.interrupt = ref.interrupt;
1869
- this.subscriberCount = ref.subscriberCount;
1870
-
1871
- this._fx = transformFx(ref);
1872
- }
1873
-
1874
- run<R4>(sink: Sink.Sink<A, E, R4>) {
1875
- return this._fx.run(sink);
1876
- }
1877
-
1878
- toEffect(): Effect.Effect<A, E, R | R3> {
1879
- return this.transformEffect(this.ref.asEffect());
1880
- }
1881
-
1882
- updates<E2, R2, C>(
1883
- run: (ref: GetSetDelete<A, E, R>) => Effect.Effect<C, E2, R2>,
1884
- ): Effect.Effect<C, E | E2, R | R2> {
1885
- return this.ref.updates(run);
1886
- }
1887
-
1888
- onFailure(cause: Cause.Cause<E>): Effect.Effect<unknown, never, R> {
1889
- return this.ref.onFailure(cause);
1890
- }
1891
-
1892
- onSuccess(value: A): Effect.Effect<unknown, never, R> {
1893
- return this.ref.onSuccess(value);
1894
- }
1895
- }
1896
-
1897
- export const slice: {
1898
- (skip: number, take: number): <A, E, R>(ref: RefSubject<A, E, R>) => RefSubject<A, E, R>;
1899
- <A, E, R>(ref: RefSubject<A, E, R>, skip: number, take: number): RefSubject<A, E, R>;
1900
- } = dual(3, function slice<
1901
- A,
1902
- E,
1903
- R,
1904
- >(ref: RefSubject<A, E, R>, skip: number, take: number): RefSubject<A, E, R> {
1905
- return new RefSubjectSimpleTransform(ref, (_) => fxSlice(_, { skip, take }), identity);
1906
- });
1907
-
1908
- class RefSubjectTransform<A, B, E, R>
1909
- extends YieldableFx<B, E, R | Scope.Scope, B, E, R>
1910
- implements RefSubject<B, E, R>
1911
- {
1912
- readonly [ComputedTypeId]: ComputedTypeId = ComputedTypeId;
1913
- readonly [RefSubjectTypeId]: RefSubjectTypeId = RefSubjectTypeId;
1914
-
1915
- readonly version: Effect.Effect<number, E, R>;
1916
- readonly interrupt: Effect.Effect<void, never, R>;
1917
- readonly subscriberCount: Effect.Effect<number, never, R>;
1918
- private _fx: Fx<B, E, Scope.Scope | R>;
1919
-
1920
- readonly ref: RefSubject<A, E, R>;
1921
- readonly toB: (a: A) => B;
1922
- readonly toA: (b: B) => A;
1923
-
1924
- constructor(ref: RefSubject<A, E, R>, toB: (a: A) => B, toA: (b: B) => A) {
1925
- super();
1926
-
1927
- this.ref = ref;
1928
- this.toB = toB;
1929
- this.toA = toA;
1930
- this.version = ref.version;
1931
- this.interrupt = ref.interrupt;
1932
- this.subscriberCount = ref.subscriberCount;
1933
-
1934
- this._fx = fxMapEffect(ref, (a) => Effect.succeed(toB(a)));
1935
- }
1936
-
1937
- run<R2>(sink: Sink.Sink<B, E, R2>) {
1938
- return this._fx.run(sink);
1939
- }
1940
-
1941
- toEffect(): Effect.Effect<B, E, R> {
1942
- return Effect.map(this.ref.asEffect(), this.toB);
1943
- }
1944
-
1945
- updates<E2, R2, C>(
1946
- run: (ref: GetSetDelete<B, E, R>) => Effect.Effect<C, E2, R2>,
1947
- ): Effect.Effect<C, E | E2, R | R2> {
1948
- return this.ref.updates((innerRef) => {
1949
- const getSetDelete: GetSetDelete<B, E, R> = {
1950
- get: Effect.map(innerRef.get, this.toB),
1951
- set: (b: B) => Effect.map(innerRef.set(this.toA(b)), this.toB),
1952
- delete: Effect.map(innerRef.delete, Option.map(this.toB)),
1953
- };
1954
- return run(getSetDelete);
1955
- });
1956
- }
1957
-
1958
- onFailure(cause: Cause.Cause<E>): Effect.Effect<unknown, never, R> {
1959
- return this.ref.onFailure(cause);
1960
- }
1961
-
1962
- onSuccess(value: B): Effect.Effect<unknown, never, R> {
1963
- return this.ref.onSuccess(this.toA(value));
1964
- }
1965
- }
1966
-
1967
- /**
1968
- * Transforms a `RefSubject` invariantly using bidirectional mapping functions.
1969
- *
1970
- * @example
1971
- * ```ts
1972
- * import { Effect } from "effect"
1973
- * import * as RefSubject from "@typed/fx/RefSubject"
1974
- *
1975
- * const program = Effect.gen(function* () {
1976
- * const count = yield* RefSubject.make(5)
1977
- *
1978
- * // Transform to string and back
1979
- * const countStr = RefSubject.transform(
1980
- * count,
1981
- * (n) => n.toString(),
1982
- * (s) => parseInt(s, 10)
1983
- * )
1984
- *
1985
- * const value = yield* countStr
1986
- * console.log(value) // "5"
1987
- *
1988
- * // Set using the transformed type
1989
- * yield* RefSubject.set(countStr, "10")
1990
- *
1991
- * // Original reflects the change
1992
- * const original = yield* count
1993
- * console.log(original) // 10
1994
- * })
1995
- * ```
1996
- *
1997
- * @since 1.0.0
1998
- * @category combinators
1999
- */
2000
- export const transform: {
2001
- <A, B>(
2002
- toB: (a: A) => B,
2003
- toA: (b: B) => A,
2004
- ): <E, R>(ref: RefSubject<A, E, R>) => RefSubject<B, E, R>;
2005
- <A, E, R, B>(ref: RefSubject<A, E, R>, toB: (a: A) => B, toA: (b: B) => A): RefSubject<B, E, R>;
2006
- } = dual(3, function transform<
2007
- A,
2008
- E,
2009
- R,
2010
- B,
2011
- >(ref: RefSubject<A, E, R>, toB: (a: A) => B, toA: (b: B) => A): RefSubject<B, E, R> {
2012
- return new RefSubjectTransform(ref, toB, toA);
2013
- });
2014
-
2015
- type RefKind = "r" | "c" | "f";
2016
-
2017
- const join = (a: RefKind, b: RefKind) => {
2018
- if (a === "r") return b;
2019
- if (b === "r") return a;
2020
- if (a === "f") return a;
2021
- if (b === "f") return b;
2022
- return "c";
2023
- };
2024
-
2025
- function getRefKind<
2026
- const Refs extends ReadonlyArray<
2027
- RefSubject<any, any, any> | Computed<any, any, any> | Filtered<any, any, any>
2028
- >,
2029
- >(refs: Refs): RefKind {
2030
- let kind: RefKind = "r";
2031
-
2032
- for (const ref of refs) {
2033
- if (FilteredTypeId in ref) {
2034
- kind = "f";
2035
- break;
2036
- } else if (!(RefSubjectTypeId in ref)) {
2037
- kind = join(kind, "c");
2038
- }
2039
- }
2040
-
2041
- return kind;
2042
- }
2043
-
2044
- type StructFrom<
2045
- Refs extends Readonly<Record<string, RefSubject.Any | Computed.Any | Filtered.Any>>,
2046
- > = {
2047
- c: [ComputedStructFrom<Refs>] extends [Computed<infer A, infer E, infer R>]
2048
- ? Computed<A, E, R>
2049
- : never;
2050
- f: [FilteredStructFrom<Refs>] extends [Filtered<infer A, infer E, infer R>]
2051
- ? Filtered<A, E, R>
2052
- : never;
2053
- r: [RefSubjectStructFrom<Refs>] extends [RefSubject<infer A, infer E, infer R>]
2054
- ? RefSubject<A, E, R>
2055
- : never;
2056
- }[GetStructKind<Refs>];
2057
-
2058
- type GetStructKind<
2059
- Refs extends Readonly<Record<string, RefSubject.Any | Computed.Any | Filtered.Any>>,
2060
- > = MergeKinds<
2061
- UnionToTuple<
2062
- {
2063
- [K in keyof Refs]: MatchKind<Refs[K]>;
2064
- }[keyof Refs]
2065
- >
2066
- >;
2067
-
2068
- type Ref = RefSubject.Any | Computed.Any | Filtered.Any;
2069
-
2070
- type MatchKind<T extends Ref> = [T] extends [Filtered.Any]
2071
- ? "f"
2072
- : [T] extends [RefSubject.Any]
2073
- ? "r"
2074
- : "c";
2075
-
2076
- type MergeKind<A extends RefKind, B extends RefKind> = A extends "f"
2077
- ? A
2078
- : B extends "f"
2079
- ? B
2080
- : A extends "r"
2081
- ? B
2082
- : B extends "r"
2083
- ? A
2084
- : "c";
2085
-
2086
- type MergeKinds<Kinds extends ReadonlyArray<any>> = Kinds extends readonly [
2087
- infer Head extends RefKind,
2088
- ...infer Tail extends ReadonlyArray<RefKind>,
2089
- ]
2090
- ? MergeKind<Head, MergeKinds<Tail>>
2091
- : "r";
2092
-
2093
- type FilteredStructFrom<
2094
- Refs extends Readonly<Record<string, RefSubject.Any | Computed.Any | Filtered.Any>>,
2095
- > = Filtered<
2096
- {
2097
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2098
- },
2099
- FxError<Refs[keyof Refs]>,
2100
- Effect.Services<Refs[keyof Refs]>
2101
- >;
2102
-
2103
- type ComputedStructFrom<
2104
- Refs extends Readonly<Record<string, RefSubject.Any | Computed.Any | Filtered.Any>>,
2105
- > = Computed<
2106
- {
2107
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2108
- },
2109
- Effect.Error<Refs[keyof Refs]>,
2110
- Effect.Services<Refs[keyof Refs]>
2111
- >;
2112
-
2113
- type RefSubjectStructFrom<
2114
- Refs extends Readonly<Record<string, RefSubject.Any | Computed.Any | Filtered.Any>>,
2115
- > = RefSubject<
2116
- {
2117
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2118
- },
2119
- Effect.Error<Refs[keyof Refs]>,
2120
- Effect.Services<Refs[keyof Refs]>
2121
- >;
2122
-
2123
- type TupleFrom<
2124
- Refs extends ReadonlyArray<
2125
- RefSubject<any, any, any> | Computed<any, any, any> | Filtered<any, any, any>
2126
- >,
2127
- > = {
2128
- c: [ComputedTupleFrom<Refs>] extends [Computed<infer A, infer E, infer R>]
2129
- ? Computed<A, E, R>
2130
- : never;
2131
- f: [FilteredTupleFrom<Refs>] extends [Filtered<infer A, infer E, infer R>]
2132
- ? Filtered<A, E, R>
2133
- : never;
2134
- r: [RefSubjectTupleFrom<Refs>] extends [RefSubject<infer A, infer E, infer R>]
2135
- ? RefSubject<A, E, R>
2136
- : never;
2137
- }[GetTupleKind<Refs>];
2138
-
2139
- type GetTupleKind<
2140
- Refs extends ReadonlyArray<Ref>,
2141
- Kind extends RefKind = "r",
2142
- > = Refs extends readonly [infer Head extends Ref, ...infer Tail extends ReadonlyArray<Ref>]
2143
- ? GetTupleKind<Tail, MergeKind<Kind, MatchKind<Head>>>
2144
- : Kind;
2145
-
2146
- type FilteredTupleFrom<
2147
- Refs extends ReadonlyArray<
2148
- RefSubject<any, any, any> | Computed<any, any, any> | Filtered<any, any, any>
2149
- >,
2150
- > = Filtered<
2151
- {
2152
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2153
- },
2154
- FxError<Refs[number]>,
2155
- Effect.Services<Refs[number]>
2156
- >;
2157
-
2158
- type ComputedTupleFrom<
2159
- Refs extends ReadonlyArray<
2160
- RefSubject<any, any, any> | Computed<any, any, any> | Filtered<any, any, any>
2161
- >,
2162
- > = Computed<
2163
- {
2164
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2165
- },
2166
- Effect.Error<Refs[number]>,
2167
- Effect.Services<Refs[number]>
2168
- >;
2169
-
2170
- type RefSubjectTupleFrom<
2171
- Refs extends ReadonlyArray<
2172
- RefSubject<any, any, any> | Computed<any, any, any> | Filtered<any, any, any>
2173
- >,
2174
- > = RefSubject<
2175
- {
2176
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2177
- },
2178
- Effect.Error<Refs[number]>,
2179
- Effect.Services<Refs[number]>
2180
- >;
2181
-
2182
- /**
2183
- * Combines multiple `RefSubject`, `Computed`, or `Filtered` instances into a single struct.
2184
- *
2185
- * @example
2186
- * ```ts
2187
- * import { Effect } from "effect"
2188
- * import * as RefSubject from "@typed/fx/RefSubject"
2189
- *
2190
- * const program = Effect.gen(function* () {
2191
- * const firstName = yield* RefSubject.make("Alice")
2192
- * const lastName = yield* RefSubject.make("Smith")
2193
- * const age = yield* RefSubject.make(30)
2194
- *
2195
- * // Combine into a struct
2196
- * const person = RefSubject.struct({
2197
- * firstName,
2198
- * lastName,
2199
- * age
2200
- * })
2201
- *
2202
- * const fullPerson = yield* person
2203
- * console.log(fullPerson) // { firstName: "Alice", lastName: "Smith", age: 30 }
2204
- *
2205
- * // Update one field
2206
- * yield* RefSubject.set(firstName, "Bob")
2207
- *
2208
- * // Struct automatically updates
2209
- * const updated = yield* person
2210
- * console.log(updated.firstName) // "Bob"
2211
- * })
2212
- * ```
2213
- *
2214
- * @since 1.0.0
2215
- * @category combinators
2216
- */
2217
- export function struct<
2218
- const Refs extends Readonly<Record<string, RefSubject.Any | Computed.Any | Filtered.Any>>,
2219
- >(refs: Refs): StructFrom<Refs> {
2220
- const kind = getRefKind(Object.values(refs));
2221
- switch (kind) {
2222
- case "r":
2223
- return makeStructRef(refs as any) as StructFrom<Refs>;
2224
- case "c":
2225
- return makeStructComputed(refs as any) as StructFrom<Refs>;
2226
- case "f":
2227
- return makeStructFiltered(refs as any) as any as StructFrom<Refs>;
2228
- }
2229
- }
2230
- /**
2231
- * Combines multiple `RefSubject`, `Computed`, or `Filtered` instances into a single tuple.
2232
- *
2233
- * @example
2234
- * ```ts
2235
- * import { Effect } from "effect"
2236
- * import * as RefSubject from "@typed/fx/RefSubject"
2237
- *
2238
- * const program = Effect.gen(function* () {
2239
- * const x = yield* RefSubject.make(10)
2240
- * const y = yield* RefSubject.make(20)
2241
- * const z = yield* RefSubject.make(30)
2242
- *
2243
- * // Combine into a tuple
2244
- * const point = RefSubject.tuple([x, y, z])
2245
- *
2246
- * const coords = yield* point
2247
- * console.log(coords) // [10, 20, 30]
2248
- *
2249
- * // Update one value
2250
- * yield* RefSubject.set(x, 15)
2251
- *
2252
- * // Tuple automatically updates
2253
- * const updated = yield* point
2254
- * console.log(updated) // [15, 20, 30]
2255
- * })
2256
- * ```
2257
- *
2258
- * @since 1.0.0
2259
- * @category combinators
2260
- */
2261
- export function tuple<const Refs extends ReadonlyArray<Ref>>(refs: Refs): TupleFrom<Refs> {
2262
- const kind = getRefKind(refs);
2263
- switch (kind) {
2264
- case "r":
2265
- return makeTupleRef(refs as any) as TupleFrom<Refs>;
2266
- case "c":
2267
- return makeTupleComputed(refs as any) as TupleFrom<Refs>;
2268
- case "f":
2269
- return makeTupleFiltered(refs as any) as any as TupleFrom<Refs>;
2270
- }
2271
- }
2272
-
2273
- function makeTupleRef<const Refs extends ReadonlyArray<RefSubject<any, any, any>>>(
2274
- refs: Refs,
2275
- ): RefSubjectTupleFrom<Refs> {
2276
- return new RefSubjectTuple(refs);
2277
- }
2278
-
2279
- const UNBOUNDED = { concurrency: "unbounded" } as const;
2280
-
2281
- class RefSubjectTuple<const Refs extends ReadonlyArray<RefSubject<any, any, any>>>
2282
- extends YieldableFx<
2283
- {
2284
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2285
- },
2286
- Effect.Error<Refs[number]>,
2287
- Effect.Services<Refs[number]>,
2288
- {
2289
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2290
- },
2291
- Effect.Error<Refs[number]>,
2292
- Effect.Services<Refs[number]>
2293
- >
2294
- implements RefSubjectTupleFrom<Refs>
2295
- {
2296
- readonly [ComputedTypeId]: ComputedTypeId = ComputedTypeId;
2297
- readonly [RefSubjectTypeId]: RefSubjectTypeId = RefSubjectTypeId;
2298
-
2299
- readonly version: Effect.Effect<
2300
- number,
2301
- Effect.Error<Refs[number]>,
2302
- Effect.Services<Refs[number]>
2303
- >;
2304
- readonly interrupt: Effect.Effect<void, never, Effect.Services<Refs[number]>>;
2305
- readonly subscriberCount: Effect.Effect<number, never, Effect.Services<Refs[number]>>;
2306
-
2307
- private versioned: Versioned.Versioned<
2308
- Effect.Services<Refs[number]>,
2309
- Effect.Error<Refs[number]>,
2310
- { readonly [K in keyof Refs]: Effect.Success<Refs[K]> },
2311
- Effect.Error<Refs[number]>,
2312
- Effect.Services<Refs[number]>,
2313
- { readonly [K in keyof Refs]: Effect.Success<Refs[K]> },
2314
- Effect.Error<Refs[number]>,
2315
- Effect.Services<Refs[number]>
2316
- >;
2317
-
2318
- private getSetDelete: GetSetDelete<
2319
- { readonly [K in keyof Refs]: Effect.Success<Refs[K]> },
2320
- Effect.Error<Refs[number]>,
2321
- Effect.Services<Refs[number]>
2322
- >;
2323
-
2324
- readonly refs: Refs;
2325
-
2326
- constructor(refs: Refs) {
2327
- super();
2328
-
2329
- this.refs = refs;
2330
- this.versioned = Versioned.hold(Versioned.tuple(refs)) as any;
2331
- this.version = this.versioned.version;
2332
- this.interrupt = Effect.all(
2333
- refs.map((r) => r.interrupt),
2334
- UNBOUNDED,
2335
- );
2336
- this.subscriberCount = Effect.map(
2337
- Effect.all(
2338
- refs.map((r) => r.subscriberCount),
2339
- UNBOUNDED,
2340
- ),
2341
- Array.reduce(0, sum),
2342
- );
2343
-
2344
- this.getSetDelete = {
2345
- get: this.versioned.asEffect(),
2346
- set: (a) =>
2347
- Effect.all(
2348
- refs.map((r, i) => set(r, a[i])),
2349
- UNBOUNDED,
2350
- ) as any,
2351
- delete: Effect.map(
2352
- Effect.all(
2353
- refs.map((r) => reset(r)),
2354
- UNBOUNDED,
2355
- ),
2356
- Option.all,
2357
- ) as any,
2358
- };
2359
-
2360
- this.updates = this.updates.bind(this);
2361
- this.onFailure = this.onFailure.bind(this);
2362
- this.onSuccess = this.onSuccess.bind(this);
2363
- }
2364
-
2365
- run<R2 = never>(
2366
- sink: Sink.Sink<
2367
- {
2368
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2369
- },
2370
- Effect.Error<Refs[number]>,
2371
- R2
2372
- >,
2373
- ): Effect.Effect<unknown, never, Effect.Services<Refs[number]> | R2> {
2374
- return this.versioned.run(sink);
2375
- }
2376
-
2377
- override toEffect(): Effect.Effect<
2378
- { readonly [K in keyof Refs]: Effect.Success<Refs[K]> },
2379
- Effect.Error<Refs[number]>,
2380
- Effect.Services<Refs[number]>
2381
- > {
2382
- return this.versioned.asEffect();
2383
- }
2384
-
2385
- updates<E2, R2, C>(
2386
- run: (
2387
- ref: GetSetDelete<
2388
- {
2389
- readonly [K in keyof Refs]: Effect.Success<Refs[K]>;
2390
- },
2391
- Effect.Error<Refs[number]>,
2392
- Effect.Services<Refs[number]>
2393
- >,
2394
- ) => Effect.Effect<C, E2, R2>,
2395
- ) {
2396
- return run(this.getSetDelete);
2397
- }
2398
-
2399
- onFailure(
2400
- cause: Cause.Cause<Effect.Error<Refs[number]>>,
2401
- ): Effect.Effect<unknown, never, Effect.Services<Refs[number]>> {
2402
- return Effect.all(this.refs.map((ref) => ref.onFailure(cause)));
2403
- }
2404
-
2405
- onSuccess(value: { readonly [K in keyof Refs]: Effect.Success<Refs[K]> }): Effect.Effect<
2406
- unknown,
2407
- never,
2408
- Effect.Services<Refs[number]>
2409
- > {
2410
- return Effect.catchCause(this.getSetDelete.set(value), (c) => this.onFailure(c));
2411
- }
2412
- }
2413
-
2414
- function makeTupleComputed<const Refs extends ReadonlyArray<Computed<any, any, any>>>(
2415
- refs: Refs,
2416
- ): ComputedTupleFrom<Refs> {
2417
- return new ComputedImpl(Versioned.tuple(refs) as any, Effect.succeed) as any;
2418
- }
2419
-
2420
- function makeTupleFiltered<
2421
- const Refs extends ReadonlyArray<Computed<any, any, any> | Filtered<any, any, any>>,
2422
- >(refs: Refs): FilteredTupleFrom<Refs> {
2423
- return new FilteredImpl(Versioned.tuple(refs) as any, Effect.succeedSome) as any;
2424
- }
2425
-
2426
- function makeStructRef<const Refs extends Readonly<Record<string, RefSubject.Any>>>(
2427
- refs: Refs,
2428
- ): RefSubjectStructFrom<Refs> {
2429
- return new RefSubjectStruct(refs) as any;
2430
- }
2431
-
2432
- class RefSubjectStruct<const Refs extends Readonly<Record<string, RefSubject.Any>>>
2433
- extends YieldableFx<
2434
- {
2435
- readonly [K in keyof Refs]: Success<Refs[K]>;
2436
- },
2437
- Error<Refs[keyof Refs]>,
2438
- Services<Refs[keyof Refs]> | Scope.Scope,
2439
- {
2440
- readonly [K in keyof Refs]: Success<Refs[K]>;
2441
- },
2442
- Error<Refs[keyof Refs]>,
2443
- Services<Refs[keyof Refs]>
2444
- >
2445
- implements
2446
- RefSubject<
2447
- {
2448
- readonly [K in keyof Refs]: Success<Refs[K]>;
2449
- },
2450
- Error<Refs[keyof Refs]>,
2451
- Services<Refs[keyof Refs]>
2452
- >
2453
- {
2454
- readonly [ComputedTypeId]: ComputedTypeId = ComputedTypeId;
2455
- readonly [RefSubjectTypeId]: RefSubjectTypeId = RefSubjectTypeId;
2456
-
2457
- readonly version: Effect.Effect<number, Error<Refs[keyof Refs]>, Services<Refs[keyof Refs]>>;
2458
- readonly interrupt: Effect.Effect<void, never, Services<Refs[keyof Refs]>>;
2459
- readonly subscriberCount: Effect.Effect<number, never, Services<Refs[keyof Refs]>>;
2460
-
2461
- private versioned: Versioned.Versioned<
2462
- Services<Refs[keyof Refs]>,
2463
- Error<Refs[keyof Refs]>,
2464
- { readonly [K in keyof Refs]: Success<Refs[K]> },
2465
- Error<Refs[keyof Refs]>,
2466
- Services<Refs[keyof Refs]>,
2467
- { readonly [K in keyof Refs]: Success<Refs[K]> },
2468
- Error<Refs[keyof Refs]>,
2469
- Services<Refs[keyof Refs]>
2470
- >;
2471
-
2472
- private getSetDelete: GetSetDelete<
2473
- { readonly [K in keyof Refs]: Success<Refs[K]> },
2474
- Error<Refs[keyof Refs]>,
2475
- Services<Refs[keyof Refs]>
2476
- >;
2477
-
2478
- readonly refs: Refs;
2479
-
2480
- constructor(refs: Refs) {
2481
- super();
2482
-
2483
- this.refs = refs;
2484
- this.versioned = Versioned.hold(Versioned.struct(refs)) as any;
2485
- this.version = this.versioned.version;
2486
- this.interrupt = Effect.all(
2487
- Object.values(refs).map((r) => r.interrupt),
2488
- UNBOUNDED,
2489
- );
2490
- this.subscriberCount = Effect.map(
2491
- Effect.all(
2492
- Object.values(refs).map((r) => r.subscriberCount),
2493
- UNBOUNDED,
2494
- ),
2495
- Array.reduce(0, sum),
2496
- );
2497
-
2498
- this.getSetDelete = {
2499
- get: this.versioned.asEffect(),
2500
- set: (a) =>
2501
- Effect.all(
2502
- Object.keys(refs).map((k) => set(refs[k] as any, a[k])),
2503
- UNBOUNDED,
2504
- ) as any,
2505
- delete: Effect.map(
2506
- Effect.all(
2507
- Object.values(refs).map((r) => reset(r as any)),
2508
- UNBOUNDED,
2509
- ),
2510
- Option.all,
2511
- ) as any,
2512
- };
2513
-
2514
- this.updates = this.updates.bind(this);
2515
- this.onFailure = this.onFailure.bind(this);
2516
- this.onSuccess = this.onSuccess.bind(this);
2517
- }
2518
-
2519
- run<R3 = never>(
2520
- sink: Sink.Sink<{ readonly [K in keyof Refs]: Success<Refs[K]> }, Error<Refs[keyof Refs]>, R3>,
2521
- ): Effect.Effect<unknown, never, Services<Refs[keyof Refs]> | Scope.Scope | R3> {
2522
- return this.versioned.run(sink as any) as any;
2523
- }
2524
-
2525
- toEffect() {
2526
- return this.versioned.asEffect();
2527
- }
2528
-
2529
- updates<E2, R2, C>(
2530
- run: (
2531
- ref: GetSetDelete<
2532
- {
2533
- readonly [K in keyof Refs]: Success<Refs[K]>;
2534
- },
2535
- Error<Refs[keyof Refs]>,
2536
- Services<Refs[keyof Refs]>
2537
- >,
2538
- ) => Effect.Effect<C, E2, R2>,
2539
- ) {
2540
- return run(this.getSetDelete);
2541
- }
2542
-
2543
- onFailure(
2544
- cause: Cause.Cause<Error<Refs[keyof Refs]>>,
2545
- ): Effect.Effect<unknown, never, Services<Refs[keyof Refs]>> {
2546
- return Effect.all(Object.values(this.refs).map((ref) => ref.onFailure(cause as any)));
2547
- }
2548
-
2549
- onSuccess(value: { readonly [K in keyof Refs]: Success<Refs[K]> }): Effect.Effect<
2550
- unknown,
2551
- never,
2552
- Services<Refs[keyof Refs]>
2553
- > {
2554
- return Effect.catchCause(this.getSetDelete.set(value), (c) => this.onFailure(c));
2555
- }
2556
- }
2557
-
2558
- function makeStructComputed<const Refs extends Readonly<Record<string, Computed<any, any, any>>>>(
2559
- refs: Refs,
2560
- ): ComputedStructFrom<Refs> {
2561
- return new ComputedImpl(Versioned.struct(refs) as any, Effect.succeed) as any;
2562
- }
2563
-
2564
- function makeStructFiltered<
2565
- const Refs extends Readonly<Record<string, Computed<any, any, any> | Filtered<any, any, any>>>,
2566
- >(refs: Refs): FilteredStructFrom<Refs> {
2567
- return new FilteredImpl(Versioned.struct(refs) as any, Effect.succeedSome) as any;
2568
- }
2569
-
2570
- export function computedFromService<R, A, E, R2>(
2571
- effect: Effect.Effect<Computed<A, E, R2>, never, R>,
2572
- ): Computed<A, E, R | R2> {
2573
- return new ComputedFromService(effect);
2574
- }
2575
-
2576
- class ComputedFromService<R, A, E, R2>
2577
- extends YieldableFx<A, E, R | R2 | Scope.Scope, A, E, R | R2>
2578
- implements Computed<A, E, R | R2>
2579
- {
2580
- readonly [ComputedTypeId]: ComputedTypeId = ComputedTypeId;
2581
-
2582
- private readonly effect: Effect.Effect<Computed<A, E, R2>, never, R>;
2583
- readonly version: Effect.Effect<number, E, R | R2>;
2584
- readonly interrupt: Effect.Effect<void, never, R | R2>;
2585
-
2586
- constructor(effect: Effect.Effect<Computed<A, E, R2>, never, R>) {
2587
- super();
2588
- this.effect = effect;
2589
- this.version = Effect.flatMap(this.effect, (c) => c.version);
2590
- this.interrupt = Effect.flatMap(this.effect, (c) => c.interrupt);
2591
- }
2592
-
2593
- run<RSink>(
2594
- sink: Sink.Sink<A, E, RSink>,
2595
- ): Effect.Effect<unknown, never, R | R2 | RSink | Scope.Scope> {
2596
- return Effect.flatMap(this.effect, (c) => c.run(sink));
2597
- }
2598
-
2599
- toEffect(): Effect.Effect<A, E, R | R2> {
2600
- return Effect.flatMap(this.effect, (c) => c.asEffect());
2601
- }
2602
- }
2603
-
2604
- export function filteredFromService<R, A, E, R2>(
2605
- effect: Effect.Effect<Filtered<A, E, R2>, never, R>,
2606
- ): Filtered<A, E, R | R2> {
2607
- return new FilteredFromService(effect);
2608
- }
2609
-
2610
- class FilteredFromService<R, A, E, R2>
2611
- extends YieldableFx<A, E, R | R2 | Scope.Scope, A, E | Cause.NoSuchElementError, R | R2>
2612
- implements Filtered<A, E, R | R2>
2613
- {
2614
- readonly [FilteredTypeId]: FilteredTypeId = FilteredTypeId;
2615
-
2616
- private readonly effect: Effect.Effect<Filtered<A, E, R2>, never, R>;
2617
- readonly version: Effect.Effect<number, E, R | R2>;
2618
- readonly interrupt: Effect.Effect<void, never, R | R2>;
2619
-
2620
- constructor(effect: Effect.Effect<Filtered<A, E, R2>, never, R>) {
2621
- super();
2622
- this.effect = effect;
2623
- this.version = Effect.flatMap(this.effect, (c) => c.version);
2624
- this.interrupt = Effect.flatMap(this.effect, (c) => c.interrupt);
2625
- }
2626
-
2627
- run<RSink>(
2628
- sink: Sink.Sink<A, E, RSink>,
2629
- ): Effect.Effect<unknown, never, R | R2 | RSink | Scope.Scope> {
2630
- return Effect.flatMap(this.effect, (c) => c.run(sink));
2631
- }
2632
-
2633
- toEffect(): Effect.Effect<A, E | Cause.NoSuchElementError, R | R2> {
2634
- return Effect.flatMap(this.effect, (c) => c.asEffect());
2635
- }
2636
-
2637
- asComputed(): Computed<Option.Option<A>, E, R | R2> {
2638
- return computedFromService<R, Option.Option<A>, E, R2>(
2639
- Effect.map(this.effect, (c) => c.asComputed()),
2640
- );
2641
- }
2642
- }