effect 4.0.0-rc.118 → 4.0.0

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 (132) hide show
  1. package/README.md +17 -4
  2. package/dist/Array.d.ts +25 -25
  3. package/dist/Array.d.ts.map +1 -1
  4. package/dist/Array.js +16 -16
  5. package/dist/Array.js.map +1 -1
  6. package/dist/Chunk.d.ts +26 -26
  7. package/dist/Chunk.d.ts.map +1 -1
  8. package/dist/Chunk.js +15 -15
  9. package/dist/Chunk.js.map +1 -1
  10. package/dist/Effect.d.ts +17 -17
  11. package/dist/Effect.d.ts.map +1 -1
  12. package/dist/Effect.js +5 -5
  13. package/dist/MutableHashMap.d.ts.map +1 -1
  14. package/dist/MutableHashMap.js +4 -22
  15. package/dist/MutableHashMap.js.map +1 -1
  16. package/dist/Option.d.ts +17 -17
  17. package/dist/Option.d.ts.map +1 -1
  18. package/dist/Option.js +6 -6
  19. package/dist/Option.js.map +1 -1
  20. package/dist/Queue.d.ts +19 -2
  21. package/dist/Queue.d.ts.map +1 -1
  22. package/dist/Queue.js +9 -4
  23. package/dist/Queue.js.map +1 -1
  24. package/dist/Record.d.ts +14 -14
  25. package/dist/Record.d.ts.map +1 -1
  26. package/dist/Record.js +10 -10
  27. package/dist/Record.js.map +1 -1
  28. package/dist/Schema.d.ts +37 -21
  29. package/dist/Schema.d.ts.map +1 -1
  30. package/dist/Schema.js +29 -10
  31. package/dist/Schema.js.map +1 -1
  32. package/dist/SchemaAST.d.ts.map +1 -1
  33. package/dist/SchemaAST.js +116 -97
  34. package/dist/SchemaAST.js.map +1 -1
  35. package/dist/SchemaRepresentation.d.ts +3 -1
  36. package/dist/SchemaRepresentation.d.ts.map +1 -1
  37. package/dist/SchemaRepresentation.js +3 -1
  38. package/dist/SchemaRepresentation.js.map +1 -1
  39. package/dist/ai/McpSchema.d.ts +110 -110
  40. package/dist/ai/McpSchema.d.ts.map +1 -1
  41. package/dist/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
  42. package/dist/cluster/ClusterWorkflowEngine.js +5 -1
  43. package/dist/cluster/ClusterWorkflowEngine.js.map +1 -1
  44. package/dist/cluster/MessageStorage.d.ts +32 -8
  45. package/dist/cluster/MessageStorage.d.ts.map +1 -1
  46. package/dist/cluster/MessageStorage.js +2 -1
  47. package/dist/cluster/MessageStorage.js.map +1 -1
  48. package/dist/cluster/Sharding.d.ts +5 -2
  49. package/dist/cluster/Sharding.d.ts.map +1 -1
  50. package/dist/cluster/Sharding.js +1 -1
  51. package/dist/cluster/Sharding.js.map +1 -1
  52. package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
  53. package/dist/cluster/SqlMessageStorage.js +15 -4
  54. package/dist/cluster/SqlMessageStorage.js.map +1 -1
  55. package/dist/http/HttpEffect.d.ts +1 -0
  56. package/dist/http/HttpEffect.d.ts.map +1 -1
  57. package/dist/http/HttpEffect.js +29 -22
  58. package/dist/http/HttpEffect.js.map +1 -1
  59. package/dist/internal/effect.js +75 -84
  60. package/dist/internal/effect.js.map +1 -1
  61. package/dist/internal/schema/annotations.js +1 -7
  62. package/dist/internal/schema/annotations.js.map +1 -1
  63. package/dist/internal/schema/codegen.js +56 -28
  64. package/dist/internal/schema/codegen.js.map +1 -1
  65. package/dist/internal/schema/toCodeDocument.js +10 -30
  66. package/dist/internal/schema/toCodeDocument.js.map +1 -1
  67. package/dist/internal/schema/toCodec.js +1 -15
  68. package/dist/internal/schema/toCodec.js.map +1 -1
  69. package/dist/internal/schema/toEquivalence.js +4 -3
  70. package/dist/internal/schema/toEquivalence.js.map +1 -1
  71. package/dist/internal/schema/toFormatter.js +4 -4
  72. package/dist/internal/schema/toFormatter.js.map +1 -1
  73. package/dist/reactivity/Atom.d.ts.map +1 -1
  74. package/dist/reactivity/Atom.js +6 -2
  75. package/dist/reactivity/Atom.js.map +1 -1
  76. package/dist/reactivity/AtomRpc.d.ts +3 -3
  77. package/dist/reactivity/AtomRpc.d.ts.map +1 -1
  78. package/dist/rpc/Rpc.d.ts +2 -2
  79. package/dist/rpc/Rpc.d.ts.map +1 -1
  80. package/dist/rpc/RpcClient.d.ts +7 -7
  81. package/dist/rpc/RpcClient.d.ts.map +1 -1
  82. package/dist/rpc/RpcClient.js +1 -2
  83. package/dist/rpc/RpcClient.js.map +1 -1
  84. package/dist/rpc/RpcMessage.d.ts +1 -1
  85. package/dist/rpc/RpcMessage.d.ts.map +1 -1
  86. package/dist/schema/SchemaCompiler/runtime.js +6 -1
  87. package/dist/schema/SchemaCompiler/runtime.js.map +1 -1
  88. package/dist/testing/TestSchema.d.ts +234 -6
  89. package/dist/testing/TestSchema.d.ts.map +1 -1
  90. package/dist/testing/TestSchema.js +244 -25
  91. package/dist/testing/TestSchema.js.map +1 -1
  92. package/dist/testing/index.d.ts +1 -0
  93. package/dist/testing/index.d.ts.map +1 -1
  94. package/dist/testing/index.js +1 -0
  95. package/dist/testing/index.js.map +1 -1
  96. package/dist/workflow/DurableDeferred.js +1 -1
  97. package/dist/workflow/DurableDeferred.js.map +1 -1
  98. package/dist/workflow/Workflow.js +1 -1
  99. package/dist/workflow/Workflow.js.map +1 -1
  100. package/package.json +1 -1
  101. package/src/Array.ts +31 -31
  102. package/src/Chunk.ts +29 -29
  103. package/src/Effect.ts +17 -17
  104. package/src/MutableHashMap.ts +4 -24
  105. package/src/Option.ts +19 -19
  106. package/src/Queue.ts +26 -6
  107. package/src/Record.ts +20 -20
  108. package/src/Schema.ts +48 -24
  109. package/src/SchemaAST.ts +135 -115
  110. package/src/SchemaRepresentation.ts +3 -1
  111. package/src/cluster/ClusterWorkflowEngine.ts +8 -1
  112. package/src/cluster/MessageStorage.ts +22 -5
  113. package/src/cluster/Sharding.ts +8 -4
  114. package/src/cluster/SqlMessageStorage.ts +19 -8
  115. package/src/http/HttpEffect.ts +15 -9
  116. package/src/internal/effect.ts +37 -62
  117. package/src/internal/schema/annotations.ts +0 -9
  118. package/src/internal/schema/codegen.ts +90 -43
  119. package/src/internal/schema/toCodeDocument.ts +10 -47
  120. package/src/internal/schema/toCodec.ts +3 -16
  121. package/src/internal/schema/toEquivalence.ts +4 -5
  122. package/src/internal/schema/toFormatter.ts +4 -6
  123. package/src/reactivity/Atom.ts +6 -2
  124. package/src/reactivity/AtomRpc.ts +5 -5
  125. package/src/rpc/Rpc.ts +2 -2
  126. package/src/rpc/RpcClient.ts +9 -10
  127. package/src/rpc/RpcMessage.ts +1 -1
  128. package/src/schema/SchemaCompiler/runtime.ts +17 -1
  129. package/src/testing/TestSchema.ts +315 -63
  130. package/src/testing/index.ts +1 -0
  131. package/src/workflow/DurableDeferred.ts +1 -1
  132. package/src/workflow/Workflow.ts +1 -1
