@typed/fx 2.0.0-beta.3 → 2.0.0-beta.5

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