liminal 0.17.1 → 0.17.2

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 (109) hide show
  1. package/Accumulator.ts +16 -10
  2. package/Actor.ts +35 -29
  3. package/Audition.ts +17 -12
  4. package/CHANGELOG.md +6 -0
  5. package/Client.ts +260 -295
  6. package/ClientHandle.ts +17 -6
  7. package/F.ts +3 -9
  8. package/LICENSE +202 -0
  9. package/Method.ts +11 -13
  10. package/Protocol.ts +104 -126
  11. package/Send.ts +9 -5
  12. package/_util/Diagnostic.ts +16 -0
  13. package/_util/Mutex.ts +5 -5
  14. package/dist/Accumulator.d.ts +2 -2
  15. package/dist/Accumulator.js +12 -8
  16. package/dist/Accumulator.js.map +1 -1
  17. package/dist/Actor.d.ts +9 -10
  18. package/dist/Actor.js +6 -15
  19. package/dist/Actor.js.map +1 -1
  20. package/dist/Audition.d.ts +3 -4
  21. package/dist/Audition.js +7 -5
  22. package/dist/Audition.js.map +1 -1
  23. package/dist/Client.d.ts +20 -39
  24. package/dist/Client.js +119 -139
  25. package/dist/Client.js.map +1 -1
  26. package/dist/ClientHandle.d.ts +5 -6
  27. package/dist/ClientHandle.js +1 -1
  28. package/dist/ClientHandle.js.map +1 -1
  29. package/dist/F.d.ts +3 -12
  30. package/dist/F.js +1 -3
  31. package/dist/F.js.map +1 -1
  32. package/dist/Method.d.ts +11 -12
  33. package/dist/Method.js.map +1 -1
  34. package/dist/Protocol.d.ts +46 -89
  35. package/dist/Protocol.js +35 -11
  36. package/dist/Protocol.js.map +1 -1
  37. package/dist/Send.d.ts +2 -3
  38. package/dist/Send.js +1 -1
  39. package/dist/Send.js.map +1 -1
  40. package/dist/_util/Diagnostic.d.ts +5 -0
  41. package/dist/_util/Diagnostic.js +10 -0
  42. package/dist/_util/Diagnostic.js.map +1 -0
  43. package/dist/_util/Mutex.d.ts +2 -2
  44. package/dist/_util/Mutex.js +4 -4
  45. package/dist/_util/Mutex.js.map +1 -1
  46. package/dist/errors.d.ts +15 -12
  47. package/dist/errors.js +6 -4
  48. package/dist/errors.js.map +1 -1
  49. package/dist/experimental/BranchLive.d.ts +1 -1
  50. package/dist/experimental/BranchLive.js +1 -1
  51. package/dist/experimental/BranchLive.js.map +1 -1
  52. package/dist/experimental/L/append.d.ts +1 -1
  53. package/dist/experimental/L/append.js +2 -2
  54. package/dist/experimental/L/append.js.map +1 -1
  55. package/dist/experimental/L/assistant.d.ts +2 -2
  56. package/dist/experimental/L/assistant.js +1 -1
  57. package/dist/experimental/L/assistant.js.map +1 -1
  58. package/dist/experimental/L/assistantSchema.d.ts +2 -2
  59. package/dist/experimental/L/assistantSchema.js +1 -1
  60. package/dist/experimental/L/assistantSchema.js.map +1 -1
  61. package/dist/experimental/L/assistantStream.d.ts +2 -2
  62. package/dist/experimental/L/assistantStream.js +1 -1
  63. package/dist/experimental/L/assistantStream.js.map +1 -1
  64. package/dist/experimental/L/branch.d.ts +1 -1
  65. package/dist/experimental/L/clear.d.ts +1 -1
  66. package/dist/experimental/L/clear.js +1 -1
  67. package/dist/experimental/L/clear.js.map +1 -1
  68. package/dist/experimental/L/history.d.ts +2 -2
  69. package/dist/experimental/L/history.js +2 -2
  70. package/dist/experimental/L/history.js.map +1 -1
  71. package/dist/experimental/L/init.d.ts +1 -1
  72. package/dist/experimental/L/init.js +1 -1
  73. package/dist/experimental/L/init.js.map +1 -1
  74. package/dist/experimental/L/matrix.d.ts +2 -2
  75. package/dist/experimental/L/system.d.ts +1 -1
  76. package/dist/experimental/L/system.js +1 -1
  77. package/dist/experimental/L/system.js.map +1 -1
  78. package/dist/experimental/L/user.d.ts +1 -1
  79. package/dist/experimental/L/user.js +1 -1
  80. package/dist/experimental/L/user.js.map +1 -1
  81. package/dist/experimental/Loader.d.ts +3 -3
  82. package/dist/experimental/Loader.js +2 -2
  83. package/dist/experimental/Loader.js.map +1 -1
  84. package/dist/experimental/TaggedTemplateFunction.d.ts +2 -2
  85. package/dist/experimental/Template.d.ts +8 -12
  86. package/dist/experimental/Template.js +53 -51
  87. package/dist/experimental/Template.js.map +1 -1
  88. package/dist/tsconfig.tsbuildinfo +1 -1
  89. package/errors.ts +12 -4
  90. package/experimental/BranchLive.ts +1 -1
  91. package/experimental/L/append.ts +2 -2
  92. package/experimental/L/assistant.ts +1 -1
  93. package/experimental/L/assistantSchema.ts +3 -3
  94. package/experimental/L/assistantStream.ts +1 -1
  95. package/experimental/L/clear.ts +1 -1
  96. package/experimental/L/history.ts +2 -2
  97. package/experimental/L/init.ts +1 -1
  98. package/experimental/L/matrix.ts +3 -3
  99. package/experimental/L/system.ts +1 -1
  100. package/experimental/L/user.ts +1 -1
  101. package/experimental/Loader.ts +3 -3
  102. package/experimental/TaggedTemplateFunction.ts +2 -2
  103. package/experimental/Template.ts +62 -70
  104. package/package.json +2 -6
  105. package/tsconfig.json +1 -1
  106. package/_types.ts +0 -27
  107. package/dist/_types.d.ts +0 -22
  108. package/dist/_types.js +0 -2
  109. package/dist/_types.js.map +0 -1