@@ -33,22 +33,9 @@ function validateCanonicalObjectPropertyNames(ast: SchemaAST.Objects): void {
33
33
 
34
34
  function makeReorder(getPriority: (ast: SchemaAST.AST) => number) {
35
35
  return (types: ReadonlyArray<SchemaAST.AST>): ReadonlyArray<SchemaAST.AST> => {
36
- // Create a map of original indices for O(1) lookup
37
- const indexMap = new Map<SchemaAST.AST, number>()
38
- for (let i = 0; i < types.length; i++) {
39
- indexMap.set(SchemaAST.toEncoded(types[i]), i)
40
- }
41
-
42
- // Create a sorted copy of the types array
43
- const sortedTypes = [...types].sort((a, b) => {
44
- a = SchemaAST.toEncoded(a)
45
- b = SchemaAST.toEncoded(b)
46
- const pa = getPriority(a)
47
- const pb = getPriority(b)
48
- if (pa !== pb) return pa - pb
49
- // If priorities are equal, maintain original order (stable sort)
50
- return indexMap.get(a)! - indexMap.get(b)!
51
- })
36
+ const sortedTypes = [...types].sort((a, b) =>
37
+ getPriority(SchemaAST.toEncoded(a)) - getPriority(SchemaAST.toEncoded(b))
38
+ )
52
39
 
53
40
  // Check if order changed by comparing arrays
54
41
  const orderChanged = sortedTypes.some((ast, index) => ast !== types[index])
@@ -136,13 +136,12 @@ function recur(ast: SchemaAST.AST): Equivalence.Equivalence<any> {
136
136
  }
137
137
  case "Union": {
138
138
  const types = SchemaAST.toType(ast).types
139
- const compiled = new Map(
140
- types.map((candidate, i) => [candidate, [SchemaParser._is(candidate), recur(ast.types[i])] as const] as const)
141
- )
139
+ const index = SchemaAST.getCandidateIndex(types)
140
+ const compiled = types.map((candidate, i) => [SchemaParser._is(candidate), recur(ast.types[i])] as const)
142
141
  return Equivalence.make((a, b) => {
143
- const candidates = SchemaAST.getCandidates(a, types)
142
+ const candidates = index(a, false)
144
143
  for (let i = 0; i < candidates.length; i++) {
145
- const [is, equivalence] = compiled.get(candidates[i])!
144
+ const [is, equivalence] = compiled[candidates[i]]
146
145
  if (is(a) && is(b)) {
147
146
  return equivalence(a, b)
148
147
  }
@@ -217,14 +217,12 @@ export function toFormatter<T>(ast: SchemaAST.AST, options?: {
217
217
  }
218
218
  case "Union": {
219
219
  const types = SchemaAST.toType(ast).types
220
- const getCandidates = (value: any) => SchemaAST.getCandidates(value, types)
221
- const compiled = new Map(
222
- types.map((candidate, i) => [candidate, [SchemaParser._is(candidate), recur(ast.types[i])] as const] as const)
223
- )
220
+ const index = SchemaAST.getCandidateIndex(types)
221
+ const compiled = types.map((candidate, i) => [SchemaParser._is(candidate), recur(ast.types[i])] as const)
224
222
  return (value) => {
225
- const candidates = getCandidates(value)
223
+ const candidates = index(value, false)
226
224
  for (let i = 0; i < candidates.length; i++) {
227
- const [is, formatter] = compiled.get(candidates[i])!
225
+ const [is, formatter] = compiled[candidates[i]]
228
226
  if (is(value)) {
229
227
  return formatter(value)
230
228
  }
@@ -1453,8 +1453,9 @@ function makeResultFn<Arg, E, A>(
1453
1453
  Effect.forkDetach(eff, { startImmediately: true }),
1454
1454
  (fiber) => {
1455
1455
  fibers.add(fiber)
1456
+ const runningFibers = Array.from(fibers)
1456
1457
  fiber.addObserver(() => fibers.delete(fiber))
1457
- return Effect.map(Fiber.joinAll(fibers), (arr) => arr[0])
1458
+ return Effect.map(Fiber.joinAll(runningFibers), (arr) => arr[0])
1458
1459
  }
1459
1460
  )
1460
1461
  }
@@ -1619,7 +1620,10 @@ export const family = typeof WeakRef === "undefined" || typeof FinalizationRegis
1619
1620
  ): (arg: Arg) => T => {
1620
1621
  const atoms = MutableHashMap.empty<Arg, WeakRef<T>>()
1621
1622
  const registry = new FinalizationRegistry<Arg>((arg) => {
1622
- MutableHashMap.remove(atoms, arg)
1623
+ const entry = MutableHashMap.get(atoms, arg)
1624
+ if (entry._tag === "Some" && entry.value.deref() === undefined) {
1625
+ MutableHashMap.remove(atoms, arg)
1626
+ }
1623
1627
  })
1624
1628
  return function(arg) {
1625
1629
  const atomEntry = MutableHashMap.get(atoms, arg).pipe(
@@ -57,8 +57,8 @@ export interface AtomRpcClient<Self, Id extends string, Rpcs extends Rpc.Any> ex
57
57
 
58
58
  readonly mutation: <Tag extends Rpc.Tag<Rpcs>>(
59
59
  arg: Tag
60
- ) => Rpc.ExtractTag<Rpcs, Tag> extends Rpc.Rpc<
61
- infer _Tag,
60
+ ) => Rpcs extends Rpc.Rpc<
61
+ infer _Tag extends Tag,
62
62
  infer _Payload,
63
63
  infer _Success,
64
64
  infer _Error,
@@ -67,7 +67,7 @@ export interface AtomRpcClient<Self, Id extends string, Rpcs extends Rpc.Any> ex
67
67
  > ? [_Success] extends [RpcSchema.Stream<infer _A, infer _E>] ? never
68
68
  : Atom.AtomResultFn<
69
69
  {
70
- readonly payload: Rpc.PayloadConstructor<Rpc.ExtractTag<Rpcs, Tag>>
70
+ readonly payload: Rpc.PayloadConstructor<Rpcs>
71
71
  readonly reactivityKeys?:
72
72
  | ReadonlyArray<unknown>
73
73
  | ReadonlyRecord<string, ReadonlyArray<unknown>>
@@ -91,8 +91,8 @@ export interface AtomRpcClient<Self, Id extends string, Rpcs extends Rpc.Any> ex
91
91
  readonly timeToLive?: Duration.Input | undefined
92
92
  readonly serializationKey?: string | undefined
93
93
  }
94
- ) => Rpc.ExtractTag<Rpcs, Tag> extends Rpc.Rpc<
95
- infer _Tag,
94
+ ) => Rpcs extends Rpc.Rpc<
95
+ infer _Tag extends Tag,
96
96
  infer _Payload,
97
97
  infer _Success,
98
98
  infer _Error,
package/src/rpc/Rpc.ts CHANGED
@@ -687,14 +687,14 @@ export type IsStream<R extends Any, Tag extends string> = R extends Rpc<
687
687
  never
688
688
 
689
689
  /**
690
- * Extracts the RPC with the specified tag from an RPC union.
690
+ * Extracts RPCs whose tags match the given tag or union of tags.
691
691
  *
692
692
  * @stability unstable
693
693
  * @category utility types
694
694
  * @since 4.0.0
695
695
  */
696
696
  export type ExtractTag<R extends Any, Tag extends string> = R extends Rpc<
697
- Tag,
697
+ infer _Tag extends Tag,
698
698
  infer _Payload,
699
699
  infer _Success,
700
700
  infer _Error,
@@ -166,8 +166,8 @@ export declare namespace RpcClient {
166
166
  readonly context?: Context.Context<never> | undefined
167
167
  readonly discard?: Discard | undefined
168
168
  }
169
- ) => Rpc.ExtractTag<Rpcs, Tag> extends Rpc.Rpc<
170
- infer _Tag,
169
+ ) => Rpcs extends Rpc.Rpc<
170
+ infer _Tag extends Tag,
171
171
  infer _Payload,
172
172
  infer _Success,
173
173
  infer _Error,
@@ -1051,9 +1051,9 @@ export const makeProtocolSocket = (options?: {
1051
1051
  readonly retryPolicy?: Schedule.Schedule<any, Socket.SocketError> | undefined
1052
1052
  /**
1053
1053
  * Runs for each retried `SocketOpenError` when `retryTransientErrors` is enabled.
1054
- * Ping timeouts are also reported because the protocol classifies them as
1055
- * `SocketOpenError`. The returned `Effect<void>` cannot fail with a typed error
1056
- * or require services; defects are logged and ignored so retries can continue.
1054
+ * A missed pong fails in-flight calls and is not reported through this hook.
1055
+ * The returned `Effect<void>` cannot fail with a typed error or require
1056
+ * services; defects are logged and ignored so retries can continue.
1057
1057
  */
1058
1058
  readonly onTransientError?: ((error: RpcClientError) => Effect.Effect<void>) | undefined
1059
1059
  }): Effect.Effect<
@@ -1149,8 +1149,7 @@ export const makeProtocolSocket = (options?: {
1149
1149
  () =>
1150
1150
  Effect.fail(
1151
1151
  new Socket.SocketError({
1152
- reason: new Socket.SocketOpenError({
1153
- kind: "Timeout",
1152
+ reason: new Socket.SocketReadError({
1154
1153
  cause: new Error("ping timeout")
1155
1154
  })
1156
1155
  })
@@ -1251,9 +1250,9 @@ export const layerProtocolSocket = (options?: {
1251
1250
  readonly retryTransientErrors?: boolean | undefined
1252
1251
  /**
1253
1252
  * Runs for each retried `SocketOpenError` when `retryTransientErrors` is enabled.
1254
- * Ping timeouts are also reported because the protocol classifies them as
1255
- * `SocketOpenError`. The returned `Effect<void>` cannot fail with a typed error
1256
- * or require services; defects are logged and ignored so retries can continue.
1253
+ * A missed pong fails in-flight calls and is not reported through this hook.
1254
+ * The returned `Effect<void>` cannot fail with a typed error or require
1255
+ * services; defects are logged and ignored so retries can continue.
1257
1256
  */
1258
1257
  readonly onTransientError?: ((error: RpcClientError) => Effect.Effect<void>) | undefined
1259
1258
  }): Layer.Layer<
@@ -297,7 +297,7 @@ export type ExitEncoded<A, E> = {
297
297
  readonly defect: unknown
298
298
  } | {
299
299
  readonly _tag: "Interrupt"
300
- readonly fiberId: number | undefined
300
+ readonly fiberId: number | null | undefined
301
301
  }
302
302
  >
303
303
  }
@@ -237,6 +237,21 @@ const invalidEncoding = (
237
237
  ? invalidType(ast, value, options)
238
238
  : Interpreter.wrapEncoding(ast, input, options, invalidType(ast.encoding[index - 1].to, value, options))
239
239
 
240
+ const invalidEncodingChecks = (
241
+ ast: SchemaAST.AST & { readonly encoding: SchemaAST.Encoding },
242
+ input: unknown,
243
+ issues: readonly [SchemaIssue.Issue, ...Array<SchemaIssue.Issue>],
244
+ options: SchemaAST.ParseOptions
245
+ ) => {
246
+ const source = ast.encoding[ast.encoding.length - 1].to
247
+ return Interpreter.wrapEncoding(
248
+ ast,
249
+ input,
250
+ options,
251
+ Effect.fail(new SchemaIssue.Composite(source, issues, input, options))
252
+ )
253
+ }
254
+
240
255
  /**
241
256
  * @internal
242
257
  */
@@ -255,6 +270,7 @@ export const runtime = {
255
270
  die: Effect.die,
256
271
  invalidType,
257
272
  invalidEncoding,
273
+ invalidEncodingChecks,
258
274
  getCheckIssues,
259
275
  check,
260
276
  getExpectedKeys: (ast: SchemaAST.Objects) =>
@@ -262,7 +278,7 @@ export const runtime = {
262
278
  hasExcessProperties,
263
279
  matchesTemplateLiteral: (ast: SchemaAST.TemplateLiteral, input: unknown, options: SchemaAST.ParseOptions) =>
264
280
  typeof input === "string" && ast.matchPart(input, options) !== undefined,
265
- getCandidates: SchemaAST.getCandidates,
281
+ getCandidateIndex: SchemaAST.getCandidateIndex,
266
282
  getIndexSignatureKeys: SchemaAST.getIndexSignatureKeys,
267
283
  parameterFromPropertyKey: SchemaAST.parameterFromPropertyKey,
268
284
  getConstructorDescriptor: SchemaAST.getConstructorDescriptor,