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.
- package/Accumulator.ts +16 -10
- package/Actor.ts +35 -29
- package/Audition.ts +17 -12
- package/CHANGELOG.md +6 -0
- package/Client.ts +260 -295
- package/ClientHandle.ts +17 -6
- package/F.ts +3 -9
- package/LICENSE +202 -0
- package/Method.ts +11 -13
- package/Protocol.ts +104 -126
- package/Send.ts +9 -5
- package/_util/Diagnostic.ts +16 -0
- package/_util/Mutex.ts +5 -5
- package/dist/Accumulator.d.ts +2 -2
- package/dist/Accumulator.js +12 -8
- package/dist/Accumulator.js.map +1 -1
- package/dist/Actor.d.ts +9 -10
- package/dist/Actor.js +6 -15
- package/dist/Actor.js.map +1 -1
- package/dist/Audition.d.ts +3 -4
- package/dist/Audition.js +7 -5
- package/dist/Audition.js.map +1 -1
- package/dist/Client.d.ts +20 -39
- package/dist/Client.js +119 -139
- package/dist/Client.js.map +1 -1
- package/dist/ClientHandle.d.ts +5 -6
- package/dist/ClientHandle.js +1 -1
- package/dist/ClientHandle.js.map +1 -1
- package/dist/F.d.ts +3 -12
- package/dist/F.js +1 -3
- package/dist/F.js.map +1 -1
- package/dist/Method.d.ts +11 -12
- package/dist/Method.js.map +1 -1
- package/dist/Protocol.d.ts +46 -89
- package/dist/Protocol.js +35 -11
- package/dist/Protocol.js.map +1 -1
- package/dist/Send.d.ts +2 -3
- package/dist/Send.js +1 -1
- package/dist/Send.js.map +1 -1
- package/dist/_util/Diagnostic.d.ts +5 -0
- package/dist/_util/Diagnostic.js +10 -0
- package/dist/_util/Diagnostic.js.map +1 -0
- package/dist/_util/Mutex.d.ts +2 -2
- package/dist/_util/Mutex.js +4 -4
- package/dist/_util/Mutex.js.map +1 -1
- package/dist/errors.d.ts +15 -12
- package/dist/errors.js +6 -4
- package/dist/errors.js.map +1 -1
- package/dist/experimental/BranchLive.d.ts +1 -1
- package/dist/experimental/BranchLive.js +1 -1
- package/dist/experimental/BranchLive.js.map +1 -1
- package/dist/experimental/L/append.d.ts +1 -1
- package/dist/experimental/L/append.js +2 -2
- package/dist/experimental/L/append.js.map +1 -1
- package/dist/experimental/L/assistant.d.ts +2 -2
- package/dist/experimental/L/assistant.js +1 -1
- package/dist/experimental/L/assistant.js.map +1 -1
- package/dist/experimental/L/assistantSchema.d.ts +2 -2
- package/dist/experimental/L/assistantSchema.js +1 -1
- package/dist/experimental/L/assistantSchema.js.map +1 -1
- package/dist/experimental/L/assistantStream.d.ts +2 -2
- package/dist/experimental/L/assistantStream.js +1 -1
- package/dist/experimental/L/assistantStream.js.map +1 -1
- package/dist/experimental/L/branch.d.ts +1 -1
- package/dist/experimental/L/clear.d.ts +1 -1
- package/dist/experimental/L/clear.js +1 -1
- package/dist/experimental/L/clear.js.map +1 -1
- package/dist/experimental/L/history.d.ts +2 -2
- package/dist/experimental/L/history.js +2 -2
- package/dist/experimental/L/history.js.map +1 -1
- package/dist/experimental/L/init.d.ts +1 -1
- package/dist/experimental/L/init.js +1 -1
- package/dist/experimental/L/init.js.map +1 -1
- package/dist/experimental/L/matrix.d.ts +2 -2
- package/dist/experimental/L/system.d.ts +1 -1
- package/dist/experimental/L/system.js +1 -1
- package/dist/experimental/L/system.js.map +1 -1
- package/dist/experimental/L/user.d.ts +1 -1
- package/dist/experimental/L/user.js +1 -1
- package/dist/experimental/L/user.js.map +1 -1
- package/dist/experimental/Loader.d.ts +3 -3
- package/dist/experimental/Loader.js +2 -2
- package/dist/experimental/Loader.js.map +1 -1
- package/dist/experimental/TaggedTemplateFunction.d.ts +2 -2
- package/dist/experimental/Template.d.ts +8 -12
- package/dist/experimental/Template.js +53 -51
- package/dist/experimental/Template.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/errors.ts +12 -4
- package/experimental/BranchLive.ts +1 -1
- package/experimental/L/append.ts +2 -2
- package/experimental/L/assistant.ts +1 -1
- package/experimental/L/assistantSchema.ts +3 -3
- package/experimental/L/assistantStream.ts +1 -1
- package/experimental/L/clear.ts +1 -1
- package/experimental/L/history.ts +2 -2
- package/experimental/L/init.ts +1 -1
- package/experimental/L/matrix.ts +3 -3
- package/experimental/L/system.ts +1 -1
- package/experimental/L/user.ts +1 -1
- package/experimental/Loader.ts +3 -3
- package/experimental/TaggedTemplateFunction.ts +2 -2
- package/experimental/Template.ts +62 -70
- package/package.json +2 -6
- package/tsconfig.json +1 -1
- package/_types.ts +0 -27
- package/dist/_types.d.ts +0 -22
- package/dist/_types.js +0 -2
- 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.
|
|
24
|
-
new (_: never): Context.
|
|
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.
|
|
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*
|
|
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*
|
|
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*
|
|
98
|
+
yield* PubSub.publish(pubsub, initial_)
|
|
93
99
|
return { ref, pubsub }
|
|
94
|
-
}).pipe(Layer.
|
|
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<
|
|
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
|
|
34
|
+
EventDefinitions extends Record<string, S.Struct.Fields>,
|
|
27
35
|
> {
|
|
28
|
-
readonly name: S.
|
|
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
|
|
44
|
-
> extends Context.
|
|
45
|
-
new (_: never): Context.
|
|
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.
|
|
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
|
|
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
|
|
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.
|
|
94
|
+
const tag = Context.Service<ActorSelf, Service<ActorSelf, NameA, AttachmentFields, EventDefinitions>>()(id)
|
|
87
95
|
|
|
88
|
-
const sendAll: Send<ActorSelf, EventDefinitions> =
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
121
|
+
disconnectAll,
|
|
116
122
|
handler,
|
|
117
123
|
})
|
|
118
124
|
}
|
package/Audition.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { Pipeable, Stream, Effect, Function
|
|
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
|
|
27
|
-
f: () => () => AuditionError.
|
|
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
|
|
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 |
|
|
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
|
|
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 |
|
|
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
|
|
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 |
|
|
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(
|
|
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(() =>
|
|
95
|
+
Effect.tap(() => debug("AuditionStaged", { client: client.key })),
|
|
91
96
|
Stream.unwrap,
|
|
92
97
|
),
|
|
93
98
|
),
|