@ohos-ports/rivetkit-effect 2.3.17-beta.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.
- package/dist/Action.d.ts +104 -0
- package/dist/Action.d.ts.map +1 -0
- package/dist/Action.js +50 -0
- package/dist/Action.js.map +1 -0
- package/dist/Actor.d.ts +132 -0
- package/dist/Actor.d.ts.map +1 -0
- package/dist/Actor.js +104 -0
- package/dist/Actor.js.map +1 -0
- package/dist/Client.d.ts +31 -0
- package/dist/Client.d.ts.map +1 -0
- package/dist/Client.js +100 -0
- package/dist/Client.js.map +1 -0
- package/dist/Registry.d.ts +134 -0
- package/dist/Registry.d.ts.map +1 -0
- package/dist/Registry.js +178 -0
- package/dist/Registry.js.map +1 -0
- package/dist/RivetError.d.ts +480 -0
- package/dist/RivetError.d.ts.map +1 -0
- package/dist/RivetError.js +985 -0
- package/dist/RivetError.js.map +1 -0
- package/dist/RivetLogger.d.ts +41 -0
- package/dist/RivetLogger.d.ts.map +1 -0
- package/dist/RivetLogger.js +41 -0
- package/dist/RivetLogger.js.map +1 -0
- package/dist/State.d.ts +159 -0
- package/dist/State.d.ts.map +1 -0
- package/dist/State.js +98 -0
- package/dist/State.js.map +1 -0
- package/dist/internal/ActionDispatcher.d.ts +14 -0
- package/dist/internal/ActionDispatcher.d.ts.map +1 -0
- package/dist/internal/ActionDispatcher.js +100 -0
- package/dist/internal/ActionDispatcher.js.map +1 -0
- package/dist/internal/ActionErrorEnvelope.d.ts +11 -0
- package/dist/internal/ActionErrorEnvelope.d.ts.map +1 -0
- package/dist/internal/ActionErrorEnvelope.js +14 -0
- package/dist/internal/ActionErrorEnvelope.js.map +1 -0
- package/dist/internal/ActorInstanceManager.d.ts +28 -0
- package/dist/internal/ActorInstanceManager.d.ts.map +1 -0
- package/dist/internal/ActorInstanceManager.js +51 -0
- package/dist/internal/ActorInstanceManager.js.map +1 -0
- package/dist/internal/ActorStateAdapter.d.ts +18 -0
- package/dist/internal/ActorStateAdapter.d.ts.map +1 -0
- package/dist/internal/ActorStateAdapter.js +24 -0
- package/dist/internal/ActorStateAdapter.js.map +1 -0
- package/dist/internal/StateOptions.d.ts +13 -0
- package/dist/internal/StateOptions.d.ts.map +1 -0
- package/dist/internal/StateOptions.js +2 -0
- package/dist/internal/StateOptions.js.map +1 -0
- package/dist/internal/logging.d.ts +22 -0
- package/dist/internal/logging.d.ts.map +1 -0
- package/dist/internal/logging.js +138 -0
- package/dist/internal/logging.js.map +1 -0
- package/dist/internal/tracing.d.ts +23 -0
- package/dist/internal/tracing.d.ts.map +1 -0
- package/dist/internal/tracing.js +30 -0
- package/dist/internal/tracing.js.map +1 -0
- package/dist/internal/utils.d.ts +7 -0
- package/dist/internal/utils.d.ts.map +1 -0
- package/dist/internal/utils.js +7 -0
- package/dist/internal/utils.js.map +1 -0
- package/dist/mod.d.ts +8 -0
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +8 -0
- package/dist/mod.js.map +1 -0
- package/package.json +56 -0
- package/src/Action.ts +231 -0
- package/src/Actor.test-d.ts +641 -0
- package/src/Actor.test.ts +209 -0
- package/src/Actor.ts +575 -0
- package/src/Client.test.ts +206 -0
- package/src/Client.ts +218 -0
- package/src/Registry.test-d.ts +126 -0
- package/src/Registry.test.ts +400 -0
- package/src/Registry.ts +304 -0
- package/src/RivetError.test.ts +199 -0
- package/src/RivetError.ts +1163 -0
- package/src/RivetLogger.ts +60 -0
- package/src/State.test.ts +340 -0
- package/src/State.ts +420 -0
- package/src/internal/ActionDispatcher.ts +189 -0
- package/src/internal/ActionErrorEnvelope.ts +19 -0
- package/src/internal/ActorInstanceManager.ts +137 -0
- package/src/internal/ActorStateAdapter.ts +82 -0
- package/src/internal/StateOptions.ts +21 -0
- package/src/internal/logging.test.ts +274 -0
- package/src/internal/logging.ts +197 -0
- package/src/internal/tracing.ts +42 -0
- package/src/internal/utils.ts +12 -0
- package/src/mod.ts +7 -0
package/src/State.ts
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `State` is a typed view over an actor's persisted state, plus a
|
|
3
|
+
* subscribable stream of every change.
|
|
4
|
+
*
|
|
5
|
+
* Unlike a `Ref`, `State` has no in-memory cell — the persisted store
|
|
6
|
+
* is the source of truth. Reads decode the live store on demand;
|
|
7
|
+
* writes encode and overwrite it. A `PubSub<A>` backs {@link changes}
|
|
8
|
+
* and is fed externally — the runtime publishes to it from rivetkit's
|
|
9
|
+
* `onStateChange` callback so subscribers see every committed change,
|
|
10
|
+
* including ones initiated outside the SDK.
|
|
11
|
+
*
|
|
12
|
+
* Read and write are Effect-typed so schemas with asynchronous
|
|
13
|
+
* transforms (or service requirements) are supported. `update` and
|
|
14
|
+
* `modify` serialize through a per-`State` semaphore so read/apply/
|
|
15
|
+
* write triples are atomic across fibers; `set` shares the same lock
|
|
16
|
+
* so all writes are linearized.
|
|
17
|
+
*
|
|
18
|
+
* The PubSub uses replay = 1, matching `SubscriptionRef`: a new
|
|
19
|
+
* subscriber immediately sees the most recent value.
|
|
20
|
+
*/
|
|
21
|
+
import {
|
|
22
|
+
Effect,
|
|
23
|
+
Inspectable,
|
|
24
|
+
identity,
|
|
25
|
+
Pipeable,
|
|
26
|
+
Predicate,
|
|
27
|
+
PubSub,
|
|
28
|
+
Semaphore,
|
|
29
|
+
Scope,
|
|
30
|
+
Stream,
|
|
31
|
+
type Types,
|
|
32
|
+
} from "effect";
|
|
33
|
+
import { dual } from "effect/Function";
|
|
34
|
+
|
|
35
|
+
const TypeId = "~@rivetkit/effect/State";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Internal access to `State`'s backing store and publish machinery.
|
|
39
|
+
*
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
export const RuntimeTypeId: unique symbol = Symbol.for(
|
|
43
|
+
"~@rivetkit/effect/State/Runtime",
|
|
44
|
+
) as never;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A view over a persisted state cell with a subscribable change stream.
|
|
48
|
+
*
|
|
49
|
+
* - `A` — the value type
|
|
50
|
+
* - `E` — the read/write closures' failure type (e.g. a schema's
|
|
51
|
+
* `SchemaError` when read/write decode/encode against a schema)
|
|
52
|
+
* - `R` — the read/write closures' service requirements
|
|
53
|
+
*/
|
|
54
|
+
export interface State<A, E = never, R = never>
|
|
55
|
+
extends Variance<A, E, R>,
|
|
56
|
+
Pipeable.Pipeable,
|
|
57
|
+
Inspectable.Inspectable {
|
|
58
|
+
/**
|
|
59
|
+
* Retrieves the persisted value.
|
|
60
|
+
*/
|
|
61
|
+
readonly get: Effect.Effect<A, E, R>;
|
|
62
|
+
/**
|
|
63
|
+
* Retrieves the persisted value and sets a new value atomically.
|
|
64
|
+
*/
|
|
65
|
+
readonly getAndSet: (value: A) => Effect.Effect<A, E, R>;
|
|
66
|
+
/**
|
|
67
|
+
* Retrieves the current value and updates it atomically with the result of
|
|
68
|
+
* applying a function.
|
|
69
|
+
*/
|
|
70
|
+
readonly getAndUpdate: (f: (a: A) => A) => Effect.Effect<A, E, R>;
|
|
71
|
+
/**
|
|
72
|
+
* Retrieves the current value and updates it atomically with the result of
|
|
73
|
+
* applying an effectful function.
|
|
74
|
+
*/
|
|
75
|
+
readonly getAndUpdateEffect: <E2, R2>(
|
|
76
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
77
|
+
) => Effect.Effect<A, E | E2, R | R2>;
|
|
78
|
+
/**
|
|
79
|
+
* Modifies atomically with a function that computes a return value and
|
|
80
|
+
* a new value.
|
|
81
|
+
*/
|
|
82
|
+
readonly modify: <B>(
|
|
83
|
+
f: (a: A) => readonly [B, A],
|
|
84
|
+
) => Effect.Effect<B, E, R>;
|
|
85
|
+
/**
|
|
86
|
+
* Modifies atomically with an effectful function that computes a return value
|
|
87
|
+
* and a new value.
|
|
88
|
+
*/
|
|
89
|
+
readonly modifyEffect: <B, E2, R2>(
|
|
90
|
+
f: (a: A) => Effect.Effect<readonly [B, A], E2, R2>,
|
|
91
|
+
) => Effect.Effect<B, E | E2, R | R2>;
|
|
92
|
+
/**
|
|
93
|
+
* Writes the persisted value.
|
|
94
|
+
*/
|
|
95
|
+
readonly set: (value: A) => Effect.Effect<void, E, R>;
|
|
96
|
+
/**
|
|
97
|
+
* Sets the persisted value and returns the new value.
|
|
98
|
+
*/
|
|
99
|
+
readonly setAndGet: (value: A) => Effect.Effect<A, E, R>;
|
|
100
|
+
/**
|
|
101
|
+
* Updates the persisted value with the result of applying a function.
|
|
102
|
+
*/
|
|
103
|
+
readonly update: (f: (a: A) => A) => Effect.Effect<void, E, R>;
|
|
104
|
+
/**
|
|
105
|
+
* Updates the persisted value with the result of applying an effectful function.
|
|
106
|
+
*/
|
|
107
|
+
readonly updateEffect: <E2, R2>(
|
|
108
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
109
|
+
) => Effect.Effect<void, E | E2, R | R2>;
|
|
110
|
+
/**
|
|
111
|
+
* Updates the persisted value with the result of applying an effectful function
|
|
112
|
+
* and returns the new value.
|
|
113
|
+
*/
|
|
114
|
+
readonly updateAndGet: (f: (a: A) => A) => Effect.Effect<A, E, R>;
|
|
115
|
+
/**
|
|
116
|
+
* Updates the persisted value with the result of applying an effectful function
|
|
117
|
+
* and returns the new value.
|
|
118
|
+
*/
|
|
119
|
+
readonly updateAndGetEffect: <E2, R2>(
|
|
120
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
121
|
+
) => Effect.Effect<A, E | E2, R | R2>;
|
|
122
|
+
/**
|
|
123
|
+
* Creates a stream that emits the current persisted value and all subsequent
|
|
124
|
+
* changes.
|
|
125
|
+
*/
|
|
126
|
+
readonly changes: Stream.Stream<A>;
|
|
127
|
+
/**
|
|
128
|
+
* Internal access to `State`'s backing store and publish machinery.
|
|
129
|
+
*
|
|
130
|
+
* @internal
|
|
131
|
+
*/
|
|
132
|
+
readonly [RuntimeTypeId]: StateRuntime<A, E, R>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Runtime-only hooks for wiring actor state persistence and change
|
|
137
|
+
* notifications.
|
|
138
|
+
*
|
|
139
|
+
* @internal
|
|
140
|
+
*/
|
|
141
|
+
export interface StateRuntime<A, E = never, R = never> {
|
|
142
|
+
readonly publish: (value: A) => Effect.Effect<boolean>;
|
|
143
|
+
readonly publishEffect: <E2, R2>(
|
|
144
|
+
effect: Effect.Effect<A, E2, R2>,
|
|
145
|
+
) => Effect.Effect<boolean, E2, R2>;
|
|
146
|
+
readonly read: () => Effect.Effect<A, E, R>;
|
|
147
|
+
readonly write: (value: A) => Effect.Effect<void, E, R>;
|
|
148
|
+
readonly pubsub: PubSub.PubSub<A>;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const isState = (u: unknown): u is State<unknown, unknown> =>
|
|
152
|
+
Predicate.hasProperty(u, TypeId);
|
|
153
|
+
|
|
154
|
+
export interface Variance<A, E, R> {
|
|
155
|
+
readonly [TypeId]: {
|
|
156
|
+
readonly _A: Types.Invariant<A>;
|
|
157
|
+
readonly _E: Types.Covariant<E>;
|
|
158
|
+
readonly _R: Types.Covariant<R>;
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const Proto = {
|
|
163
|
+
...Pipeable.Prototype,
|
|
164
|
+
...Inspectable.BaseProto,
|
|
165
|
+
[TypeId]: { _A: identity, _E: identity, _R: identity },
|
|
166
|
+
toJSON(this: State<unknown, unknown, unknown>) {
|
|
167
|
+
return { _id: "State" };
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Creates a `State` from `read` and `write` closures over the
|
|
173
|
+
* underlying store. The closures are responsible for any
|
|
174
|
+
* encoding/decoding; `State` itself is schema-agnostic.
|
|
175
|
+
*
|
|
176
|
+
* The current value (per `read()`) is published to the pubsub on
|
|
177
|
+
* construction so any subscription obtained later replays it.
|
|
178
|
+
*
|
|
179
|
+
* The backing PubSub is scoped and shuts down when the current
|
|
180
|
+
* `Scope` closes.
|
|
181
|
+
*/
|
|
182
|
+
export const make = Effect.fnUntraced(function* <A, E, R>(
|
|
183
|
+
read: () => Effect.Effect<A, E, R>,
|
|
184
|
+
write: (value: A) => Effect.Effect<void, E, R>,
|
|
185
|
+
): Effect.fn.Return<State<A, E, R>, E, R | Scope.Scope> {
|
|
186
|
+
const pubsub = yield* PubSub.unbounded<A>({ replay: 1 });
|
|
187
|
+
yield* Effect.addFinalizer(() => PubSub.shutdown(pubsub));
|
|
188
|
+
const initial = yield* read();
|
|
189
|
+
yield* PubSub.publish(pubsub, initial);
|
|
190
|
+
const self = Object.create(Proto);
|
|
191
|
+
const semaphore = yield* Semaphore.make(1);
|
|
192
|
+
self[RuntimeTypeId] = {
|
|
193
|
+
read,
|
|
194
|
+
write,
|
|
195
|
+
pubsub,
|
|
196
|
+
publish: (value: A) => PubSub.publish(pubsub, value),
|
|
197
|
+
publishEffect: <E2, R2>(effect: Effect.Effect<A, E2, R2>) =>
|
|
198
|
+
Semaphore.withPermit(
|
|
199
|
+
semaphore,
|
|
200
|
+
Effect.flatMap(effect, (value) =>
|
|
201
|
+
PubSub.publish(pubsub, value),
|
|
202
|
+
),
|
|
203
|
+
),
|
|
204
|
+
};
|
|
205
|
+
self.get = Effect.suspend(() => self[RuntimeTypeId].read());
|
|
206
|
+
self.set = (value: A) => Semaphore.withPermit(semaphore, write(value));
|
|
207
|
+
self.getAndSet = (value: A) =>
|
|
208
|
+
Semaphore.withPermit(
|
|
209
|
+
semaphore,
|
|
210
|
+
Effect.flatMap(read(), (previous) =>
|
|
211
|
+
Effect.as(write(value), previous),
|
|
212
|
+
),
|
|
213
|
+
);
|
|
214
|
+
self.setAndGet = (value: A) =>
|
|
215
|
+
Semaphore.withPermit(semaphore, Effect.as(write(value), value));
|
|
216
|
+
self.update = (f: (a: A) => A) =>
|
|
217
|
+
Semaphore.withPermit(
|
|
218
|
+
semaphore,
|
|
219
|
+
Effect.flatMap(read(), (a) => write(f(a))),
|
|
220
|
+
);
|
|
221
|
+
self.updateEffect = <E2, R2>(f: (a: A) => Effect.Effect<A, E2, R2>) =>
|
|
222
|
+
Semaphore.withPermit(
|
|
223
|
+
semaphore,
|
|
224
|
+
Effect.flatMap(read(), (a) => Effect.flatMap(f(a), write)),
|
|
225
|
+
);
|
|
226
|
+
self.getAndUpdate = (f: (a: A) => A) =>
|
|
227
|
+
Semaphore.withPermit(
|
|
228
|
+
semaphore,
|
|
229
|
+
Effect.flatMap(read(), (previous) =>
|
|
230
|
+
Effect.as(write(f(previous)), previous),
|
|
231
|
+
),
|
|
232
|
+
);
|
|
233
|
+
self.getAndUpdateEffect = <E2, R2>(f: (a: A) => Effect.Effect<A, E2, R2>) =>
|
|
234
|
+
Semaphore.withPermit(
|
|
235
|
+
semaphore,
|
|
236
|
+
Effect.flatMap(read(), (previous) =>
|
|
237
|
+
Effect.flatMap(f(previous), (next) =>
|
|
238
|
+
Effect.as(write(next), previous),
|
|
239
|
+
),
|
|
240
|
+
),
|
|
241
|
+
);
|
|
242
|
+
self.updateAndGet = (f: (a: A) => A) =>
|
|
243
|
+
Semaphore.withPermit(
|
|
244
|
+
semaphore,
|
|
245
|
+
Effect.flatMap(read(), (a) => {
|
|
246
|
+
const next = f(a);
|
|
247
|
+
return Effect.as(write(next), next);
|
|
248
|
+
}),
|
|
249
|
+
);
|
|
250
|
+
self.updateAndGetEffect = <E2, R2>(f: (a: A) => Effect.Effect<A, E2, R2>) =>
|
|
251
|
+
Semaphore.withPermit(
|
|
252
|
+
semaphore,
|
|
253
|
+
Effect.flatMap(read(), (a) =>
|
|
254
|
+
Effect.flatMap(f(a), (next) => Effect.as(write(next), next)),
|
|
255
|
+
),
|
|
256
|
+
);
|
|
257
|
+
self.modify = <B>(f: (a: A) => readonly [B, A]) =>
|
|
258
|
+
Semaphore.withPermit(
|
|
259
|
+
semaphore,
|
|
260
|
+
Effect.flatMap(read(), (a) => {
|
|
261
|
+
const [b, next] = f(a);
|
|
262
|
+
return Effect.as(write(next), b);
|
|
263
|
+
}),
|
|
264
|
+
);
|
|
265
|
+
self.modifyEffect = <B, E2, R2>(
|
|
266
|
+
f: (a: A) => Effect.Effect<readonly [B, A], E2, R2>,
|
|
267
|
+
) =>
|
|
268
|
+
Semaphore.withPermit(
|
|
269
|
+
semaphore,
|
|
270
|
+
Effect.flatMap(read(), (a) =>
|
|
271
|
+
Effect.flatMap(f(a), ([b, next]) => Effect.as(write(next), b)),
|
|
272
|
+
),
|
|
273
|
+
);
|
|
274
|
+
self.changes = Stream.fromPubSub(pubsub);
|
|
275
|
+
return self;
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
export const get = <A, E, R>(self: State<A, E, R>): Effect.Effect<A, E, R> =>
|
|
279
|
+
self.get;
|
|
280
|
+
|
|
281
|
+
export const getAndSet: {
|
|
282
|
+
<A>(value: A): <E, R>(self: State<A, E, R>) => Effect.Effect<A, E, R>;
|
|
283
|
+
<A, E, R>(self: State<A, E, R>, value: A): Effect.Effect<A, E, R>;
|
|
284
|
+
} = dual(
|
|
285
|
+
2,
|
|
286
|
+
<A, E, R>(self: State<A, E, R>, value: A): Effect.Effect<A, E, R> =>
|
|
287
|
+
self.getAndSet(value),
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
export const getAndUpdate: {
|
|
291
|
+
<A>(f: (a: A) => A): <E, R>(self: State<A, E, R>) => Effect.Effect<A, E, R>;
|
|
292
|
+
<A, E, R>(self: State<A, E, R>, f: (a: A) => A): Effect.Effect<A, E, R>;
|
|
293
|
+
} = dual(
|
|
294
|
+
2,
|
|
295
|
+
<A, E, R>(self: State<A, E, R>, f: (a: A) => A): Effect.Effect<A, E, R> =>
|
|
296
|
+
self.getAndUpdate(f),
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
export const getAndUpdateEffect: {
|
|
300
|
+
<A, E2, R2>(
|
|
301
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
302
|
+
): <E, R>(self: State<A, E, R>) => Effect.Effect<A, E | E2, R | R2>;
|
|
303
|
+
<A, E, R, E2, R2>(
|
|
304
|
+
self: State<A, E, R>,
|
|
305
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
306
|
+
): Effect.Effect<A, E | E2, R | R2>;
|
|
307
|
+
} = dual(
|
|
308
|
+
2,
|
|
309
|
+
<A, E, R, E2, R2>(
|
|
310
|
+
self: State<A, E, R>,
|
|
311
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
312
|
+
): Effect.Effect<A, E | E2, R | R2> => self.getAndUpdateEffect(f),
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
export const modify: {
|
|
316
|
+
<A, B>(
|
|
317
|
+
f: (a: A) => readonly [B, A],
|
|
318
|
+
): <E, R>(self: State<A, E, R>) => Effect.Effect<B, E, R>;
|
|
319
|
+
<A, E, R, B>(
|
|
320
|
+
self: State<A, E, R>,
|
|
321
|
+
f: (a: A) => readonly [B, A],
|
|
322
|
+
): Effect.Effect<B, E, R>;
|
|
323
|
+
} = dual(
|
|
324
|
+
2,
|
|
325
|
+
<A, E, R, B>(
|
|
326
|
+
self: State<A, E, R>,
|
|
327
|
+
f: (a: A) => readonly [B, A],
|
|
328
|
+
): Effect.Effect<B, E, R> => self.modify(f),
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
export const modifyEffect: {
|
|
332
|
+
<A, B, E2, R2>(
|
|
333
|
+
f: (a: A) => Effect.Effect<readonly [B, A], E2, R2>,
|
|
334
|
+
): <E, R>(self: State<A, E, R>) => Effect.Effect<B, E | E2, R | R2>;
|
|
335
|
+
<A, E, R, B, E2, R2>(
|
|
336
|
+
self: State<A, E, R>,
|
|
337
|
+
f: (a: A) => Effect.Effect<readonly [B, A], E2, R2>,
|
|
338
|
+
): Effect.Effect<B, E | E2, R | R2>;
|
|
339
|
+
} = dual(
|
|
340
|
+
2,
|
|
341
|
+
<A, E, R, B, E2, R2>(
|
|
342
|
+
self: State<A, E, R>,
|
|
343
|
+
f: (a: A) => Effect.Effect<readonly [B, A], E2, R2>,
|
|
344
|
+
): Effect.Effect<B, E | E2, R | R2> => self.modifyEffect(f),
|
|
345
|
+
);
|
|
346
|
+
|
|
347
|
+
export const set: {
|
|
348
|
+
<A>(value: A): <E, R>(self: State<A, E, R>) => Effect.Effect<void, E, R>;
|
|
349
|
+
<A, E, R>(self: State<A, E, R>, value: A): Effect.Effect<void, E, R>;
|
|
350
|
+
} = dual(
|
|
351
|
+
2,
|
|
352
|
+
<A, E, R>(self: State<A, E, R>, value: A): Effect.Effect<void, E, R> =>
|
|
353
|
+
self.set(value),
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
export const setAndGet: {
|
|
357
|
+
<A>(value: A): <E, R>(self: State<A, E, R>) => Effect.Effect<A, E, R>;
|
|
358
|
+
<A, E, R>(self: State<A, E, R>, value: A): Effect.Effect<A, E, R>;
|
|
359
|
+
} = dual(
|
|
360
|
+
2,
|
|
361
|
+
<A, E, R>(self: State<A, E, R>, value: A): Effect.Effect<A, E, R> =>
|
|
362
|
+
self.setAndGet(value),
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
export const update: {
|
|
366
|
+
<A>(
|
|
367
|
+
f: (a: A) => A,
|
|
368
|
+
): <E, R>(self: State<A, E, R>) => Effect.Effect<void, E, R>;
|
|
369
|
+
<A, E, R>(self: State<A, E, R>, f: (a: A) => A): Effect.Effect<void, E, R>;
|
|
370
|
+
} = dual(
|
|
371
|
+
2,
|
|
372
|
+
<A, E, R>(
|
|
373
|
+
self: State<A, E, R>,
|
|
374
|
+
f: (a: A) => A,
|
|
375
|
+
): Effect.Effect<void, E, R> => self.update(f),
|
|
376
|
+
);
|
|
377
|
+
|
|
378
|
+
export const updateEffect: {
|
|
379
|
+
<A, E2, R2>(
|
|
380
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
381
|
+
): <E, R>(self: State<A, E, R>) => Effect.Effect<void, E | E2, R | R2>;
|
|
382
|
+
<A, E, R, E2, R2>(
|
|
383
|
+
self: State<A, E, R>,
|
|
384
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
385
|
+
): Effect.Effect<void, E | E2, R | R2>;
|
|
386
|
+
} = dual(
|
|
387
|
+
2,
|
|
388
|
+
<A, E, R, E2, R2>(
|
|
389
|
+
self: State<A, E, R>,
|
|
390
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
391
|
+
): Effect.Effect<void, E | E2, R | R2> => self.updateEffect(f),
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
export const updateAndGet: {
|
|
395
|
+
<A>(f: (a: A) => A): <E, R>(self: State<A, E, R>) => Effect.Effect<A, E, R>;
|
|
396
|
+
<A, E, R>(self: State<A, E, R>, f: (a: A) => A): Effect.Effect<A, E, R>;
|
|
397
|
+
} = dual(
|
|
398
|
+
2,
|
|
399
|
+
<A, E, R>(self: State<A, E, R>, f: (a: A) => A): Effect.Effect<A, E, R> =>
|
|
400
|
+
self.updateAndGet(f),
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
export const updateAndGetEffect: {
|
|
404
|
+
<A, E2, R2>(
|
|
405
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
406
|
+
): <E, R>(self: State<A, E, R>) => Effect.Effect<A, E | E2, R | R2>;
|
|
407
|
+
<A, E, R, E2, R2>(
|
|
408
|
+
self: State<A, E, R>,
|
|
409
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
410
|
+
): Effect.Effect<A, E | E2, R | R2>;
|
|
411
|
+
} = dual(
|
|
412
|
+
2,
|
|
413
|
+
<A, E, R, E2, R2>(
|
|
414
|
+
self: State<A, E, R>,
|
|
415
|
+
f: (a: A) => Effect.Effect<A, E2, R2>,
|
|
416
|
+
): Effect.Effect<A, E | E2, R | R2> => self.updateAndGetEffect(f),
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
export const changes = <A, E, R>(self: State<A, E, R>): Stream.Stream<A> =>
|
|
420
|
+
self.changes;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Cause,
|
|
3
|
+
Effect,
|
|
4
|
+
Exit,
|
|
5
|
+
type Fiber,
|
|
6
|
+
Option,
|
|
7
|
+
Record,
|
|
8
|
+
Schema,
|
|
9
|
+
Tracer,
|
|
10
|
+
} from "effect";
|
|
11
|
+
import * as Rivetkit from "@ohos-ports/rivetkit";
|
|
12
|
+
import type * as Action from "../Action.ts";
|
|
13
|
+
import type { ActionHandlersFrom, ActionRequest, Actor } from "../Actor.ts";
|
|
14
|
+
import type * as Client from "../Client.ts";
|
|
15
|
+
import * as ActionErrorEnvelope from "./ActionErrorEnvelope.ts";
|
|
16
|
+
import { makeActorLogAnnotations } from "./logging.ts";
|
|
17
|
+
import { readTraceMeta, rpcSystem } from "./tracing.ts";
|
|
18
|
+
import { hasStringProperty } from "./utils.ts";
|
|
19
|
+
|
|
20
|
+
export type Instance<ActionHandlers> = {
|
|
21
|
+
readonly actionHandlers: ActionHandlers;
|
|
22
|
+
readonly runFork: <A, E>(
|
|
23
|
+
effect: Effect.Effect<A, E, any>,
|
|
24
|
+
options?: Effect.RunOptions,
|
|
25
|
+
) => Fiber.Fiber<A, E>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const make = <
|
|
29
|
+
Name extends string,
|
|
30
|
+
Actions extends Action.AnyWithProps,
|
|
31
|
+
ActionHandlers extends ActionHandlersFrom<Actions>,
|
|
32
|
+
ActorDefinition extends Rivetkit.AnyActorDefinition,
|
|
33
|
+
>({
|
|
34
|
+
actor,
|
|
35
|
+
getInstance,
|
|
36
|
+
}: {
|
|
37
|
+
readonly actor: Actor<Name, Actions>;
|
|
38
|
+
readonly getInstance: (
|
|
39
|
+
actorId: string,
|
|
40
|
+
) => Instance<ActionHandlers> | undefined;
|
|
41
|
+
}) =>
|
|
42
|
+
Record.fromIterableWith(actor.actions, (action) => {
|
|
43
|
+
const decodePayload = Schema.decodeUnknownEffect(
|
|
44
|
+
Schema.toCodecJson(action.payloadSchema),
|
|
45
|
+
);
|
|
46
|
+
const encodeSuccess = Schema.encodeEffect(
|
|
47
|
+
Schema.toCodecJson(action.successSchema),
|
|
48
|
+
);
|
|
49
|
+
const encodeError = Schema.encodeEffect(
|
|
50
|
+
Schema.toCodecJson(action.errorSchema),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return [
|
|
54
|
+
action._tag,
|
|
55
|
+
async (
|
|
56
|
+
c: Rivetkit.ActionContextOf<ActorDefinition>,
|
|
57
|
+
payload: Action.Payload<typeof action>,
|
|
58
|
+
meta?: Client.ActionMeta, // TODO: Find better type
|
|
59
|
+
) => {
|
|
60
|
+
// Always wrap in a server-side span so the handler has a
|
|
61
|
+
// live `currentSpan` even when the caller didn't ship trace
|
|
62
|
+
// context (e.g., a non-Effect-SDK client). When trace context
|
|
63
|
+
// is present, reattach it as the parent so the server span
|
|
64
|
+
// joins the caller's trace.
|
|
65
|
+
const rpcMethod = `${actor.name}/${action._tag}`;
|
|
66
|
+
const traceMeta = readTraceMeta(meta);
|
|
67
|
+
|
|
68
|
+
const instance = getInstance(c.actorId);
|
|
69
|
+
if (!instance) {
|
|
70
|
+
if (c.abortSignal.aborted) throw makeActorAbortedError();
|
|
71
|
+
throw new Error("actor instance missing");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const actionEffect = Effect.gen(function* () {
|
|
75
|
+
// The handler map is keyed by the same action
|
|
76
|
+
// definitions being registered here, but
|
|
77
|
+
// TypeScript loses that relationship once the
|
|
78
|
+
// actions are widened into the RivetKit actions
|
|
79
|
+
// record.
|
|
80
|
+
const actionHandler = instance.actionHandlers[
|
|
81
|
+
action._tag as keyof ActionHandlers
|
|
82
|
+
] as (
|
|
83
|
+
envelope: ActionRequest<typeof action>,
|
|
84
|
+
) => Action.ResultFrom<typeof action, any>;
|
|
85
|
+
// Raw RivetKit clients call no-argument actions with an
|
|
86
|
+
// absent first argument. The Effect JSON Void codec expects
|
|
87
|
+
// null, so adapt only actions that declared no payload.
|
|
88
|
+
const payloadForDecode =
|
|
89
|
+
!action.hasPayload && payload === undefined
|
|
90
|
+
? null
|
|
91
|
+
: payload;
|
|
92
|
+
const decodedPayload = yield* decodePayload(
|
|
93
|
+
payloadForDecode,
|
|
94
|
+
).pipe(
|
|
95
|
+
Effect.mapError(
|
|
96
|
+
() =>
|
|
97
|
+
new Rivetkit.RivetError(
|
|
98
|
+
"request",
|
|
99
|
+
"invalid",
|
|
100
|
+
`Invalid payload for action ${actor.name}/${action._tag}`,
|
|
101
|
+
),
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
// The payload was decoded with this action's schema,
|
|
105
|
+
// so this is the runtime boundary that restores the
|
|
106
|
+
// typed envelope expected by the user handler.
|
|
107
|
+
const actionRequest = {
|
|
108
|
+
_tag: action._tag,
|
|
109
|
+
action,
|
|
110
|
+
payload: decodedPayload,
|
|
111
|
+
} as ActionRequest<typeof action>;
|
|
112
|
+
|
|
113
|
+
const resultExit = yield* Effect.exit(
|
|
114
|
+
actionHandler(actionRequest),
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
if (Exit.isSuccess(resultExit)) {
|
|
118
|
+
return yield* encodeSuccess(resultExit.value).pipe(
|
|
119
|
+
Effect.orDie,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const expectedError = Exit.findErrorOption(resultExit);
|
|
124
|
+
|
|
125
|
+
if (Option.isSome(expectedError)) {
|
|
126
|
+
const encodedError = yield* encodeError(
|
|
127
|
+
expectedError.value,
|
|
128
|
+
).pipe(Effect.orDie);
|
|
129
|
+
|
|
130
|
+
return yield* Effect.fail(
|
|
131
|
+
new Rivetkit.UserError(
|
|
132
|
+
hasStringProperty("message")(encodedError)
|
|
133
|
+
? encodedError.message
|
|
134
|
+
: `${action._tag} failed`,
|
|
135
|
+
{
|
|
136
|
+
code: hasStringProperty("_tag")(
|
|
137
|
+
encodedError,
|
|
138
|
+
)
|
|
139
|
+
? encodedError._tag
|
|
140
|
+
: undefined,
|
|
141
|
+
metadata:
|
|
142
|
+
ActionErrorEnvelope.make(encodedError),
|
|
143
|
+
},
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return yield* Effect.failCause(resultExit.cause);
|
|
149
|
+
}).pipe(
|
|
150
|
+
Effect.withSpan(rpcMethod, {
|
|
151
|
+
parent: traceMeta
|
|
152
|
+
? Tracer.externalSpan(traceMeta)
|
|
153
|
+
: undefined,
|
|
154
|
+
kind: "server",
|
|
155
|
+
attributes: {
|
|
156
|
+
"rpc.system.name": rpcSystem,
|
|
157
|
+
"rpc.method": rpcMethod,
|
|
158
|
+
},
|
|
159
|
+
}),
|
|
160
|
+
Effect.annotateLogs(makeActorLogAnnotations(c)),
|
|
161
|
+
);
|
|
162
|
+
const fiber = instance.runFork(actionEffect, {
|
|
163
|
+
signal: c.abortSignal,
|
|
164
|
+
});
|
|
165
|
+
const exit = await new Promise<Exit.Exit<unknown, unknown>>(
|
|
166
|
+
(resolve) => fiber.addObserver(resolve),
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
if (Exit.isSuccess(exit)) return exit.value;
|
|
170
|
+
// Action fibers can be interrupted by a caller abort signal
|
|
171
|
+
// or by the actor instance scope closing during sleep, destroy,
|
|
172
|
+
// or shutdown. Surface those lifecycle exits as RivetKit's
|
|
173
|
+
// structured action-aborted error instead of an internal error.
|
|
174
|
+
if (Cause.hasInterruptsOnly(exit.cause)) {
|
|
175
|
+
throw makeActorAbortedError();
|
|
176
|
+
}
|
|
177
|
+
const expectedError = Exit.findErrorOption(exit);
|
|
178
|
+
if (Option.isSome(expectedError)) {
|
|
179
|
+
throw expectedError.value;
|
|
180
|
+
}
|
|
181
|
+
throw Cause.squash(exit.cause);
|
|
182
|
+
},
|
|
183
|
+
];
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const makeActorAbortedError = () =>
|
|
187
|
+
new Rivetkit.RivetError("actor", "aborted", "Actor aborted", {
|
|
188
|
+
public: true,
|
|
189
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
export const tag = "EffectActionError" as const;
|
|
4
|
+
|
|
5
|
+
export const schemaVersion = 1 as const;
|
|
6
|
+
|
|
7
|
+
export const ActionErrorEnvelope = Schema.Struct({
|
|
8
|
+
_tag: Schema.tag(tag),
|
|
9
|
+
version: Schema.Literal(schemaVersion),
|
|
10
|
+
error: Schema.Unknown,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export type ActionErrorEnvelope = typeof ActionErrorEnvelope.Type;
|
|
14
|
+
|
|
15
|
+
export const make = (error: unknown): ActionErrorEnvelope => ({
|
|
16
|
+
_tag: tag,
|
|
17
|
+
version: schemaVersion,
|
|
18
|
+
error,
|
|
19
|
+
});
|