effect 4.0.0-beta.27 → 4.0.0-beta.29

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 (208) hide show
  1. package/dist/Brand.d.ts +1 -1
  2. package/dist/Brand.d.ts.map +1 -1
  3. package/dist/Brand.js +1 -1
  4. package/dist/Brand.js.map +1 -1
  5. package/dist/ConfigProvider.d.ts +1 -1
  6. package/dist/Cron.d.ts +1 -1
  7. package/dist/Data.d.ts +1 -1
  8. package/dist/Data.d.ts.map +1 -1
  9. package/dist/Data.js.map +1 -1
  10. package/dist/Effect.d.ts +300 -184
  11. package/dist/Effect.d.ts.map +1 -1
  12. package/dist/Effect.js +99 -61
  13. package/dist/Effect.js.map +1 -1
  14. package/dist/Encoding.d.ts +1 -1
  15. package/dist/Exit.d.ts +24 -12
  16. package/dist/Exit.d.ts.map +1 -1
  17. package/dist/Exit.js +8 -4
  18. package/dist/Exit.js.map +1 -1
  19. package/dist/Fiber.d.ts +1 -0
  20. package/dist/Fiber.d.ts.map +1 -1
  21. package/dist/Fiber.js.map +1 -1
  22. package/dist/Function.d.ts +1 -9
  23. package/dist/Function.d.ts.map +1 -1
  24. package/dist/Function.js +2 -10
  25. package/dist/Function.js.map +1 -1
  26. package/dist/Graph.d.ts +1 -1
  27. package/dist/Newtype.d.ts +291 -0
  28. package/dist/Newtype.d.ts.map +1 -0
  29. package/dist/Newtype.js +161 -0
  30. package/dist/Newtype.js.map +1 -0
  31. package/dist/PlatformError.d.ts +2 -2
  32. package/dist/References.d.ts +6 -1
  33. package/dist/References.d.ts.map +1 -1
  34. package/dist/References.js +6 -1
  35. package/dist/References.js.map +1 -1
  36. package/dist/RequestResolver.d.ts +19 -19
  37. package/dist/RequestResolver.js +10 -10
  38. package/dist/RequestResolver.js.map +1 -1
  39. package/dist/Schedule.d.ts +142 -80
  40. package/dist/Schedule.d.ts.map +1 -1
  41. package/dist/Schedule.js +58 -32
  42. package/dist/Schedule.js.map +1 -1
  43. package/dist/Scheduler.d.ts +9 -0
  44. package/dist/Scheduler.d.ts.map +1 -1
  45. package/dist/Scheduler.js +11 -0
  46. package/dist/Scheduler.js.map +1 -1
  47. package/dist/Schema.d.ts +1 -1
  48. package/dist/Schema.d.ts.map +1 -1
  49. package/dist/Schema.js +3 -1
  50. package/dist/Schema.js.map +1 -1
  51. package/dist/SchemaAST.d.ts.map +1 -1
  52. package/dist/SchemaAST.js +1 -1
  53. package/dist/SchemaAST.js.map +1 -1
  54. package/dist/Stdio.d.ts +6 -2
  55. package/dist/Stdio.d.ts.map +1 -1
  56. package/dist/Stdio.js +2 -2
  57. package/dist/Stdio.js.map +1 -1
  58. package/dist/Stream.d.ts +8 -4
  59. package/dist/Stream.d.ts.map +1 -1
  60. package/dist/Stream.js +8 -4
  61. package/dist/Stream.js.map +1 -1
  62. package/dist/Types.d.ts +1 -22
  63. package/dist/Types.d.ts.map +1 -1
  64. package/dist/index.d.ts +71 -3
  65. package/dist/index.d.ts.map +1 -1
  66. package/dist/index.js +71 -3
  67. package/dist/index.js.map +1 -1
  68. package/dist/internal/effect.js +3 -1
  69. package/dist/internal/effect.js.map +1 -1
  70. package/dist/unstable/ai/LanguageModel.d.ts +12 -28
  71. package/dist/unstable/ai/LanguageModel.d.ts.map +1 -1
  72. package/dist/unstable/ai/LanguageModel.js +4 -18
  73. package/dist/unstable/ai/LanguageModel.js.map +1 -1
  74. package/dist/unstable/ai/McpSchema.d.ts +20 -1
  75. package/dist/unstable/ai/McpSchema.d.ts.map +1 -1
  76. package/dist/unstable/ai/McpSchema.js +8 -0
  77. package/dist/unstable/ai/McpSchema.js.map +1 -1
  78. package/dist/unstable/ai/McpServer.d.ts +65 -12
  79. package/dist/unstable/ai/McpServer.d.ts.map +1 -1
  80. package/dist/unstable/ai/McpServer.js +159 -45
  81. package/dist/unstable/ai/McpServer.js.map +1 -1
  82. package/dist/unstable/ai/Toolkit.d.ts +1 -1
  83. package/dist/unstable/ai/Toolkit.d.ts.map +1 -1
  84. package/dist/unstable/ai/Toolkit.js +4 -11
  85. package/dist/unstable/ai/Toolkit.js.map +1 -1
  86. package/dist/unstable/ai/internal/codec-transformer.js +0 -5
  87. package/dist/unstable/ai/internal/codec-transformer.js.map +1 -1
  88. package/dist/unstable/cli/Prompt.js +35 -8
  89. package/dist/unstable/cli/Prompt.js.map +1 -1
  90. package/dist/unstable/cluster/Message.d.ts +5 -5
  91. package/dist/unstable/cluster/Reply.d.ts +3 -3
  92. package/dist/unstable/encoding/Msgpack.d.ts +1 -1
  93. package/dist/unstable/encoding/Ndjson.d.ts +1 -1
  94. package/dist/unstable/encoding/Sse.d.ts +1 -1
  95. package/dist/unstable/eventlog/EventJournal.d.ts +1 -1
  96. package/dist/unstable/eventlog/EventLogRemote.d.ts +1 -1
  97. package/dist/unstable/http/Cookies.d.ts +45 -1
  98. package/dist/unstable/http/Cookies.d.ts.map +1 -1
  99. package/dist/unstable/http/Cookies.js +22 -0
  100. package/dist/unstable/http/Cookies.js.map +1 -1
  101. package/dist/unstable/http/HttpBody.d.ts +1 -1
  102. package/dist/unstable/http/HttpClient.d.ts.map +1 -1
  103. package/dist/unstable/http/HttpClient.js +3 -7
  104. package/dist/unstable/http/HttpClient.js.map +1 -1
  105. package/dist/unstable/http/HttpClientError.d.ts +7 -7
  106. package/dist/unstable/http/HttpClientRequest.d.ts +5 -0
  107. package/dist/unstable/http/HttpClientRequest.d.ts.map +1 -1
  108. package/dist/unstable/http/HttpClientRequest.js +21 -17
  109. package/dist/unstable/http/HttpClientRequest.js.map +1 -1
  110. package/dist/unstable/http/HttpEffect.d.ts +7 -0
  111. package/dist/unstable/http/HttpEffect.d.ts.map +1 -1
  112. package/dist/unstable/http/HttpEffect.js +6 -0
  113. package/dist/unstable/http/HttpEffect.js.map +1 -1
  114. package/dist/unstable/http/HttpServerError.d.ts +6 -6
  115. package/dist/unstable/http/HttpServerRequest.d.ts +11 -0
  116. package/dist/unstable/http/HttpServerRequest.d.ts.map +1 -1
  117. package/dist/unstable/http/HttpServerRequest.js +291 -1
  118. package/dist/unstable/http/HttpServerRequest.js.map +1 -1
  119. package/dist/unstable/http/HttpServerResponse.d.ts +47 -1
  120. package/dist/unstable/http/HttpServerResponse.d.ts.map +1 -1
  121. package/dist/unstable/http/HttpServerResponse.js +227 -0
  122. package/dist/unstable/http/HttpServerResponse.js.map +1 -1
  123. package/dist/unstable/http/HttpStaticServer.d.ts +69 -0
  124. package/dist/unstable/http/HttpStaticServer.d.ts.map +1 -0
  125. package/dist/unstable/http/HttpStaticServer.js +353 -0
  126. package/dist/unstable/http/HttpStaticServer.js.map +1 -0
  127. package/dist/unstable/http/Multipart.d.ts +1 -1
  128. package/dist/unstable/http/UrlParams.d.ts +1 -1
  129. package/dist/unstable/http/index.d.ts +4 -0
  130. package/dist/unstable/http/index.d.ts.map +1 -1
  131. package/dist/unstable/http/index.js +4 -0
  132. package/dist/unstable/http/index.js.map +1 -1
  133. package/dist/unstable/httpapi/HttpApiGroup.d.ts +1 -0
  134. package/dist/unstable/httpapi/HttpApiGroup.d.ts.map +1 -1
  135. package/dist/unstable/httpapi/HttpApiGroup.js.map +1 -1
  136. package/dist/unstable/persistence/KeyValueStore.d.ts +1 -1
  137. package/dist/unstable/reactivity/Atom.d.ts +7 -2
  138. package/dist/unstable/reactivity/Atom.d.ts.map +1 -1
  139. package/dist/unstable/reactivity/Atom.js +33 -15
  140. package/dist/unstable/reactivity/Atom.js.map +1 -1
  141. package/dist/unstable/reactivity/AtomHttpApi.d.ts +4 -6
  142. package/dist/unstable/reactivity/AtomHttpApi.d.ts.map +1 -1
  143. package/dist/unstable/reactivity/AtomHttpApi.js +39 -9
  144. package/dist/unstable/reactivity/AtomHttpApi.js.map +1 -1
  145. package/dist/unstable/reactivity/AtomRegistry.js +26 -2
  146. package/dist/unstable/reactivity/AtomRegistry.js.map +1 -1
  147. package/dist/unstable/reactivity/AtomRpc.d.ts +8 -8
  148. package/dist/unstable/reactivity/AtomRpc.d.ts.map +1 -1
  149. package/dist/unstable/reactivity/AtomRpc.js +46 -20
  150. package/dist/unstable/reactivity/AtomRpc.js.map +1 -1
  151. package/dist/unstable/rpc/Rpc.d.ts +1 -1
  152. package/dist/unstable/rpc/Rpc.d.ts.map +1 -1
  153. package/dist/unstable/rpc/Rpc.js.map +1 -1
  154. package/dist/unstable/rpc/RpcMiddleware.d.ts +5 -5
  155. package/dist/unstable/rpc/RpcMiddleware.d.ts.map +1 -1
  156. package/dist/unstable/rpc/RpcMiddleware.js.map +1 -1
  157. package/dist/unstable/rpc/RpcServer.js +2 -2
  158. package/dist/unstable/rpc/RpcServer.js.map +1 -1
  159. package/dist/unstable/rpc/Utils.js +1 -1
  160. package/dist/unstable/rpc/Utils.js.map +1 -1
  161. package/dist/unstable/socket/SocketServer.d.ts +3 -3
  162. package/dist/unstable/sql/Migrator.d.ts +1 -1
  163. package/dist/unstable/sql/SqlResolver.js +2 -2
  164. package/dist/unstable/sql/SqlResolver.js.map +1 -1
  165. package/dist/unstable/workflow/Workflow.d.ts +1 -1
  166. package/package.json +1 -1
  167. package/src/Brand.ts +1 -1
  168. package/src/Data.ts +1 -2
  169. package/src/Effect.ts +300 -184
  170. package/src/Exit.ts +24 -12
  171. package/src/Fiber.ts +1 -0
  172. package/src/Function.ts +2 -10
  173. package/src/Newtype.ts +308 -0
  174. package/src/References.ts +6 -1
  175. package/src/RequestResolver.ts +20 -20
  176. package/src/Schedule.ts +142 -80
  177. package/src/Scheduler.ts +12 -0
  178. package/src/Schema.ts +4 -2
  179. package/src/SchemaAST.ts +1 -4
  180. package/src/Stdio.ts +8 -4
  181. package/src/Stream.ts +8 -4
  182. package/src/Types.ts +1 -23
  183. package/src/index.ts +72 -3
  184. package/src/internal/effect.ts +3 -0
  185. package/src/unstable/ai/LanguageModel.ts +16 -37
  186. package/src/unstable/ai/McpSchema.ts +14 -0
  187. package/src/unstable/ai/McpServer.ts +224 -53
  188. package/src/unstable/ai/Toolkit.ts +5 -14
  189. package/src/unstable/ai/internal/codec-transformer.ts +0 -7
  190. package/src/unstable/cli/Prompt.ts +31 -9
  191. package/src/unstable/http/Cookies.ts +84 -0
  192. package/src/unstable/http/HttpClient.ts +5 -6
  193. package/src/unstable/http/HttpClientRequest.ts +21 -17
  194. package/src/unstable/http/HttpEffect.ts +8 -0
  195. package/src/unstable/http/HttpServerRequest.ts +388 -1
  196. package/src/unstable/http/HttpServerResponse.ts +328 -1
  197. package/src/unstable/http/HttpStaticServer.ts +456 -0
  198. package/src/unstable/http/index.ts +5 -0
  199. package/src/unstable/httpapi/HttpApiGroup.ts +1 -0
  200. package/src/unstable/reactivity/Atom.ts +62 -35
  201. package/src/unstable/reactivity/AtomHttpApi.ts +45 -11
  202. package/src/unstable/reactivity/AtomRegistry.ts +30 -2
  203. package/src/unstable/reactivity/AtomRpc.ts +48 -17
  204. package/src/unstable/rpc/Rpc.ts +1 -3
  205. package/src/unstable/rpc/RpcMiddleware.ts +12 -6
  206. package/src/unstable/rpc/RpcServer.ts +2 -2
  207. package/src/unstable/rpc/Utils.ts +1 -1
  208. package/src/unstable/sql/SqlResolver.ts +2 -2
