effect 4.0.0-beta.5 → 4.0.0-beta.7

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 (482) hide show
  1. package/dist/Cache.d.ts +2 -2
  2. package/dist/Cache.d.ts.map +1 -1
  3. package/dist/Cache.js +1 -1
  4. package/dist/Cache.js.map +1 -1
  5. package/dist/Cause.d.ts +1 -1
  6. package/dist/Cause.d.ts.map +1 -1
  7. package/dist/Channel.d.ts.map +1 -1
  8. package/dist/Channel.js +6 -4
  9. package/dist/Channel.js.map +1 -1
  10. package/dist/Config.d.ts +3 -3
  11. package/dist/Config.js +3 -3
  12. package/dist/Config.js.map +1 -1
  13. package/dist/Data.d.ts +3 -2
  14. package/dist/Data.d.ts.map +1 -1
  15. package/dist/Data.js +9 -8
  16. package/dist/Data.js.map +1 -1
  17. package/dist/DateTime.d.ts +23 -161
  18. package/dist/DateTime.d.ts.map +1 -1
  19. package/dist/DateTime.js +6 -51
  20. package/dist/DateTime.js.map +1 -1
  21. package/dist/Duration.d.ts +12 -12
  22. package/dist/Duration.d.ts.map +1 -1
  23. package/dist/Duration.js +12 -12
  24. package/dist/Duration.js.map +1 -1
  25. package/dist/Effect.d.ts +13 -239
  26. package/dist/Effect.d.ts.map +1 -1
  27. package/dist/Effect.js +1 -119
  28. package/dist/Effect.js.map +1 -1
  29. package/dist/Encoding.d.ts +194 -0
  30. package/dist/Encoding.d.ts.map +1 -0
  31. package/dist/Encoding.js +352 -0
  32. package/dist/Encoding.js.map +1 -0
  33. package/dist/Equal.js.map +1 -1
  34. package/dist/FileSystem.js +2 -2
  35. package/dist/FileSystem.js.map +1 -1
  36. package/dist/Formatter.d.ts +131 -47
  37. package/dist/Formatter.d.ts.map +1 -1
  38. package/dist/Formatter.js +229 -51
  39. package/dist/Formatter.js.map +1 -1
  40. package/dist/Graph.d.ts.map +1 -1
  41. package/dist/Graph.js +2 -2
  42. package/dist/Graph.js.map +1 -1
  43. package/dist/JsonSchema.d.ts +299 -10
  44. package/dist/JsonSchema.d.ts.map +1 -1
  45. package/dist/JsonSchema.js +323 -4
  46. package/dist/JsonSchema.js.map +1 -1
  47. package/dist/Latch.d.ts +109 -0
  48. package/dist/Latch.d.ts.map +1 -0
  49. package/dist/Latch.js +72 -0
  50. package/dist/Latch.js.map +1 -0
  51. package/dist/LayerMap.d.ts +4 -4
  52. package/dist/LayerMap.d.ts.map +1 -1
  53. package/dist/LogLevel.d.ts +27 -0
  54. package/dist/LogLevel.d.ts.map +1 -1
  55. package/dist/LogLevel.js +28 -100
  56. package/dist/LogLevel.js.map +1 -1
  57. package/dist/Logger.d.ts +4 -4
  58. package/dist/Logger.d.ts.map +1 -1
  59. package/dist/Metric.d.ts +2 -2
  60. package/dist/Metric.d.ts.map +1 -1
  61. package/dist/Metric.js +1 -1
  62. package/dist/Metric.js.map +1 -1
  63. package/dist/Pipeable.d.ts +17 -0
  64. package/dist/Pipeable.d.ts.map +1 -1
  65. package/dist/Pipeable.js +19 -1
  66. package/dist/Pipeable.js.map +1 -1
  67. package/dist/Pool.d.ts +6 -4
  68. package/dist/Pool.d.ts.map +1 -1
  69. package/dist/Pool.js +7 -5
  70. package/dist/Pool.js.map +1 -1
  71. package/dist/PubSub.d.ts +3 -2
  72. package/dist/PubSub.d.ts.map +1 -1
  73. package/dist/PubSub.js +3 -2
  74. package/dist/PubSub.js.map +1 -1
  75. package/dist/Random.d.ts +1 -1
  76. package/dist/Random.d.ts.map +1 -1
  77. package/dist/Random.js +3 -12
  78. package/dist/Random.js.map +1 -1
  79. package/dist/RcMap.d.ts +2 -2
  80. package/dist/RcMap.d.ts.map +1 -1
  81. package/dist/RcMap.js +1 -1
  82. package/dist/RcMap.js.map +1 -1
  83. package/dist/RcRef.d.ts +1 -1
  84. package/dist/RcRef.d.ts.map +1 -1
  85. package/dist/Request.d.ts +1 -1
  86. package/dist/Request.d.ts.map +1 -1
  87. package/dist/Request.js +2 -1
  88. package/dist/Request.js.map +1 -1
  89. package/dist/RequestResolver.d.ts +6 -26
  90. package/dist/RequestResolver.d.ts.map +1 -1
  91. package/dist/RequestResolver.js +0 -20
  92. package/dist/RequestResolver.js.map +1 -1
  93. package/dist/Schedule.d.ts +22 -13
  94. package/dist/Schedule.d.ts.map +1 -1
  95. package/dist/Schedule.js +53 -25
  96. package/dist/Schedule.js.map +1 -1
  97. package/dist/Schema.d.ts +196 -46
  98. package/dist/Schema.d.ts.map +1 -1
  99. package/dist/Schema.js +385 -71
  100. package/dist/Schema.js.map +1 -1
  101. package/dist/SchemaGetter.d.ts +2 -2
  102. package/dist/SchemaGetter.d.ts.map +1 -1
  103. package/dist/SchemaGetter.js +12 -14
  104. package/dist/SchemaGetter.js.map +1 -1
  105. package/dist/SchemaRepresentation.d.ts +44 -43
  106. package/dist/SchemaRepresentation.d.ts.map +1 -1
  107. package/dist/SchemaRepresentation.js +5 -2
  108. package/dist/SchemaRepresentation.js.map +1 -1
  109. package/dist/SchemaTransformation.d.ts +37 -0
  110. package/dist/SchemaTransformation.d.ts.map +1 -1
  111. package/dist/SchemaTransformation.js +80 -0
  112. package/dist/SchemaTransformation.js.map +1 -1
  113. package/dist/ScopedCache.d.ts +2 -2
  114. package/dist/ScopedCache.d.ts.map +1 -1
  115. package/dist/ScopedCache.js +1 -1
  116. package/dist/ScopedCache.js.map +1 -1
  117. package/dist/Semaphore.d.ts +211 -0
  118. package/dist/Semaphore.d.ts.map +1 -0
  119. package/dist/{PartitionedSemaphore.js → Semaphore.js} +97 -13
  120. package/dist/Semaphore.js.map +1 -0
  121. package/dist/Stream.d.ts +19 -19
  122. package/dist/Stream.d.ts.map +1 -1
  123. package/dist/Stream.js +12 -11
  124. package/dist/Stream.js.map +1 -1
  125. package/dist/Struct.d.ts +16 -0
  126. package/dist/Struct.d.ts.map +1 -1
  127. package/dist/Struct.js +22 -0
  128. package/dist/Struct.js.map +1 -1
  129. package/dist/SubscriptionRef.d.ts +2 -1
  130. package/dist/SubscriptionRef.d.ts.map +1 -1
  131. package/dist/SubscriptionRef.js +2 -1
  132. package/dist/SubscriptionRef.js.map +1 -1
  133. package/dist/SynchronizedRef.d.ts +2 -1
  134. package/dist/SynchronizedRef.d.ts.map +1 -1
  135. package/dist/SynchronizedRef.js +2 -1
  136. package/dist/SynchronizedRef.js.map +1 -1
  137. package/dist/Types.d.ts +8 -0
  138. package/dist/Types.d.ts.map +1 -1
  139. package/dist/index.d.ts +148 -5
  140. package/dist/index.d.ts.map +1 -1
  141. package/dist/index.js +148 -5
  142. package/dist/index.js.map +1 -1
  143. package/dist/internal/core.js +11 -3
  144. package/dist/internal/core.js.map +1 -1
  145. package/dist/internal/dateTime.js +3 -11
  146. package/dist/internal/dateTime.js.map +1 -1
  147. package/dist/internal/effect.js +2 -2
  148. package/dist/internal/effect.js.map +1 -1
  149. package/dist/internal/random.d.ts +2 -0
  150. package/dist/internal/random.d.ts.map +1 -0
  151. package/dist/internal/random.js +13 -0
  152. package/dist/internal/random.js.map +1 -0
  153. package/dist/internal/rcRef.js +3 -2
  154. package/dist/internal/rcRef.js.map +1 -1
  155. package/dist/testing/TestClock.d.ts +7 -6
  156. package/dist/testing/TestClock.d.ts.map +1 -1
  157. package/dist/testing/TestClock.js +6 -4
  158. package/dist/testing/TestClock.js.map +1 -1
  159. package/dist/testing/TestSchema.d.ts +6 -6
  160. package/dist/testing/TestSchema.d.ts.map +1 -1
  161. package/dist/unstable/ai/AiError.d.ts +48 -48
  162. package/dist/unstable/ai/AiError.d.ts.map +1 -1
  163. package/dist/unstable/ai/Chat.d.ts +3 -3
  164. package/dist/unstable/ai/Chat.d.ts.map +1 -1
  165. package/dist/unstable/ai/Chat.js +4 -3
  166. package/dist/unstable/ai/Chat.js.map +1 -1
  167. package/dist/unstable/ai/LanguageModel.d.ts +2 -2
  168. package/dist/unstable/ai/LanguageModel.d.ts.map +1 -1
  169. package/dist/unstable/ai/McpSchema.d.ts +51 -51
  170. package/dist/unstable/ai/McpServer.d.ts +1 -1
  171. package/dist/unstable/ai/McpServer.d.ts.map +1 -1
  172. package/dist/unstable/ai/McpServer.js.map +1 -1
  173. package/dist/unstable/ai/Prompt.d.ts +20 -20
  174. package/dist/unstable/ai/Prompt.d.ts.map +1 -1
  175. package/dist/unstable/ai/Response.d.ts +26 -26
  176. package/dist/unstable/ai/Response.d.ts.map +1 -1
  177. package/dist/unstable/ai/Response.js +1 -1
  178. package/dist/unstable/ai/Response.js.map +1 -1
  179. package/dist/unstable/cli/CliError.d.ts +5 -5
  180. package/dist/unstable/cluster/ClusterCron.d.ts +1 -1
  181. package/dist/unstable/cluster/ClusterCron.d.ts.map +1 -1
  182. package/dist/unstable/cluster/ClusterCron.js +1 -1
  183. package/dist/unstable/cluster/ClusterCron.js.map +1 -1
  184. package/dist/unstable/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
  185. package/dist/unstable/cluster/ClusterWorkflowEngine.js +2 -1
  186. package/dist/unstable/cluster/ClusterWorkflowEngine.js.map +1 -1
  187. package/dist/unstable/cluster/Entity.d.ts +5 -4
  188. package/dist/unstable/cluster/Entity.d.ts.map +1 -1
  189. package/dist/unstable/cluster/Entity.js.map +1 -1
  190. package/dist/unstable/cluster/EntityResource.d.ts +2 -2
  191. package/dist/unstable/cluster/EntityResource.d.ts.map +1 -1
  192. package/dist/unstable/cluster/Envelope.d.ts +1 -1
  193. package/dist/unstable/cluster/K8sHttpClient.d.ts +1 -1
  194. package/dist/unstable/cluster/K8sHttpClient.js +1 -1
  195. package/dist/unstable/cluster/K8sHttpClient.js.map +1 -1
  196. package/dist/unstable/cluster/Message.d.ts +5 -5
  197. package/dist/unstable/cluster/MessageStorage.d.ts.map +1 -1
  198. package/dist/unstable/cluster/MessageStorage.js +2 -1
  199. package/dist/unstable/cluster/MessageStorage.js.map +1 -1
  200. package/dist/unstable/cluster/Reply.d.ts +3 -3
  201. package/dist/unstable/cluster/Runner.d.ts +1 -1
  202. package/dist/unstable/cluster/Runners.d.ts.map +1 -1
  203. package/dist/unstable/cluster/Runners.js +4 -3
  204. package/dist/unstable/cluster/Runners.js.map +1 -1
  205. package/dist/unstable/cluster/Sharding.d.ts +2 -2
  206. package/dist/unstable/cluster/Sharding.d.ts.map +1 -1
  207. package/dist/unstable/cluster/Sharding.js +8 -6
  208. package/dist/unstable/cluster/Sharding.js.map +1 -1
  209. package/dist/unstable/cluster/ShardingConfig.d.ts +21 -21
  210. package/dist/unstable/cluster/ShardingConfig.d.ts.map +1 -1
  211. package/dist/unstable/cluster/ShardingConfig.js.map +1 -1
  212. package/dist/unstable/cluster/SqlRunnerStorage.js +1 -1
  213. package/dist/unstable/cluster/SqlRunnerStorage.js.map +1 -1
  214. package/dist/unstable/cluster/internal/entityManager.js +5 -4
  215. package/dist/unstable/cluster/internal/entityManager.js.map +1 -1
  216. package/dist/unstable/cluster/internal/entityReaper.js +2 -1
  217. package/dist/unstable/cluster/internal/entityReaper.js.map +1 -1
  218. package/dist/unstable/cluster/internal/resourceRef.js +2 -1
  219. package/dist/unstable/cluster/internal/resourceRef.js.map +1 -1
  220. package/dist/unstable/devtools/DevToolsSchema.d.ts +36 -36
  221. package/dist/unstable/encoding/Sse.d.ts +3 -3
  222. package/dist/unstable/encoding/Sse.d.ts.map +1 -1
  223. package/dist/unstable/encoding/Sse.js.map +1 -1
  224. package/dist/unstable/eventlog/EventJournal.d.ts +1 -1
  225. package/dist/unstable/eventlog/EventLog.d.ts.map +1 -1
  226. package/dist/unstable/eventlog/EventLog.js +2 -1
  227. package/dist/unstable/eventlog/EventLog.js.map +1 -1
  228. package/dist/unstable/eventlog/EventLogRemote.d.ts +5 -5
  229. package/dist/unstable/http/Cookies.d.ts +2 -2
  230. package/dist/unstable/http/Cookies.d.ts.map +1 -1
  231. package/dist/unstable/http/Cookies.js +2 -2
  232. package/dist/unstable/http/Cookies.js.map +1 -1
  233. package/dist/unstable/http/HttpClient.d.ts +4 -4
  234. package/dist/unstable/http/HttpClient.d.ts.map +1 -1
  235. package/dist/unstable/http/HttpClientRequest.d.ts +11 -11
  236. package/dist/unstable/http/HttpClientRequest.d.ts.map +1 -1
  237. package/dist/unstable/http/HttpClientRequest.js +8 -1
  238. package/dist/unstable/http/HttpClientRequest.js.map +1 -1
  239. package/dist/unstable/http/HttpEffect.d.ts.map +1 -1
  240. package/dist/unstable/http/HttpEffect.js +18 -26
  241. package/dist/unstable/http/HttpEffect.js.map +1 -1
  242. package/dist/unstable/http/HttpMethod.d.ts +4 -4
  243. package/dist/unstable/http/HttpMethod.d.ts.map +1 -1
  244. package/dist/unstable/http/HttpMethod.js +3 -3
  245. package/dist/unstable/http/HttpMethod.js.map +1 -1
  246. package/dist/unstable/http/Multipart.d.ts +2 -2
  247. package/dist/unstable/http/UrlParams.d.ts +1 -1
  248. package/dist/unstable/httpapi/HttpApiBuilder.js +2 -2
  249. package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
  250. package/dist/unstable/httpapi/HttpApiScalar.d.ts +6 -0
  251. package/dist/unstable/httpapi/HttpApiScalar.d.ts.map +1 -1
  252. package/dist/unstable/httpapi/HttpApiScalar.js.map +1 -1
  253. package/dist/unstable/observability/Otlp.d.ts +12 -12
  254. package/dist/unstable/observability/Otlp.d.ts.map +1 -1
  255. package/dist/unstable/observability/OtlpExporter.d.ts +2 -2
  256. package/dist/unstable/observability/OtlpExporter.d.ts.map +1 -1
  257. package/dist/unstable/observability/OtlpExporter.js +1 -1
  258. package/dist/unstable/observability/OtlpExporter.js.map +1 -1
  259. package/dist/unstable/observability/OtlpLogger.d.ts +4 -4
  260. package/dist/unstable/observability/OtlpLogger.d.ts.map +1 -1
  261. package/dist/unstable/observability/OtlpMetrics.d.ts +4 -4
  262. package/dist/unstable/observability/OtlpMetrics.d.ts.map +1 -1
  263. package/dist/unstable/observability/OtlpTracer.d.ts +4 -4
  264. package/dist/unstable/observability/OtlpTracer.d.ts.map +1 -1
  265. package/dist/unstable/persistence/KeyValueStore.js +4 -4
  266. package/dist/unstable/persistence/KeyValueStore.js.map +1 -1
  267. package/dist/unstable/persistence/Persistable.d.ts +2 -2
  268. package/dist/unstable/persistence/Persistable.d.ts.map +1 -1
  269. package/dist/unstable/persistence/PersistedCache.d.ts +6 -5
  270. package/dist/unstable/persistence/PersistedCache.d.ts.map +1 -1
  271. package/dist/unstable/persistence/PersistedCache.js +2 -1
  272. package/dist/unstable/persistence/PersistedCache.js.map +1 -1
  273. package/dist/unstable/persistence/PersistedQueue.d.ts +12 -12
  274. package/dist/unstable/persistence/PersistedQueue.d.ts.map +1 -1
  275. package/dist/unstable/persistence/PersistedQueue.js +12 -11
  276. package/dist/unstable/persistence/PersistedQueue.js.map +1 -1
  277. package/dist/unstable/persistence/Persistence.d.ts +1 -1
  278. package/dist/unstable/persistence/Persistence.d.ts.map +1 -1
  279. package/dist/unstable/persistence/Persistence.js +2 -2
  280. package/dist/unstable/persistence/Persistence.js.map +1 -1
  281. package/dist/unstable/persistence/RateLimiter.d.ts +3 -3
  282. package/dist/unstable/persistence/RateLimiter.d.ts.map +1 -1
  283. package/dist/unstable/persistence/RateLimiter.js +1 -1
  284. package/dist/unstable/persistence/RateLimiter.js.map +1 -1
  285. package/dist/unstable/process/ChildProcess.d.ts +1 -1
  286. package/dist/unstable/process/ChildProcess.d.ts.map +1 -1
  287. package/dist/unstable/process/ChildProcessSpawner.d.ts +6 -0
  288. package/dist/unstable/process/ChildProcessSpawner.d.ts.map +1 -1
  289. package/dist/unstable/process/ChildProcessSpawner.js.map +1 -1
  290. package/dist/unstable/reactivity/Atom.d.ts +6 -6
  291. package/dist/unstable/reactivity/Atom.d.ts.map +1 -1
  292. package/dist/unstable/reactivity/Atom.js +3 -3
  293. package/dist/unstable/reactivity/Atom.js.map +1 -1
  294. package/dist/unstable/reactivity/AtomHttpApi.d.ts +1 -1
  295. package/dist/unstable/reactivity/AtomHttpApi.d.ts.map +1 -1
  296. package/dist/unstable/reactivity/AtomHttpApi.js +1 -1
  297. package/dist/unstable/reactivity/AtomHttpApi.js.map +1 -1
  298. package/dist/unstable/reactivity/AtomRpc.d.ts +1 -1
  299. package/dist/unstable/reactivity/AtomRpc.d.ts.map +1 -1
  300. package/dist/unstable/reactivity/AtomRpc.js +1 -1
  301. package/dist/unstable/reactivity/AtomRpc.js.map +1 -1
  302. package/dist/unstable/reactivity/index.d.ts +2 -2
  303. package/dist/unstable/reactivity/index.d.ts.map +1 -1
  304. package/dist/unstable/reactivity/index.js +2 -2
  305. package/dist/unstable/reactivity/index.js.map +1 -1
  306. package/dist/unstable/rpc/Rpc.d.ts +2 -2
  307. package/dist/unstable/rpc/Rpc.d.ts.map +1 -1
  308. package/dist/unstable/rpc/RpcClient.d.ts +5 -26
  309. package/dist/unstable/rpc/RpcClient.d.ts.map +1 -1
  310. package/dist/unstable/rpc/RpcClient.js +6 -13
  311. package/dist/unstable/rpc/RpcClient.js.map +1 -1
  312. package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
  313. package/dist/unstable/rpc/RpcServer.js +5 -3
  314. package/dist/unstable/rpc/RpcServer.js.map +1 -1
  315. package/dist/unstable/rpc/Utils.d.ts.map +1 -1
  316. package/dist/unstable/rpc/Utils.js +2 -1
  317. package/dist/unstable/rpc/Utils.js.map +1 -1
  318. package/dist/unstable/socket/Socket.d.ts +4 -4
  319. package/dist/unstable/socket/Socket.d.ts.map +1 -1
  320. package/dist/unstable/socket/Socket.js +3 -2
  321. package/dist/unstable/socket/Socket.js.map +1 -1
  322. package/dist/unstable/sql/SqlModel.d.ts +2 -2
  323. package/dist/unstable/sql/SqlModel.d.ts.map +1 -1
  324. package/dist/unstable/sql/SqlModel.js +3 -3
  325. package/dist/unstable/sql/SqlModel.js.map +1 -1
  326. package/dist/unstable/sql/SqlSchema.d.ts +16 -5
  327. package/dist/unstable/sql/SqlSchema.d.ts.map +1 -1
  328. package/dist/unstable/sql/SqlSchema.js +17 -7
  329. package/dist/unstable/sql/SqlSchema.js.map +1 -1
  330. package/dist/unstable/sql/Statement.js +0 -1
  331. package/dist/unstable/sql/Statement.js.map +1 -1
  332. package/dist/unstable/workers/Worker.d.ts.map +1 -1
  333. package/dist/unstable/workers/Worker.js +2 -1
  334. package/dist/unstable/workers/Worker.js.map +1 -1
  335. package/dist/unstable/workflow/DurableClock.d.ts +3 -3
  336. package/dist/unstable/workflow/DurableClock.d.ts.map +1 -1
  337. package/dist/unstable/workflow/DurableClock.js +3 -3
  338. package/dist/unstable/workflow/DurableClock.js.map +1 -1
  339. package/dist/unstable/workflow/DurableDeferred.js +2 -2
  340. package/dist/unstable/workflow/DurableDeferred.js.map +1 -1
  341. package/dist/unstable/workflow/Workflow.d.ts +1 -1
  342. package/dist/unstable/workflow/WorkflowEngine.d.ts +2 -1
  343. package/dist/unstable/workflow/WorkflowEngine.d.ts.map +1 -1
  344. package/dist/unstable/workflow/WorkflowEngine.js +2 -1
  345. package/dist/unstable/workflow/WorkflowEngine.js.map +1 -1
  346. package/package.json +1 -2
  347. package/src/Cache.ts +3 -3
  348. package/src/Cause.ts +1 -1
  349. package/src/Channel.ts +6 -4
  350. package/src/Config.ts +3 -3
  351. package/src/Data.ts +10 -9
  352. package/src/DateTime.ts +24 -164
  353. package/src/Duration.ts +15 -15
  354. package/src/Effect.ts +15 -261
  355. package/src/Encoding.ts +879 -0
  356. package/src/Equal.ts +2 -2
  357. package/src/FileSystem.ts +4 -4
  358. package/src/Formatter.ts +253 -51
  359. package/src/Graph.ts +8 -5
  360. package/src/JsonSchema.ts +383 -10
  361. package/src/Latch.ts +112 -0
  362. package/src/LayerMap.ts +5 -5
  363. package/src/LogLevel.ts +31 -0
  364. package/src/Logger.ts +5 -5
  365. package/src/Metric.ts +4 -4
  366. package/src/Pipeable.ts +32 -1
  367. package/src/Pool.ts +13 -11
  368. package/src/PubSub.ts +10 -9
  369. package/src/Random.ts +5 -14
  370. package/src/RcMap.ts +5 -5
  371. package/src/RcRef.ts +1 -1
  372. package/src/Request.ts +3 -2
  373. package/src/RequestResolver.ts +9 -29
  374. package/src/Schedule.ts +261 -140
  375. package/src/Schema.ts +571 -109
  376. package/src/SchemaGetter.ts +12 -14
  377. package/src/SchemaRepresentation.ts +5 -2
  378. package/src/SchemaTransformation.ts +104 -0
  379. package/src/ScopedCache.ts +3 -3
  380. package/src/Semaphore.ts +356 -0
  381. package/src/Stream.ts +43 -42
  382. package/src/Struct.ts +26 -0
  383. package/src/SubscriptionRef.ts +3 -2
  384. package/src/SynchronizedRef.ts +3 -2
  385. package/src/Types.ts +9 -0
  386. package/src/index.ts +151 -6
  387. package/src/internal/core.ts +10 -2
  388. package/src/internal/dateTime.ts +9 -30
  389. package/src/internal/effect.ts +24 -23
  390. package/src/internal/random.ts +20 -0
  391. package/src/internal/rcRef.ts +4 -3
  392. package/src/testing/TestClock.ts +13 -11
  393. package/src/testing/TestSchema.ts +8 -8
  394. package/src/unstable/ai/AiError.ts +2 -2
  395. package/src/unstable/ai/Chat.ts +12 -11
  396. package/src/unstable/ai/LanguageModel.ts +3 -3
  397. package/src/unstable/ai/McpServer.ts +2 -2
  398. package/src/unstable/ai/Prompt.ts +37 -37
  399. package/src/unstable/ai/Response.ts +25 -25
  400. package/src/unstable/cluster/ClusterCron.ts +2 -2
  401. package/src/unstable/cluster/ClusterWorkflowEngine.ts +3 -2
  402. package/src/unstable/cluster/Entity.ts +7 -6
  403. package/src/unstable/cluster/EntityResource.ts +4 -4
  404. package/src/unstable/cluster/Envelope.ts +1 -1
  405. package/src/unstable/cluster/K8sHttpClient.ts +1 -1
  406. package/src/unstable/cluster/MessageStorage.ts +3 -5
  407. package/src/unstable/cluster/Runners.ts +6 -5
  408. package/src/unstable/cluster/Sharding.ts +11 -9
  409. package/src/unstable/cluster/ShardingConfig.ts +10 -11
  410. package/src/unstable/cluster/SqlRunnerStorage.ts +1 -1
  411. package/src/unstable/cluster/internal/entityManager.ts +9 -8
  412. package/src/unstable/cluster/internal/entityReaper.ts +2 -1
  413. package/src/unstable/cluster/internal/resourceRef.ts +2 -1
  414. package/src/unstable/encoding/Sse.ts +2 -4
  415. package/src/unstable/eventlog/EventLog.ts +2 -1
  416. package/src/unstable/http/Cookies.ts +3 -3
  417. package/src/unstable/http/HttpClient.ts +4 -4
  418. package/src/unstable/http/HttpClientRequest.ts +15 -11
  419. package/src/unstable/http/HttpEffect.ts +17 -25
  420. package/src/unstable/http/HttpMethod.ts +16 -4
  421. package/src/unstable/http/Multipart.ts +2 -2
  422. package/src/unstable/http/UrlParams.ts +1 -1
  423. package/src/unstable/httpapi/HttpApiBuilder.ts +2 -2
  424. package/src/unstable/httpapi/HttpApiScalar.ts +6 -0
  425. package/src/unstable/observability/Otlp.ts +12 -12
  426. package/src/unstable/observability/OtlpExporter.ts +3 -3
  427. package/src/unstable/observability/OtlpLogger.ts +4 -4
  428. package/src/unstable/observability/OtlpMetrics.ts +4 -4
  429. package/src/unstable/observability/OtlpTracer.ts +4 -4
  430. package/src/unstable/persistence/KeyValueStore.ts +4 -4
  431. package/src/unstable/persistence/Persistable.ts +2 -2
  432. package/src/unstable/persistence/PersistedCache.ts +20 -9
  433. package/src/unstable/persistence/PersistedQueue.ts +25 -24
  434. package/src/unstable/persistence/Persistence.ts +3 -3
  435. package/src/unstable/persistence/RateLimiter.ts +4 -4
  436. package/src/unstable/process/ChildProcess.ts +1 -1
  437. package/src/unstable/process/ChildProcessSpawner.ts +6 -0
  438. package/src/unstable/reactivity/Atom.ts +13 -13
  439. package/src/unstable/reactivity/AtomHttpApi.ts +3 -3
  440. package/src/unstable/reactivity/AtomRpc.ts +3 -3
  441. package/src/unstable/reactivity/index.ts +2 -2
  442. package/src/unstable/rpc/Rpc.ts +3 -3
  443. package/src/unstable/rpc/RpcClient.ts +12 -54
  444. package/src/unstable/rpc/RpcServer.ts +6 -4
  445. package/src/unstable/rpc/Utils.ts +2 -1
  446. package/src/unstable/socket/Socket.ts +7 -6
  447. package/src/unstable/sql/SqlModel.ts +5 -5
  448. package/src/unstable/sql/SqlSchema.ts +41 -25
  449. package/src/unstable/sql/Statement.ts +0 -1
  450. package/src/unstable/workers/Worker.ts +2 -1
  451. package/src/unstable/workflow/DurableClock.ts +8 -8
  452. package/src/unstable/workflow/DurableDeferred.ts +2 -2
  453. package/src/unstable/workflow/WorkflowEngine.ts +3 -2
  454. package/dist/PartitionedSemaphore.d.ts +0 -52
  455. package/dist/PartitionedSemaphore.d.ts.map +0 -1
  456. package/dist/PartitionedSemaphore.js.map +0 -1
  457. package/dist/encoding/Base64.d.ts +0 -67
  458. package/dist/encoding/Base64.d.ts.map +0 -1
  459. package/dist/encoding/Base64.js +0 -146
  460. package/dist/encoding/Base64.js.map +0 -1
  461. package/dist/encoding/Base64Url.d.ts +0 -60
  462. package/dist/encoding/Base64Url.d.ts.map +0 -1
  463. package/dist/encoding/Base64Url.js +0 -89
  464. package/dist/encoding/Base64Url.js.map +0 -1
  465. package/dist/encoding/EncodingError.d.ts +0 -31
  466. package/dist/encoding/EncodingError.d.ts.map +0 -1
  467. package/dist/encoding/EncodingError.js +0 -22
  468. package/dist/encoding/EncodingError.js.map +0 -1
  469. package/dist/encoding/Hex.d.ts +0 -61
  470. package/dist/encoding/Hex.d.ts.map +0 -1
  471. package/dist/encoding/Hex.js +0 -115
  472. package/dist/encoding/Hex.js.map +0 -1
  473. package/dist/encoding/index.d.ts +0 -26
  474. package/dist/encoding/index.d.ts.map +0 -1
  475. package/dist/encoding/index.js +0 -27
  476. package/dist/encoding/index.js.map +0 -1
  477. package/src/PartitionedSemaphore.ts +0 -182
  478. package/src/encoding/Base64.ts +0 -366
  479. package/src/encoding/Base64Url.ts +0 -104
  480. package/src/encoding/EncodingError.ts +0 -35
  481. package/src/encoding/Hex.ts +0 -390
  482. package/src/encoding/index.ts +0 -31