package/Accumulator.ts CHANGED
@@ -1,4 +1,8 @@
1
- import { Deferred, Types, Option, Ref, PubSub, Stream, Effect, Context, Layer } from "effect"
1
+ import { Deferred, Types, Option, Ref, PubSub, Stream, Effect, Context, Layer, Semaphore } from "effect"
2
+
3
+ import * as Diagnostic from "./_util/Diagnostic.ts"
4
+
5
+ const { debug } = Diagnostic.module("Accumulator")
2
6
 
3
7
  const TypeId = "~liminal/Accumulator" as const
4
8
 
@@ -20,8 +24,8 @@ export type Reduce<State, Item, K extends Types.Tags<Item> = Types.Tags<Item>, E
20
24
  item: Types.ExtractTag<Item, K>,
21
25
  ) => (accumulator: State) => Effect.Effect<State, E, R>
22
26
 
23
- export interface Accumulator<Self, Id extends string, State> extends Context.Tag<Self, Service<State>> {
24
- new (_: never): Context.TagClassShape<Id, Service<State>>
27
+ export interface Accumulator<Self, Id extends string, State> extends Context.Service<Self, Service<State>> {
28
+ new (_: never): Context.ServiceClass.Shape<Id, Service<State>>
25
29
 
26
30
  readonly [TypeId]: typeof TypeId
27
31
 
@@ -42,14 +46,14 @@ export interface Accumulator<Self, Id extends string, State> extends Context.Tag
42
46
  export const Service =
43
47
  <Self, State>() =>
44
48
  <Id extends string>(id: Id): Accumulator<Self, Id, State> => {
45
- const tag = Context.Tag(id)<Self, Service<State>>()
49
+ const tag = Context.Service<Self, Service<State>>()(id)
46
50
 
47
- const get = tag.pipe(
51
+ const get = tag.asEffect().pipe(
48
52
  Effect.map(({ ref }) => ref),
49
53
  Effect.flatMap(Ref.get),
50
54
  )
51
55
 
52
- const stream = tag.pipe(
56
+ const stream = tag.asEffect().pipe(
53
57
  Effect.map(({ pubsub }) => Stream.fromPubSub(pubsub)),
54
58
  Stream.unwrap,
55
59
  )
@@ -65,7 +69,7 @@ export const Service =
65
69
  reduce,
66
70
  }: AccumulatorLayerConfig<Item, E, R, State, E2, R2, E3, R3>): Layer.Layer<Self, E | E2 | E3, R | R2 | R3> =>
67
71
  Effect.gen(function* () {
68
- const semaphore = yield* Effect.makeSemaphore(1)
72
+ const semaphore = yield* Semaphore.make(1)
69
73
  const deferred = yield* Deferred.make<State>()
70
74
  const pubsub = yield* PubSub.unbounded<State>({ replay: 1 })
71
75
  yield* source.pipe(
@@ -76,22 +80,24 @@ export const Service =
76
80
  if (Option.isSome(match)) {
77
81
  const { value } = match
78
82
  yield* Deferred.succeed(deferred, value)
83
+ yield* debug("InitializedState", { state: value })
79
84
  }
80
85
  return
81
86
  }
82
87
  const current = yield* Ref.get(ref)
83
88
  const reduced = yield* reduce(item)(current)
84
89
  yield* Ref.set(ref, reduced)
85
- yield* pubsub.publish(reduced)
90
+ yield* PubSub.publish(pubsub, reduced)
91
+ yield* debug("ReducedState", { item, previous: current, current: reduced })
86
92
  }, semaphore.withPermits(1)),
87
93
  ),
88
94
  Effect.forkScoped,
89
95
  )
90
96
  const initial_ = yield* Deferred.await(deferred)
91
97
  const ref = yield* Ref.make(initial_)
92
- yield* pubsub.publish(initial_)
98
+ yield* PubSub.publish(pubsub, initial_)
93
99
  return { ref, pubsub }
94
- }).pipe(Layer.scoped(tag))
100
+ }).pipe(Layer.effect(tag))
95
101
 
