Skip to content

Commit 0ac994a

Browse files
committed
feat(effect): add type guards for Action, Message, and Actor
1 parent f74e13c commit 0ac994a

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

rivetkit-typescript/packages/effect/src/Action.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { type Pipeable, pipeArguments } from "effect/Pipeable";
2+
import * as Predicate from "effect/Predicate";
23
import * as Schema from "effect/Schema";
34

45
const TypeId = "~@rivetkit/effect/Action";
56

7+
export const isAction = (u: unknown): u is Action<any, any, any, any> =>
8+
Predicate.hasProperty(u, TypeId);
9+
610
/**
711
* A Rivet Actor action: a synchronous request-response call dispatched
812
* on the actor's main loop.

rivetkit-typescript/packages/effect/src/Actor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as Context from "effect/Context";
22
import type * as Effect from "effect/Effect";
33
import * as Layer from "effect/Layer";
44
import { type Pipeable, pipeArguments } from "effect/Pipeable";
5+
import * as Predicate from "effect/Predicate";
56
import type * as PubSub from "effect/PubSub";
67
import type * as Queue from "effect/Queue";
78
import * as Schema from "effect/Schema";
@@ -13,6 +14,9 @@ import type * as Message from "./Message";
1314

1415
const TypeId = "~@rivetkit/effect/Actor";
1516

17+
export const isActor = (u: unknown): u is Actor<any, any, any, any, any> =>
18+
Predicate.hasProperty(u, TypeId);
19+
1620
/**
1721
* Schemas keyed by the event names an actor can publish.
1822
*/
@@ -441,9 +445,6 @@ export type ServerServices<A> = A extends Actor<
441445
| EventEncodeServices<_Events>
442446
: never;
443447

444-
export const isActor = (u: unknown): u is Any =>
445-
typeof u === "object" && u !== null && TypeId in u;
446-
447448
const identity = <A>(value: A): A => value;
448449

449450
const Proto = {

rivetkit-typescript/packages/effect/src/Message.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { type Pipeable, pipeArguments } from "effect/Pipeable";
2+
import * as Predicate from "effect/Predicate";
23
import * as Schema from "effect/Schema";
34

45
const TypeId = "~@rivetkit/effect/Message";
56

67
const EnvelopeTypeId = "~@rivetkit/effect/Message/Envelope";
78

9+
export const isMessage = (u: unknown): u is Message<any, any, any> =>
10+
Predicate.hasProperty(u, TypeId);
11+
812
/**
913
* A Rivet Actor message: a durable, queued operation that the actor
1014
* processes asynchronously on its main loop.

0 commit comments

Comments
 (0)