@@ -86,11 +86,9 @@
86
86
  */
87
87
  import * as DateTime from "./DateTime.ts"
88
88
  import * as Effect from "./Effect.ts"
89
- import * as Base64 from "./encoding/Base64.ts"
90
- import * as Base64Url from "./encoding/Base64Url.ts"
91
- import * as Hex from "./encoding/Hex.ts"
89
+ import * as Encoding from "./Encoding.ts"
92
90
  import * as Option from "./Option.ts"
93
- import { Class } from "./Pipeable.ts"
91
+ import * as Pipeable from "./Pipeable.ts"
94
92
  import * as Predicate from "./Predicate.ts"
95
93
  import * as Result from "./Result.ts"
96
94
  import type * as Schema from "./Schema.ts"
@@ -136,7 +134,7 @@ import * as Str from "./String.ts"
136
134
  * @category model
137
135
  * @since 4.0.0
138
136
  */
139
- export class Getter<out T, in E, R = never> extends Class {
137
+ export class Getter<out T, in E, R = never> extends Pipeable.Class {
140
138
  readonly run: (
141
139
  input: Option.Option<E>,
142
140
  options: AST.ParseOptions
@@ -1218,7 +1216,7 @@ export function split<E extends string>(options?: {
1218
1216
  * @since 4.0.0
1219
1217
  */
1220
1218
  export function encodeBase64<E extends Uint8Array | string>(): Getter<string, E> {
1221
- return transform(Base64.encode)
1219
+ return transform(Encoding.encodeBase64)
1222
1220
  }
1223
1221
 
1224
1222
  /**
@@ -1244,7 +1242,7 @@ export function encodeBase64<E extends Uint8Array | string>(): Getter<string, E>
1244
1242
  * @since 4.0.0
1245
1243
  */
1246
1244
  export function encodeBase64Url<E extends Uint8Array | string>(): Getter<string, E> {
1247
- return transform(Base64Url.encode)
1245
+ return transform(Encoding.encodeBase64Url)
1248
1246
  }
1249
1247
 
1250
1248
  /**
@@ -1269,7 +1267,7 @@ export function encodeBase64Url<E extends Uint8Array | string>(): Getter<string,
1269
1267
  * @since 4.0.0
1270
1268
  */
1271
1269
  export function encodeHex<E extends Uint8Array | string>(): Getter<string, E> {
1272
- return transform(Hex.encode)
1270
+ return transform(Encoding.encodeHex)
1273
1271
  }
1274
1272
 
1275
1273
  /**
@@ -1297,7 +1295,7 @@ export function encodeHex<E extends Uint8Array | string>(): Getter<string, E> {
1297
1295
  export function decodeBase64<E extends string>(): Getter<Uint8Array, E> {
1298
1296
  return transformOrFail((input) =>
1299
1297
  Result.mapError(
1300
- Base64.decode(input),
1298
+ Encoding.decodeBase64(input),
1301
1299
  (e) => new Issue.InvalidValue(Option.some(input), { message: e.message })
1302
1300
  ).asEffect()
1303
1301
  )
@@ -1327,7 +1325,7 @@ export function decodeBase64<E extends string>(): Getter<Uint8Array, E> {
1327
1325
  */
1328
1326
  export function decodeBase64String<E extends string>(): Getter<string, E> {
1329
1327
  return transformOrFail((input) =>
1330
- Result.match(Base64.decodeString(input), {
1328
+ Result.match(Encoding.decodeBase64String(input), {
1331
1329
  onFailure: (e) => Effect.fail(new Issue.InvalidValue(Option.some(input), { message: e.message })),
1332
1330
  onSuccess: Effect.succeed
1333
1331
  })
@@ -1358,7 +1356,7 @@ export function decodeBase64String<E extends string>(): Getter<string, E> {
1358
1356
  */
1359
1357
  export function decodeBase64Url<E extends string>(): Getter<Uint8Array, E> {
1360
1358
  return transformOrFail((input) =>
1361
- Result.match(Base64Url.decode(input), {
1359
+ Result.match(Encoding.decodeBase64Url(input), {
1362
1360
  onFailure: (e) => Effect.fail(new Issue.InvalidValue(Option.some(input), { message: e.message })),
1363
1361
  onSuccess: Effect.succeed
1364
1362
  })
@@ -1389,7 +1387,7 @@ export function decodeBase64Url<E extends string>(): Getter<Uint8Array, E> {
1389
1387
  */
1390
1388
  export function decodeBase64UrlString<E extends string>(): Getter<string, E> {
1391
1389
  return transformOrFail((input) =>
1392
- Result.match(Base64Url.decodeString(input), {
1390
+ Result.match(Encoding.decodeBase64UrlString(input), {
1393
1391
  onFailure: (e) => Effect.fail(new Issue.InvalidValue(Option.some(input), { message: e.message })),
1394
1392
  onSuccess: Effect.succeed
1395
1393
  })
@@ -1420,7 +1418,7 @@ export function decodeBase64UrlString<E extends string>(): Getter<string, E> {
1420
1418
  */
1421
1419
  export function decodeHex<E extends string>(): Getter<Uint8Array, E> {
1422
1420
  return transformOrFail((input) =>
1423
- Result.match(Hex.decode(input), {
1421
+ Result.match(Encoding.decodeHex(input), {
1424
1422
  onFailure: (e) => Effect.fail(new Issue.InvalidValue(Option.some(input), { message: e.message })),
1425
1423
  onSuccess: Effect.succeed
1426
1424
  })
@@ -1451,7 +1449,7 @@ export function decodeHex<E extends string>(): Getter<Uint8Array, E> {
1451
1449
  */
1452
1450
  export function decodeHexString<E extends string>(): Getter<string, E> {
1453
1451
  return transformOrFail((input) =>
1454
- Result.match(Hex.decodeString(input), {
1452
+ Result.match(Encoding.decodeHexString(input), {
1455
1453
  onFailure: (e) => Effect.fail(new Issue.InvalidValue(Option.some(input), { message: e.message })),
1456
1454
  onSuccess: Effect.succeed
1457
1455
  })
@@ -1698,7 +1698,8 @@ export type Reviver<T> = (declaration: Declaration, recur: (representation: Repr
1698
1698
 
1699
1699
  /**
1700
1700
  * Default {@link Reviver} for {@link toSchema} that handles built-in Effect
1701
- * types (Option, Result, Redacted, Cause, Exit, ReadonlyMap, ReadonlySet,
1701
+ * types (Option, Result, Redacted, Cause, Exit, ReadonlyMap, HashMap,
1702
+ * ReadonlySet,
1702
1703
  * Date, Duration, URL, RegExp, etc.).
1703
1704
  *
1704
1705
  * - Pass as `options.reviver` to {@link toSchema} to reconstruct schemas that
@@ -1723,7 +1724,7 @@ export const toSchemaDefaultReviver: Reviver<Schema.Top> = (s, recur) => {
1723
1724
  case "effect/Redacted":
1724
1725
  return Schema.Redacted(typeParameters[0])
1725
1726
  case "effect/Cause/Failure":
1726
- return Schema.CauseFailure(typeParameters[0], typeParameters[1])
1727
+ return Schema.CauseReason(typeParameters[0], typeParameters[1])
1727
1728
  case "effect/Cause":
1728
1729
  return Schema.Cause(typeParameters[0], typeParameters[1])
1729
1730
  case "Error":
@@ -1732,6 +1733,8 @@ export const toSchemaDefaultReviver: Reviver<Schema.Top> = (s, recur) => {
1732
1733
  return Schema.Exit(typeParameters[0], typeParameters[1], typeParameters[2])
1733
1734
  case "ReadonlyMap":
1734
1735
  return Schema.ReadonlyMap(typeParameters[0], typeParameters[1])
1736
+ case "effect/HashMap":
1737
+ return Schema.HashMap(typeParameters[0], typeParameters[1])
1735
1738
  case "ReadonlySet":
1736
1739
  return Schema.ReadonlySet(typeParameters[0])
1737
1740
  case "RegExp":
@@ -82,6 +82,8 @@
82
82
  * @since 4.0.0
83
83
  */
84
84
 
85
+ import * as BigDecimal from "./BigDecimal.ts"
86
+ import * as DateTime from "./DateTime.ts"
85
87
  import * as Duration from "./Duration.ts"
86
88
  import * as Effect from "./Effect.ts"
87
89
  import * as Option from "./Option.ts"
@@ -1136,6 +1138,33 @@ export const urlFromString: Transformation<URL, string> = transformOrFail<URL, s
1136
1138
  encode: (url) => Effect.succeed(url.href)
1137
1139
  })
1138
1140
 
1141
+ /**
1142
+ * Decodes a `string` into a `BigDecimal` and encodes a `BigDecimal` back to
1143
+ * its string representation.
1144
+ *
1145
+ * When to use this:
1146
+ * - Parsing decimal number strings from APIs or user input.
1147
+ *
1148
+ * Behavior:
1149
+ * - Decode: calls `BigDecimal.fromString(s)`. Fails with `InvalidValue` if the
1150
+ * string is not a valid BigDecimal representation.
1151
+ * - Encode: returns `BigDecimal.format(bd)`.
1152
+ *
1153
+ * @since 4.0.0
1154
+ */
1155
+ export const bigDecimalFromString: Transformation<BigDecimal.BigDecimal, string> = transformOrFail<
1156
+ BigDecimal.BigDecimal,
1157
+ string
1158
+ >({
1159
+ decode: (s) => {
1160
+ const result = BigDecimal.fromString(s)
1161
+ return result === undefined
1162
+ ? Effect.fail(new Issue.InvalidValue(Option.some(s), { message: `Invalid BigDecimal string: ${s}` }))
1163
+ : Effect.succeed(result)
1164
+ },
1165
+ encode: (bd) => Effect.succeed(BigDecimal.format(bd))
1166
+ })
1167
+
1139
1168
  /**
1140
1169
  * Decodes a Base64-encoded `string` into a `Uint8Array` and encodes a
1141
1170
  * `Uint8Array` back to a Base64 string.
@@ -1264,3 +1293,78 @@ export const fromURLSearchParams = new Transformation<unknown, URLSearchParams>(
1264
1293
  Getter.decodeURLSearchParams(),
1265
1294
  Getter.encodeURLSearchParams()
1266
1295
  )
1296
+
1297
+ /**
1298
+ * @since 4.0.0
1299
+ */
1300
+ export const timeZoneOffsetFromNumber: Transformation<DateTime.TimeZone.Offset, number> = transform<
1301
+ DateTime.TimeZone.Offset,
1302
+ number
1303
+ >({
1304
+ decode: (n) => DateTime.zoneMakeOffset(n),
1305
+ encode: (tz) => tz.offset
1306
+ })
1307
+
1308
+ /**
1309
+ * @since 4.0.0
1310
+ */
1311
+ export const timeZoneNamedFromString: Transformation<DateTime.TimeZone.Named, string> = transformOrFail<
1312
+ DateTime.TimeZone.Named,
1313
+ string
1314
+ >({
1315
+ decode: (s) => {
1316
+ const result = DateTime.zoneMakeNamed(s)
1317
+ return result === undefined
1318
+ ? Effect.fail(new Issue.InvalidValue(Option.some(s), { message: `Invalid IANA time zone: ${s}` }))
1319
+ : Effect.succeed(result)
1320
+ },
1321
+ encode: (tz) => Effect.succeed(tz.id)
1322
+ })
1323
+
1324
+ /**
1325
+ * @since 4.0.0
1326
+ */
1327
+ export const timeZoneFromString: Transformation<DateTime.TimeZone, string> = transformOrFail<
1328
+ DateTime.TimeZone,
1329
+ string
1330
+ >({
1331
+ decode: (s) => {
1332
+ const result = DateTime.zoneFromString(s)
1333
+ return result === undefined
1334
+ ? Effect.fail(new Issue.InvalidValue(Option.some(s), { message: `Invalid time zone: ${s}` }))
1335
+ : Effect.succeed(result)
1336
+ },
1337
+ encode: (tz) => Effect.succeed(DateTime.zoneToString(tz))
1338
+ })
1339
+
1340
+ /**
1341
+ * @since 4.0.0
1342
+ */
1343
+ export const dateTimeUtcFromString: Transformation<DateTime.Utc, string> = transformOrFail<
1344
+ DateTime.Utc,
1345
+ string
1346
+ >({
1347
+ decode: (s) => {
1348
+ const result = DateTime.make(s)
1349
+ return result === undefined
1350
+ ? Effect.fail(new Issue.InvalidValue(Option.some(s), { message: "Invalid DateTime input" }))
1351
+ : Effect.succeed(DateTime.toUtc(result))
1352
+ },
1353
+ encode: (utc) => Effect.succeed(DateTime.formatIso(utc))
1354
+ })
1355
+
1356
+ /**
1357
+ * @since 4.0.0
1358
+ */
1359
+ export const dateTimeZonedFromString: Transformation<DateTime.Zoned, string> = transformOrFail<
1360
+ DateTime.Zoned,
1361
+ string
1362
+ >({
1363
+ decode: (s) => {
1364
+ const result = DateTime.makeZonedFromString(s)
1365
+ return result === undefined
1366
+ ? Effect.fail(new Issue.InvalidValue(Option.some(s), { message: `Invalid zoned DateTime string: ${s}` }))
1367
+ : Effect.succeed(result)
1368
+ },
1369
+ encode: (zoned) => Effect.succeed(DateTime.formatIsoZoned(zoned))
1370
+ })
@@ -69,7 +69,7 @@ export const makeWith = <
69
69
  >(options: {
70
70
  readonly lookup: (key: Key) => Effect.Effect<A, E, R | Scope.Scope>
71
71
  readonly capacity: number
72
- readonly timeToLive?: ((exit: Exit.Exit<A, E>, key: Key) => Duration.DurationInput) | undefined
72
+ readonly timeToLive?: ((exit: Exit.Exit<A, E>, key: Key) => Duration.Input) | undefined
73
73
  readonly requireServicesAt?: ServiceMode | undefined
74
74
  }): Effect.Effect<
75
75
  ScopedCache<Key, A, E, "lookup" extends ServiceMode ? Exclude<R, Scope.Scope> : never>,
@@ -88,7 +88,7 @@ export const makeWith = <
88
88
  self.state = { _tag: "Open", map }
89
89
  self.capacity = options.capacity
90
90
  self.timeToLive = options.timeToLive
91
- ? (exit: Exit.Exit<A, E>, key: Key) => Duration.fromDurationInputUnsafe(options.timeToLive!(exit, key))
91
+ ? (exit: Exit.Exit<A, E>, key: Key) => Duration.fromInputUnsafe(options.timeToLive!(exit, key))
92
92
  : defaultTimeToLive
93
93
  return effect.as(
94
94
  Scope.addFinalizer(
@@ -116,7 +116,7 @@ export const make = <
116
116
  options: {
117
117
  readonly lookup: (key: Key) => Effect.Effect<A, E, R | Scope.Scope>
118
118
  readonly capacity: number
119
- readonly timeToLive?: Duration.DurationInput | undefined
119
+ readonly timeToLive?: Duration.Input | undefined
120
120
  readonly requireServicesAt?: ServiceMode | undefined
121
121
  }
122
122
  ): Effect.Effect<
@@ -0,0 +1,356 @@
1
+ /**
2
+ * @since 2.0.0
3
+ */
4
+ import * as Effect from "./Effect.ts"
5
+ import * as internal from "./internal/effect.ts"
6
+ import * as MutableHashMap from "./MutableHashMap.ts"
7
+ import * as Option from "./Option.ts"
8
+
9
+ /**
10
+ * @category models
11
+ * @since 2.0.0
12
+ * @example
13
+ * ```ts
14
+ * import { Effect, Semaphore } from "effect"
15
+ *
16
+ * // Create and use a semaphore for controlling concurrent access
17
+ * const program = Effect.gen(function*() {
18
+ * const semaphore = yield* Semaphore.make(2)
19
+ *
20
+ * return yield* semaphore.withPermits(1)(
21
+ * Effect.succeed("Resource accessed")
22
+ * )
23
+ * })
24
+ * ```
25
+ */
26
+ export interface Semaphore {
27
+ /**
28
+ * Adjusts the number of permits available in the semaphore.
29
+ */
30
+ resize(permits: number): Effect.Effect<void>
31
+
32
+ /**
33
+ * Runs an effect with the given number of permits and releases the permits
34
+ * when the effect completes.
35
+ *
36
+ * **Details**
37
+ *
38
+ * This function acquires the specified number of permits before executing
39
+ * the provided effect. Once the effect finishes, the permits are released.
40
+ * If insufficient permits are available, the function will wait until they
41
+ * are released by other tasks.
42
+ */
43
+ withPermits(permits: number): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
44
+
45
+ /**
46
+ * Runs an effect with the given number of permits and releases the permits
47
+ * when the effect completes.
48
+ *
49
+ * **Details**
50
+ *
51
+ * This function acquires the specified number of permits before executing
52
+ * the provided effect. Once the effect finishes, the permits are released.
53
+ * If insufficient permits are available, the function will wait until they
54
+ * are released by other tasks.
55
+ */
56
+ withPermit<A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>
57
+
58
+ /**
59
+ * Runs an effect only if the specified number of permits are immediately
60
+ * available.
61
+ *
62
+ * **Details**
63
+ *
64
+ * This function attempts to acquire the specified number of permits. If they
65
+ * are available, it runs the effect and releases the permits after the effect
66
+ * completes. If permits are not available, the effect does not execute, and
67
+ * the result is `Option.none`.
68
+ */
69
+ withPermitsIfAvailable(
70
+ permits: number
71
+ ): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<Option.Option<A>, E, R>
72
+
73
+ /**
74
+ * Acquires the specified number of permits and returns the resulting
75
+ * available permits, suspending the task if they are not yet available.
76
+ * Concurrent pending `take` calls are processed in a first-in, first-out manner.
77
+ */
78
+ take(permits: number): Effect.Effect<number>
79
+
80
+ /**
81
+ * Releases the specified number of permits and returns the resulting
82
+ * available permits.
83
+ */
84
+ release(permits: number): Effect.Effect<number>
85
+
86
+ /**
87
+ * Releases all permits held by this semaphore and returns the resulting available permits.
88
+ */
89
+ releaseAll: Effect.Effect<number>
90
+ }
91
+
92
+ /**
93
+ * Unsafely creates a new Semaphore.
94
+ *
95
+ * **Previously Known As**
96
+ *
97
+ * This API replaces the following from Effect 3.x:
98
+ *
99
+ * - `Effect.makeSemaphoreUnsafe`
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * import { Effect, Semaphore } from "effect"
104
+ *
105
+ * const semaphore = Semaphore.makeUnsafe(3)
106
+ *
107
+ * const task = (id: number) =>
108
+ * semaphore.withPermits(1)(
109
+ * Effect.gen(function*() {
110
+ * yield* Effect.log(`Task ${id} started`)
111
+ * yield* Effect.sleep("1 second")
112
+ * yield* Effect.log(`Task ${id} completed`)
113
+ * })
114
+ * )
115
+ *
116
+ * // Only 3 tasks can run concurrently
117
+ * const program = Effect.all([
118
+ * task(1),
119
+ * task(2),
120
+ * task(3),
121
+ * task(4),
122
+ * task(5)
123
+ * ], { concurrency: "unbounded" })
124
+ * ```
125
+ *
126
+ * @since 2.0.0
127
+ * @category constructors
128
+ */
129
+ export const makeUnsafe: (permits: number) => Semaphore = internal.makeSemaphoreUnsafe
130
+
131
+ /**
132
+ * Creates a new Semaphore.
133
+ *
134
+ * **Previously Known As**
135
+ *
136
+ * This API replaces the following from Effect 3.x:
137
+ *
138
+ * - `Effect.makeSemaphore`
139
+ *
140
+ * @example
141
+ * ```ts
142
+ * import { Effect, Semaphore } from "effect"
143
+ *
144
+ * const program = Effect.gen(function*() {
145
+ * const semaphore = yield* Semaphore.make(2)
146
+ *
147
+ * const task = (id: number) =>
148
+ * semaphore.withPermits(1)(
149
+ * Effect.gen(function*() {
150
+ * yield* Effect.log(`Task ${id} acquired permit`)
151
+ * yield* Effect.sleep("1 second")
152
+ * yield* Effect.log(`Task ${id} releasing permit`)
153
+ * })
154
+ * )
155
+ *
156
+ * // Run 4 tasks, but only 2 can run concurrently
157
+ * yield* Effect.all([task(1), task(2), task(3), task(4)])
158
+ * })
159
+ * ```
160
+ *
161
+ * @since 2.0.0
162
+ * @category constructors
163
+ */
164
+ export const make: (permits: number) => Effect.Effect<Semaphore> = internal.makeSemaphore
165
+
166
+ // -----------------------------------------------------------------------------
167
+ // Partitioned
168
+ // -----------------------------------------------------------------------------
169
+
170
+ /**
171
+ * @since 3.19.4
172
+ * @category models
173
+ */
174
+ export const PartitionedTypeId: PartitionedTypeId = "~effect/PartitionedSemaphore"
175
+
176
+ /**
177
+ * @since 3.19.4
178
+ * @category models
179
+ */
180
+ export type PartitionedTypeId = "~effect/PartitionedSemaphore"
181
+
182
+ /**
183
+ * A `Partitioned` semaphore controls access to a shared permit pool while
184
+ * tracking waiters by partition key.
185
+ *
186
+ * Waiting permits are distributed across partitions in round-robin order.
187
+ *
188
+ * **Previously Known As**
189
+ *
190
+ * This API replaces the following from Effect 3.x:
191
+ *
192
+ * - `PartitionedSemaphore.PartitionedSemaphore`
193
+ *
194
+ * @since 3.19.4
195
+ * @category models
196
+ */
197
+ export interface Partitioned<in K> {
198
+ readonly [PartitionedTypeId]: PartitionedTypeId
199
+ readonly withPermits: (
200
+ key: K,
201
+ permits: number
202
+ ) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
203
+ }
204
+
205
+ /**
206
+ * Creates a `Partitioned` semaphore unsafely.
207
+ *
208
+ * **Previously Known As**
209
+ *
210
+ * This API replaces the following from Effect 3.x:
211
+ *
212
+ * - `PartitionedSemaphore.makeUnsafe`
213
+ *
214
+ * @since 3.19.4
215
+ * @category constructors
216
+ */
217
+ export const makePartitionedUnsafe = <K = unknown>(options: {
218
+ readonly permits: number
219
+ }): Partitioned<K> => {
220
+ const maxPermits = Math.max(0, options.permits)
221
+
222
+ if (!Number.isFinite(maxPermits)) {
223
+ return {
224
+ [PartitionedTypeId]: PartitionedTypeId,
225
+ withPermits: () => (effect) => effect
226
+ }
227
+ }
228
+
229
+ let totalPermits = maxPermits
230
+ let waitingPermits = 0
231
+
232
+ type Waiter = {
233
+ permits: number
234
+ readonly resume: () => void
235
+ }
236
+ const partitions = MutableHashMap.empty<K, Set<Waiter>>()
237
+
238
+ const take = (key: K, permits: number) =>
239
+ Effect.callback<void>((resume) => {
240
+ if (maxPermits < permits) {
241
+ resume(Effect.never)
242
+ return
243
+ }
244
+
245
+ if (totalPermits >= permits) {
246
+ totalPermits -= permits
247
+ resume(Effect.void)
248
+ return
249
+ }
250
+
251
+ const needed = permits - totalPermits
252
+ const taken = permits - needed
253
+ if (totalPermits > 0) {
254
+ totalPermits = 0
255
+ }
256
+ waitingPermits += needed
257
+
258
+ const waiters = Option.getOrElse(
259
+ MutableHashMap.get(partitions, key),
260
+ () => {
261
+ const set = new Set<Waiter>()
262
+ MutableHashMap.set(partitions, key, set)
263
+ return set
264
+ }
265
+ )
266
+
267
+ const entry: Waiter = {
268
+ permits: needed,
269
+ resume: () => {
270
+ cleanup()
271
+ resume(Effect.void)
272
+ }
273
+ }
274
+
275
+ const cleanup = () => {
276
+ waiters.delete(entry)
277
+ if (waiters.size === 0) {
278
+ MutableHashMap.remove(partitions, key)
279
+ }
280
+ }
281
+
282
+ waiters.add(entry)
283
+
284
+ return Effect.sync(() => {
285
+ cleanup()
286
+ waitingPermits -= entry.permits
287
+ if (taken > 0) {
288
+ releaseUnsafe(taken)
289
+ }
290
+ })
291
+ })
292
+
293
+ let iterator = partitions[Symbol.iterator]()
294
+
295
+ const releaseUnsafe = (permits: number): void => {
296
+ while (permits > 0) {
297
+ if (waitingPermits === 0) {
298
+ totalPermits += permits
299
+ return
300
+ }
301
+
302
+ let state = iterator.next()
303
+ if (state.done) {
304
+ iterator = partitions[Symbol.iterator]()
305
+ state = iterator.next()
306
+ if (state.done) {
307
+ return
308
+ }
309
+ }
310
+
311
+ const waiter = state.value[1].values().next().value
312
+ if (waiter === undefined) {
313
+ continue
314
+ }
315
+
316
+ waiter.permits -= 1
317
+ waitingPermits -= 1
318
+
319
+ if (waiter.permits === 0) {
320
+ waiter.resume()
321
+ }
322
+
323
+ permits -= 1
324
+ }
325
+ }
326
+
327
+ return {
328
+ [PartitionedTypeId]: PartitionedTypeId,
329
+ withPermits: (key, permits) => {
330
+ const takePermits = take(key, permits)
331
+ return (effect) =>
332
+ Effect.uninterruptibleMask((restore) =>
333
+ Effect.flatMap(
334
+ restore(takePermits),
335
+ () => Effect.ensuring(restore(effect), Effect.sync(() => releaseUnsafe(permits)))
336
+ )
337
+ )
338
+ }
339
+ }
340
+ }
341
+
342
+ /**
343
+ * Creates a `Partitioned` semaphore.
344
+ *
345
+ * **Previously Known As**
346
+ *
347
+ * This API replaces the following from Effect 3.x:
348
+ *
349
+ * - `PartitionedSemaphore.make`
350
+ *
351
+ * @since 3.19.4
352
+ * @category constructors
353
+ */
354
+ export const makePartitioned = <K = unknown>(options: {
355
+ readonly permits: number
356
+ }): Effect.Effect<Partitioned<K>> => Effect.sync(() => makePartitionedUnsafe<K>(options))