@@ -3,19 +3,21 @@
3
3
  */
4
4
  import * as Duration from "../../Duration.ts"
5
5
  import * as Effect from "../../Effect.ts"
6
+ import * as Hash from "../../Hash.ts"
6
7
  import * as Layer from "../../Layer.ts"
7
8
  import type { ReadonlyRecord } from "../../Record.ts"
9
+ import * as Schema from "../../Schema.ts"
8
10
  import type { SchemaError } from "../../Schema.ts"
9
11
  import * as ServiceMap from "../../ServiceMap.ts"
10
12
  import type { Mutable, Simplify } from "../../Types.ts"
11
13
  import type * as HttpClient from "../http/HttpClient.ts"
12
- import type * as HttpClientError from "../http/HttpClientError.ts"
14
+ import * as HttpClientError from "../http/HttpClientError.ts"
13
15
  import type { HttpClientResponse } from "../http/HttpClientResponse.ts"
14
16
  import type * as HttpApi from "../httpapi/HttpApi.ts"
15
17
  import * as HttpApiClient from "../httpapi/HttpApiClient.ts"
16
- import type * as HttpApiEndpoint from "../httpapi/HttpApiEndpoint.ts"
18
+ import * as HttpApiEndpoint from "../httpapi/HttpApiEndpoint.ts"
17
19
  import type * as HttpApiGroup from "../httpapi/HttpApiGroup.ts"