96
102
  return Object.assign(tag, { [TypeId]: TypeId, get, stream, reducer, layer })
97
103
  }
package/Actor.ts CHANGED
@@ -1,15 +1,23 @@
1
1
  import { Context, Schema as S, Effect } from "effect"
2
2
 
3
- import type { FieldsRecord, Fields } from "./_types.ts"
4
3
  import type * as ActorClient from "./Client.ts"
5
4
  import type * as ClientHandle from "./ClientHandle.ts"
6
5
  import type { MethodDefinition } from "./Method.ts"
7
- import * as Method from "./Method.ts"
8
6
  import type { Send } from "./Send.ts"
9
7
 
8
+ import * as Diagnostic from "./_util/Diagnostic.ts"
9
+ import * as Method from "./Method.ts"
10
+
11
+ const { span } = Diagnostic.module("Actor")
12
+
10
13
  export const TypeId = "~liminal/Actor" as const
11
14
 
12
- export interface Service<ActorSelf, NameA, AttachmentFields extends Fields, EventDefinitions extends FieldsRecord> {
15
+ export interface Service<
16
+ ActorSelf,
17
+ NameA,
18
+ AttachmentFields extends S.Struct.Fields,
19
+ EventDefinitions extends Record<string, S.Struct.Fields>,
20
+ > {
13
21
  readonly name: NameA
14
22
 
15
23
  readonly currentClient: ClientHandle.ClientHandle<ActorSelf, AttachmentFields, EventDefinitions>
@@ -19,13 +27,13 @@ export interface Service<ActorSelf, NameA, AttachmentFields extends Fields, Even
19
27
 
20
28
  export interface ActorDefinition<
21
29
  NameA,
22
- AttachmentFields extends Fields,
30
+ AttachmentFields extends S.Struct.Fields,
23
31
  ClientSelf,
24
32
  ClientId extends string,
25
33
  MethodDefinitions extends Record<string, MethodDefinition.Any>,
26
- EventDefinitions extends FieldsRecord,
34
+ EventDefinitions extends Record<string, S.Struct.Fields>,
27
35
  > {
28
- readonly name: S.Schema<NameA, string>
36
+ readonly name: S.Codec<NameA, string>
29
37
 
30
38
  readonly attachments: AttachmentFields
31
39
 
@@ -36,13 +44,13 @@ export interface Actor<
36
44
  ActorSelf,
37
45
  ActorId extends string,
38
46
  NameA,
39
- AttachmentFields extends Fields,
47
+ AttachmentFields extends S.Struct.Fields,
40
48
  ActorClientSelf,
41
49
  ActorClientId extends string,
42
50
  MethodDefinitions extends Record<string, MethodDefinition.Any>,
43
- EventDefinitions extends FieldsRecord,
44
- > extends Context.Tag<ActorSelf, Service<ActorSelf, NameA, AttachmentFields, EventDefinitions>> {
45
- new (_: never): Context.TagClassShape<ActorId, Service<ActorSelf, NameA, AttachmentFields, EventDefinitions>>
51
+ EventDefinitions extends Record<string, S.Struct.Fields>,
52
+ > extends Context.Service<ActorSelf, Service<ActorSelf, NameA, AttachmentFields, EventDefinitions>> {
53
+ new (_: never): Context.ServiceClass.Shape<ActorId, Service<ActorSelf, NameA, AttachmentFields, EventDefinitions>>
46
54
 
47
55
  readonly [TypeId]: typeof TypeId
48
56
 
@@ -56,12 +64,12 @@ export interface Actor<
56
64
  >
57
65
 
58
66
  readonly schema: {
59
- readonly attachments: S.Schema<S.Struct<AttachmentFields>["Type"], S.Struct<AttachmentFields>["Encoded"]>
67
+ readonly attachments: S.Codec<S.Struct<AttachmentFields>["Type"], S.Struct<AttachmentFields>["Encoded"]>
60
68
  }
61
69
 
62
70
  readonly sendAll: Send<ActorSelf, EventDefinitions>
63
71
 
64
- readonly evict: Effect.Effect<void, never, ActorSelf>
72
+ readonly disconnectAll: Effect.Effect<void, never, ActorSelf>
65
73
 
66
74
  readonly handler: <K extends keyof MethodDefinitions, R>(
67
75
  tag: K,
@@ -74,31 +82,29 @@ export const Service =
74
82
  <
75
83
  ActorId extends string,
76
84
  NameA,
77
- AttachmentFields extends Fields,
85
+ AttachmentFields extends S.Struct.Fields,
78
86
  ClientSelf,
79
87
  ClientId extends string,
80
88
  MethodDefinitions extends Record<string, MethodDefinition.Any>,
81
- EventDefinitions extends FieldsRecord,
89
+ EventDefinitions extends Record<string, S.Struct.Fields>,
82
90
  >(
83
91
  id: ActorId,
84
92
  definition: ActorDefinition<NameA, AttachmentFields, ClientSelf, ClientId, MethodDefinitions, EventDefinitions>,
85
93
  ): Actor<ActorSelf, ActorId, NameA, AttachmentFields, ClientSelf, ClientId, MethodDefinitions, EventDefinitions> => {
86
- const tag = Context.Tag(id)<ActorSelf, Service<ActorSelf, NameA, AttachmentFields, EventDefinitions>>()
94
+ const tag = Context.Service<ActorSelf, Service<ActorSelf, NameA, AttachmentFields, EventDefinitions>>()(id)
87
95
 
88
- const sendAll: Send<ActorSelf, EventDefinitions> = Effect.fnUntraced(function* (key, payload) {
89
- const { clients } = yield* tag
90
- yield* Effect.forEach(clients, (client) => client.send(key, payload), {
91
- concurrency: "unbounded",
92
- })
93
- })
96
+ const sendAll: Send<ActorSelf, EventDefinitions> = (key, payload) =>
97
+ tag.asEffect().pipe(
98
+ Effect.flatMap(({ clients }) =>
99
+ Effect.forEach(clients, (client) => client.send(key, payload), { concurrency: "unbounded" }),
100
+ ),
101
+ span("sendAll"),
102
+ )
94
103
 
95
- // TODO: more eviction
96
- const evict = Effect.gen(function* () {
97
- const { clients } = yield* tag
98
- for (const client of clients) {
99
- yield* client.disconnect
100
- }
101
- })
104
+ const disconnectAll = tag.asEffect().pipe(
105
+ Effect.flatMap(({ clients }) => Effect.forEach(clients, ({ disconnect }) => disconnect)),
106
+ span("disconnectAll"),
107
+ )
102
108
 
103
109
  const handler = <K extends keyof MethodDefinitions, R>(
104
110
  _tag: K,
@@ -112,7 +118,7 @@ export const Service =
112
118
  attachments: S.Struct(definition.attachments) as never,
113
119
  },
114
120
  sendAll,
115
- evict,
121
+ disconnectAll,
116
122
  handler,
117
123
  })
118
124
  }
package/Audition.ts CHANGED
@@ -1,12 +1,14 @@
1
- import { Pipeable, Stream, Effect, Function, Console } from "effect"
1
+ import { Schema as S, Pipeable, Stream, Effect, Function } from "effect"
2
2
 
3
- import type { FieldsRecord } from "./_types.ts"
4
3
  import type { F } from "./F.ts"
5
4
  import type * as Method from "./Method.ts"
6
5
 
6
+ import * as Diagnostic from "./_util/Diagnostic.ts"
7
7
  import * as Client from "./Client.ts"
8
8
  import { type ClientError, AuditionError } from "./errors.ts"
9
9
 
10
+ const { debug, span } = Diagnostic.module("Audition")
11
+
10
12
  const TypeId = "~liminal/Audition" as const
11
13
 
12
14
  export interface Audition<ClientSelf, MethodDefinitions extends Record<string, Method.MethodDefinition.Any>, Event>
@@ -23,8 +25,8 @@ export const empty: Audition<never, never, never> = {
23
25
  pipe() {
24
26
  return Pipeable.pipeArguments(this, arguments)
25
27
  },
26
- events: Stream.fail(AuditionError.make()),
27
- f: () => () => AuditionError.make(),
28
+ events: Stream.fail(new AuditionError()),
29
+ f: () => () => new AuditionError().asEffect(),
28
30
  }
29
31
 
30
32
  export const add: {
@@ -32,7 +34,7 @@ export const add: {
32
34
  ClientSelf,
33
35
  ClientId extends string,
34
36
  ClientMethodDefinitions extends Record<string, Method.MethodDefinition.Any>,
35
- ClientEventDefinitions extends FieldsRecord,
37
+ ClientEventDefinitions extends Record<string, S.Struct.Fields>,
36
38
  >(
37
39
  client: Client.Client<ClientSelf, ClientId, ClientMethodDefinitions, ClientEventDefinitions>,
38
40
  ): <AuditionSelf, AuditionMethodDefinitions extends Record<string, Method.MethodDefinition.Any>, AuditionEvent>(
@@ -40,7 +42,7 @@ export const add: {
40
42
  ) => Audition<
41
43
  AuditionSelf | ClientSelf,
42
44
  Method.MethodDefinition.Merge<AuditionMethodDefinitions, ClientMethodDefinitions>,
43
- AuditionEvent | FieldsRecord.TaggedMember.Type<ClientEventDefinitions>
45
+ AuditionEvent | ReturnType<typeof S.TaggedUnion<ClientEventDefinitions>>["Type"]
44
46
  >
45
47
  <
46
48
  AuditionClientSelf,
@@ -49,14 +51,14 @@ export const add: {
49
51
  ClientSelf,
50
52
  ClientId extends string,
51
53
  ClientMethodDefinitions extends Record<string, Method.MethodDefinition.Any>,
52
- ClientEventDefinitions extends FieldsRecord,
54
+ ClientEventDefinitions extends Record<string, S.Struct.Fields>,
53
55
  >(
54
56
  audition: Audition<AuditionClientSelf, AuditionMethodDefinitions, AuditionEvent>,
55
57
  client: Client.Client<ClientSelf, ClientId, ClientMethodDefinitions, ClientEventDefinitions>,
56
58
  ): Audition<
57
59
  AuditionClientSelf | ClientSelf,
58
60
  Method.MethodDefinition.Merge<AuditionMethodDefinitions, ClientMethodDefinitions>,
59
- AuditionEvent | FieldsRecord.TaggedMember.Type<ClientEventDefinitions>
61
+ AuditionEvent | ReturnType<typeof S.TaggedUnion<ClientEventDefinitions>>["Type"]
60
62
  >
61
63
  } = Function.dual(
62
64
  2,
@@ -67,14 +69,14 @@ export const add: {
67
69
  ClientSelf,
68
70
  ClientId extends string,
69
71
  ClientMethodDefinitions extends Record<string, Method.MethodDefinition.Any>,
70
- ClientEventDefinitions extends FieldsRecord,
72
+ ClientEventDefinitions extends Record<string, S.Struct.Fields>,
71
73
  >(
72
74
  audition: Audition<AuditionSelf, AuditionMethodDefinitions, AuditionEvent>,
73
75
  client: Client.Client<ClientSelf, ClientId, ClientMethodDefinitions, ClientEventDefinitions>,
74
76
  ): Audition<
75
77
  AuditionSelf | ClientSelf,
76
78
  Method.MethodDefinition.Merge<AuditionMethodDefinitions, ClientMethodDefinitions>,
77
- AuditionEvent | FieldsRecord.TaggedMember.Type<ClientEventDefinitions>
79
+ AuditionEvent | ReturnType<typeof S.TaggedUnion<ClientEventDefinitions>>["Type"]
78
80
  > => {
79
81
  const f: F<
80
82
  AuditionSelf | ClientSelf,
@@ -82,12 +84,15 @@ export const add: {
82
84
  > = (method) => (payload) =>
83
85
  audition
84
86
  .f(method as never)(payload)
85
- .pipe(Effect.catchTag("AuditionError", () => client.f(method as never)(payload)))
87
+ .pipe(
88
+ Effect.catchTag("AuditionError", () => client.f(method as never)(payload)),
89
+ span("f"),
90
+ )
86
91
 
87
92
  const events = audition.events.pipe(
88
93
  Stream.catchTag("AuditionError", () =>
89
94
  Effect.succeed(client.events).pipe(
90
- Effect.tap(() => Console.log(`Auditioning ${client.key}`)),
95
+ Effect.tap(() => debug("AuditionStaged", { client: client.key })),
91
96
  Stream.unwrap,
92
97
  ),
93
98
  ),
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # liminal
2
2
 
3
+ ## 0.17.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 366c080: Migrate to Effect v4.
8
+
3
9
  ## 0.17.1
4
10
 
5
11
  ### Patch Changes