18
- import type * as AsyncResult from "./AsyncResult.ts"
20
+ import * as AsyncResult from "./AsyncResult.ts"
19
21
  import * as Atom from "./Atom.ts"
20
22
  import * as Reactivity from "./Reactivity.ts"
21
23
 
@@ -67,7 +69,7 @@ export interface AtomHttpApiClient<Self, Id extends string, Groups extends HttpA
67
69
  }
68
70
  >,
69
71
  WithResponse extends true ? [_Success["Type"], HttpClientResponse] : _Success["Type"],
70
- _Error["Type"] | HttpClientError.HttpClientError | SchemaError
72
+ _Error["Type"]
71
73
  >
72
74
  : never
73
75
 
@@ -124,7 +126,7 @@ export interface AtomHttpApiClient<Self, Id extends string, Groups extends HttpA
124
126
  ] ? Atom.Atom<
125
127
  AsyncResult.AsyncResult<
126
128
  WithResponse extends true ? [_Success["Type"], HttpClientResponse] : _Success["Type"],
127
- _Error["Type"] | HttpClientError.HttpClientError | SchemaError
129
+ _Error["Type"]
128
130
  >
129
131
  >
130
132
  : never
@@ -169,8 +171,12 @@ export const Service = <Self>() =>
169
171
  const runtimeFactory = options.runtime ?? Atom.runtime
170
172
  self.runtime = runtimeFactory(self.layer)
171
173
 
172
- const mutationFamily = Atom.family(({ endpoint, group, withResponse }: MutationKey) =>
173
- self.runtime.fn<{
174
+ const catchErrors = Effect.catch((e: unknown) =>
175
+ Schema.isSchemaError(e) || HttpClientError.isHttpClientError(e) ? Effect.die(e) : Effect.fail(e)
176
+ )
177
+
178
+ const mutationFamily = Atom.family(({ endpoint, group, withResponse }: MutationKey) => {
179
+ const atom = self.runtime.fn<{
174
180
  params: any
175
181
  query: any
176
182
  headers: any
@@ -179,16 +185,27 @@ export const Service = <Self>() =>
179
185
  }>()(
180
186
  Effect.fnUntraced(function*(opts) {
181
187
  const client = (yield* self) as any
182
- const effect = client[group][endpoint]({
188
+ const effect = catchErrors(client[group][endpoint]({
183
189
  ...opts,
184
190
  withResponse
185
- }) as Effect.Effect<any>
191
+ }) as Effect.Effect<any>)
186
192
  return yield* opts.reactivityKeys
187
193
  ? Reactivity.mutation(effect, opts.reactivityKeys)
188
194
  : effect
189
195
  })
190
196
  )
191
- ) as any
197
+ if (withResponse === false) {
198
+ const definition = options.api.groups[group]!.endpoints[endpoint]! as HttpApiEndpoint.AnyWithProps
199
+ return Atom.serializable(atom, {
200
+ key: `AtomHttpApi:mutation:${group}:${endpoint}`,
201
+ schema: AsyncResult.Schema({
202
+ success: Schema.Union(HttpApiEndpoint.getSuccessSchemas(definition)),
203
+ error: Schema.Union(HttpApiEndpoint.getErrorSchemas(definition))
204
+ }) as any
205
+ })
206
+ }
207
+ return atom
208
+ }) as any
192
209
 
193
210
  self.mutation = ((group: string, endpoint: string, options?: {
194
211
  readonly withResponse?: boolean | undefined
@@ -202,8 +219,21 @@ export const Service = <Self>() =>
202
219
  const queryFamily = Atom.family((opts: QueryKey) => {
203
220
  let atom = self.runtime.atom(self.use((client_) => {
204
221
  const client = client_ as any
205
- return client[opts.group][opts.endpoint](opts) as Effect.Effect<any>
222
+ return catchErrors(client[opts.group][opts.endpoint](opts) as Effect.Effect<
223
+ any,
224
+ HttpClientError.HttpClientError | SchemaError
225
+ >)
206
226
  }))
227
+ if (opts.withResponse === false) {
228
+ const endpoint = options.api.groups[opts.group]!.endpoints[opts.endpoint]! as HttpApiEndpoint.AnyWithProps
229
+ atom = Atom.serializable(atom, {
230
+ key: makeSerializableKey(opts),
231
+ schema: AsyncResult.Schema({
232
+ success: Schema.Union(HttpApiEndpoint.getSuccessSchemas(endpoint)),
233
+ error: Schema.Union(HttpApiEndpoint.getErrorSchemas(endpoint))
234
+ }) as any
235
+ })
236
+ }
207
237
  if (opts.timeToLive) {
208
238
  atom = Duration.isFinite(opts.timeToLive)
209
239
  ? Atom.setIdleTTL(atom, opts.timeToLive)
@@ -261,3 +291,7 @@ interface QueryKey {
261
291
  reactivityKeys?: ReadonlyArray<unknown> | ReadonlyRecord<string, ReadonlyArray<unknown>> | undefined
262
292
  timeToLive?: Duration.Duration | undefined
263
293
  }
294
+
295
+ const makeSerializableKey = (
296
+ key: QueryKey
297
+ ): string => `AtomHttpApi:${key.group}:${key.endpoint}:${Hash.hash(key)}`
@@ -317,7 +317,11 @@ class RegistryImpl implements AtomRegistry {
317
317
  }
318
318
  if (initialValues !== undefined) {
319
319
  for (const [atom, value] of initialValues) {
320
- this.ensureNode(atom).setValue(value)
320
+ let target = atom
321
+ while (target.initialValueTarget) {
322
+ target = target.initialValueTarget
323
+ }
324
+ this.ensureNode(target).setInitialValue(value)
321
325
  }
322
326
  }
323
327
  }
@@ -558,6 +562,7 @@ class NodeImpl<A> {
558
562
  state: NodeState = NodeState.uninitialized
559
563
  lifetime: Lifetime<A> | undefined
560
564
  writeContext: WriteContextImpl<A>
565
+ preserveInitialValueOnBuild = false
561
566
 
562
567
  parents: Array<NodeImpl<any>> = []
563
568
  previousParents: Array<NodeImpl<any>> | undefined
@@ -588,7 +593,12 @@ class NodeImpl<A> {
588
593
  this.lifetime = makeLifetime(this)
589
594
  const value = this.atom.read(this.lifetime)
590
595
  if ((this.state & NodeFlags.waitingForValue) !== 0) {
591
- this.setValue(value)
596
+ if (this.preserveInitialValueOnBuild) {
597
+ this.preserveInitialValueOnBuild = false
598
+ this.state = NodeState.valid
599
+ } else {
600
+ this.setValue(value)
601
+ }
592
602
  }
593
603
 
594
604
  if (this.previousParents) {
@@ -613,6 +623,24 @@ class NodeImpl<A> {
613
623
  return Option.some(this._value)
614
624
  }
615
625
 
626
+ setInitialValue(value: A): void {
627
+ if ((this.state & NodeFlags.initialized) === 0) {
628
+ this.preserveInitialValueOnBuild = true
629
+ this.state = NodeState.stale
630
+ this._value = value
631
+
632
+ if (batchState.phase === BatchPhase.collect) {
633
+ batchState.notify.add(this)
634
+ } else {
635
+ this.notify()
636
+ }
637
+
638
+ return
639
+ }
640
+
641
+ this.setValue(value)
642
+ }
643
+
616
644
  setValue(value: A): void {
617
645
  if ((this.state & NodeFlags.initialized) === 0) {
618
646
  this.state = NodeState.valid
@@ -3,8 +3,10 @@
3
3
  */
4
4
  import * as Duration from "../../Duration.ts"
5
5
  import * as Effect from "../../Effect.ts"
6
+ import * as Hash from "../../Hash.ts"
6
7
  import * as Layer from "../../Layer.ts"
7
8
  import type { ReadonlyRecord } from "../../Record.ts"
9
+ import * as Schema from "../../Schema.ts"
8
10
  import type { Scope } from "../../Scope.ts"
9
11
  import * as ServiceMap from "../../ServiceMap.ts"
10
12
  import * as Stream from "../../Stream.ts"
@@ -12,11 +14,11 @@ import type { Mutable, NoInfer } from "../../Types.ts"
12
14
  import * as Headers from "../http/Headers.ts"
13
15
  import type * as Rpc from "../rpc/Rpc.ts"
14
16
  import * as RpcClient from "../rpc/RpcClient.ts"
15
- import type { RpcClientError } from "../rpc/RpcClientError.ts"
17
+ import { RpcClientError } from "../rpc/RpcClientError.ts"
16
18
  import type * as RpcGroup from "../rpc/RpcGroup.ts"
17
19
  import type { RequestId } from "../rpc/RpcMessage.ts"
18
20
  import * as RpcSchema from "../rpc/RpcSchema.ts"
19
- import type * as AsyncResult from "./AsyncResult.ts"
21
+ import * as AsyncResult from "./AsyncResult.ts"
20
22
  import * as Atom from "./Atom.ts"
21
23
  import * as Reactivity from "./Reactivity.ts"
22
24
 
@@ -24,7 +26,7 @@ import * as Reactivity from "./Reactivity.ts"
24
26
  * @since 4.0.0
25
27
  * @category Models
26
28
  */
27
- export interface AtomRpcClient<Self, Id extends string, Rpcs extends Rpc.Any, E> extends
29
+ export interface AtomRpcClient<Self, Id extends string, Rpcs extends Rpc.Any> extends
28
30
  ServiceMap.Service<
29
31
  Self,
30
32
  RpcClient.RpcClient.Flat<Rpcs, RpcClientError>
@@ -35,8 +37,8 @@ export interface AtomRpcClient<Self, Id extends string, Rpcs extends Rpc.Any, E>
35
37
  RpcClient.RpcClient.Flat<Rpcs, RpcClientError>
36
38
  >
37
39
 
38
- readonly layer: Layer.Layer<Self, E>
39
- readonly runtime: Atom.AtomRuntime<Self, E>
40
+ readonly layer: Layer.Layer<Self>
41
+ readonly runtime: Atom.AtomRuntime<Self>
40
42
 
41
43
  readonly mutation: <Tag extends Rpc.Tag<Rpcs>>(
42
44
  arg: Tag
@@ -58,7 +60,7 @@ export interface AtomRpcClient<Self, Id extends string, Rpcs extends Rpc.Any, E>
58
60
  readonly headers?: Headers.Input | undefined
59
61
  },
60
62
  _Success["Type"],
61
- _Error["Type"] | E | _Middleware["error"]["Type"]
63
+ _Error["Type"] | RpcClientError | _Middleware["error"]["Type"]
62
64
  >
63
65
  : never
64
66
 
@@ -82,14 +84,14 @@ export interface AtomRpcClient<Self, Id extends string, Rpcs extends Rpc.Any, E>
82
84
  > ? [_Success] extends [RpcSchema.Stream<infer _A, infer _E>] ? Atom.Writable<
83
85
  Atom.PullResult<
84
86
  _A["Type"],
85
- _E["Type"] | _Error["Type"] | E | _Middleware["error"]["Type"]
87
+ _E["Type"] | _Error["Type"] | RpcClientError | _Middleware["error"]["Type"]
86
88
  >,
87
89
  void
88
90
  >
89
91
  : Atom.Atom<
90
92
  AsyncResult.AsyncResult<
91
93
  _Success["Type"],
92
- _Error["Type"] | E | _Middleware["error"]["Type"]
94
+ _Error["Type"] | RpcClientError | _Middleware["error"]["Type"]
93
95
  >
94
96
  >
95
97
  : never
@@ -132,8 +134,8 @@ export const Service = <Self>() =>
132
134
  | undefined
133
135
  readonly runtime?: Atom.RuntimeFactory | undefined
134
136
  }
135
- ): AtomRpcClient<Self, Id, Rpcs, ER> => {
136
- const self: Mutable<AtomRpcClient<Self, Id, Rpcs, ER>> = ServiceMap.Service<
137
+ ): AtomRpcClient<Self, Id, Rpcs> => {
138
+ const self: Mutable<AtomRpcClient<Self, Id, Rpcs>> = ServiceMap.Service<
137
139
  Self,
138
140
  RpcClient.RpcClient.Flat<Rpcs, RpcClientError>
139
141
  >()(id) as any
@@ -149,12 +151,13 @@ export const Service = <Self>() =>
149
151
  never,
150
152
  RM
151
153
  >)
152
- ).pipe(Layer.provide(options.protocol))
154
+ ).pipe(Layer.provide(Layer.orDie(options.protocol)))
153
155
  const runtimeFactory = options.runtime ?? Atom.runtime
154
156
  self.runtime = runtimeFactory(self.layer)
155
157
 
156
- self.mutation = Atom.family(<Tag extends Rpc.Tag<Rpcs>>(tag: Tag) =>
157
- self.runtime.fn<{
158
+ self.mutation = Atom.family(<Tag extends Rpc.Tag<Rpcs>>(tag: Tag) => {
159
+ const rpc = options.group.requests.get(tag)! as any as Rpc.AnyWithProps
160
+ return self.runtime.fn<{
158
161
  readonly payload: Rpc.PayloadConstructor<Rpc.ExtractTag<Rpcs, Tag>>
159
162
  readonly reactivityKeys?:
160
163
  | ReadonlyArray<unknown>
@@ -169,13 +172,23 @@ export const Service = <Self>() =>
169
172
  ? Reactivity.mutation(effect, reactivityKeys)
170
173
  : effect
171
174
  }) as any
175
+ ).pipe(
176
+ Atom.serializable({
177
+ key: `AtomRpc:mutation:${tag}`,
178
+ schema: AsyncResult.Schema({
179
+ success: rpc.successSchema,
180
+ error: makeErrorSchema(rpc)
181
+ }) as any
182
+ })
172
183
  )
173
- ) as any
184
+ }) as any
174
185
 
175
186
  const queryFamily = Atom.family(
176
- ({ headers, payload, reactivityKeys, tag, timeToLive }: QueryKey) => {
187
+ (key: QueryKey) => {
188
+ const { headers, payload, reactivityKeys, tag, timeToLive } = key
177
189
  const rpc = options.group.requests.get(tag)! as any as Rpc.AnyWithProps
178
- let atom = RpcSchema.isStreamSchema(rpc.successSchema)
190
+ const isStream = RpcSchema.isStreamSchema(rpc.successSchema)
191
+ let atom = isStream
179
192
  ? self.runtime.pull(
180
193
  Stream.unwrap(
181
194
  self.use((client) =>
@@ -188,6 +201,15 @@ export const Service = <Self>() =>
188
201
  : self.runtime.atom(
189
202
  self.use((client) => client(tag, payload, { headers } as any)) as any
190
203
  )
204
+ if (!isStream) {
205
+ atom = Atom.serializable(atom, {
206
+ key: makeSerializableKey(key),
207
+ schema: AsyncResult.Schema({
208
+ success: rpc.successSchema,
209
+ error: makeErrorSchema(rpc)
210
+ }) as any
211
+ })
212
+ }
191
213
  if (timeToLive) {
192
214
  atom = Duration.isFinite(timeToLive)
193
215
  ? Atom.setIdleTTL(atom, timeToLive)
@@ -223,7 +245,7 @@ export const Service = <Self>() =>
223
245
  : undefined
224
246
  }) as any
225
247
 
226
- return self as AtomRpcClient<Self, Id, Rpcs, ER>
248
+ return self as AtomRpcClient<Self, Id, Rpcs>
227
249
  }
228
250
 
229
251
  interface QueryKey {
@@ -236,3 +258,12 @@ interface QueryKey {
236
258
  | undefined
237
259
  timeToLive?: Duration.Duration | undefined
238
260
  }
261
+
262
+ const makeErrorSchema = (rpc: Rpc.AnyWithProps): Schema.Top =>
263
+ Schema.Union([
264
+ rpc.errorSchema,
265
+ ...Array.from(rpc.middlewares, (middleware) => middleware.error),
266
+ RpcClientError
267
+ ])
268
+
269
+ const makeSerializableKey = (key: QueryKey): string => `AtomRpc:${key.tag}:${Hash.hash(key)}`
@@ -406,9 +406,7 @@ export type MiddlewareClient<R> = R extends Rpc<
406
406
  infer _Error,
407
407
  infer _Middleware,
408
408
  infer _Requires
409
- > ?
410
- _Middleware extends { readonly requiredForClient: true }
411
- ? RpcMiddleware.ForClient<ServiceMap.Service.Identifier<_Middleware>>
409
+ > ? _Middleware extends { readonly requiredForClient: true } ? RpcMiddleware.ForClient<_Middleware["Identifier"]>
412
410
  : never
413
411
  : never
414
412
 
@@ -109,7 +109,8 @@ export interface ServiceClass<
109
109
  Provides,
110
110
  E extends Schema.Top,
111
111
  ClientError,
112
- Requires
112
+ Requires,
113
+ RequiredForClient extends boolean
113
114
  > extends ServiceMap.Service<Self, RpcMiddleware<Provides, E["Type"], Requires>> {
114
115
  new(_: never): ServiceMap.ServiceClass.Shape<Name, RpcMiddleware<Provides, E["Type"], Requires>> & {
115
116
  readonly [TypeId]: {
@@ -121,7 +122,7 @@ export interface ServiceClass<
121
122
  }
122
123
  readonly [TypeId]: typeof TypeId
123
124
  readonly error: E
124
- readonly requiredForClient: boolean
125
+ readonly requiredForClient: RequiredForClient
125
126
  readonly "~ClientError": ClientError
126
127
  }
127
128
 
@@ -201,16 +202,20 @@ export const Service = <
201
202
  requires?: any
202
203
  provides?: any
203
204
  clientError?: any
204
- } = { requires: never; provides: never; clientError: never }
205
+ } = {
206
+ requires: never
207
+ provides: never
208
+ clientError: never
209
+ }
205
210
  >(): <
206
211
  const Name extends string,
207
212
  Error extends Schema.Top = Schema.Never,
208
- RequiredForClient extends boolean = false
213
+ const RequiredForClient extends boolean = false
209
214
  >(
210
215
  id: Name,
211
216
  options?: {
212
217
  readonly error?: Error | undefined
213
- readonly requiredForClient: RequiredForClient | undefined
218
+ readonly requiredForClient?: RequiredForClient | undefined
214
219
  } | undefined
215
220
  ) => ServiceClass<
216
221
  Self,
@@ -218,7 +223,8 @@ export const Service = <
218
223
  "provides" extends keyof Config ? Config["provides"] : never,
219
224
  Error,
220
225
  "clientError" extends keyof Config ? Config["clientError"] : never,
221
- "requires" extends keyof Config ? Config["requires"] : never
226
+ "requires" extends keyof Config ? Config["requires"] : never,
227
+ RequiredForClient
222
228
  > =>
223
229
  (
224
230
  id: string,
@@ -305,7 +305,7 @@ export const makeNoSerialization: <Rpcs extends Rpc.Any>(
305
305
  effect = concurrencySemaphore.withPermits(1)(effect)
306
306
  }
307
307
  const serviceMap = new Map(entry.services.mapUnsafe)
308
- serviceMap.forEach((value, key) => serviceMap.set(key, value))
308
+ requestFiber.services.mapUnsafe.forEach((value, key) => serviceMap.set(key, value))
309
309
  serviceMap.set(Scope.Scope.key, scope)
310
310
  const runFork = Effect.runForkWith(ServiceMap.makeUnsafe(serviceMap))
311
311
  const fiber = trackFiber(
@@ -1181,7 +1181,7 @@ export const makeProtocolStdio = Effect.gen(function*() {
1181
1181
  )
1182
1182
 
1183
1183
  yield* Stream.fromQueue(queue).pipe(
1184
- Stream.run(stdio.stdout),
1184
+ Stream.run(stdio.stdout()),
1185
1185
  Effect.retry(Schedule.spaced(500)),
1186
1186
  Effect.forkScoped
1187
1187
  )
@@ -30,7 +30,7 @@ export const withRun = <
30
30
  write = f
31
31
 
32
32
  for (const [args, context] of buffer) {
33
- yield* Effect.provide(write(...args), context)
33
+ yield* Effect.provideServices(Effect.suspend(() => f(...args)), context)
34
34
  }
35
35
  buffer = []
36
36
 
@@ -57,9 +57,9 @@ export const request: {
57
57
  } = function() {
58
58
  if (arguments.length === 1) {
59
59
  const resolver = arguments[0]
60
- return (payload: any) => Effect.request(SqlRequest(payload), Effect.succeed(resolver))
60
+ return (payload: any) => Effect.request(SqlRequest(payload), resolver)
61
61
  }
62
- return Effect.request(SqlRequest(arguments[0]), Effect.succeed(arguments[1]))
62
+ return Effect.request(SqlRequest(arguments[0]), arguments[1])
63
63
  } as any
64
64
 
65
65
  /**