Skip to content

Commit 7c7e92a

Browse files
committed
chore: move connect* endpoints to be a method on ActorHandle
1 parent c116b67 commit 7c7e92a

File tree

10 files changed

+286
-686
lines changed

10 files changed

+286
-686
lines changed

packages/actor-core/src/client/actor_conn.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as cbor from "cbor-x";
88
import * as errors from "./errors";
99
import { logger } from "./log";
1010
import { type WebSocketMessage as ConnMessage, messageLength } from "./utils";
11-
import { ACTOR_CONNS_SYMBOL, type ClientRaw } from "./client";
11+
import { ACTOR_CONNS_SYMBOL, TRANSPORT_SYMBOL, type ClientRaw } from "./client";
1212
import type { AnyActorDefinition } from "@/actor/definition";
1313
import pRetry from "p-retry";
1414
import { importWebSocket } from "@/common/websocket";
@@ -120,7 +120,6 @@ export class ActorConnRaw {
120120
private readonly endpoint: string,
121121
private readonly params: unknown,
122122
private readonly encodingKind: Encoding,
123-
private readonly transport: Transport,
124123
private readonly actorQuery: ActorQuery,
125124
) {
126125
this.#keepNodeAliveInterval = setInterval(() => 60_000);
@@ -240,12 +239,12 @@ enc
240239
this.#onOpenPromise = Promise.withResolvers();
241240

242241
// Connect transport
243-
if (this.transport === "websocket") {
242+
if (this.client[TRANSPORT_SYMBOL] === "websocket") {
244243
this.#connectWebSocket();
245-
} else if (this.transport === "sse") {
244+
} else if (this.client[TRANSPORT_SYMBOL] === "sse") {
246245
this.#connectSse();
247246
} else {
248-
assertUnreachable(this.transport);
247+
assertUnreachable(this.client[TRANSPORT_SYMBOL]);
249248
}
250249

251250
// Wait for result

packages/actor-core/src/client/actor_handle.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { AnyActorDefinition } from "@/actor/definition";
55
import type { ActorQuery } from "@/manager/protocol/query";
66
import type { ActorDefinitionRpcs } from "./actor_common";
77
import type { RpcRequest, RpcResponse } from "@/actor/protocol/http/rpc";
8+
import { type ActorConn, ActorConnRaw } from "./actor_conn";
9+
import { CREATE_ACTOR_CONN_PROXY, type ClientRaw } from "./client";
810

911
/**
1012
* Provides underlying functions for stateless {@link ActorHandle} for RPC calls.
@@ -13,6 +15,7 @@ import type { RpcRequest, RpcResponse } from "@/actor/protocol/http/rpc";
1315
* @see {@link ActorHandle}
1416
*/
1517
export class ActorHandleRaw {
18+
#client: ClientRaw;
1619
#endpoint: string;
1720
#encodingKind: Encoding;
1821
#actorQuery: ActorQuery;
@@ -27,11 +30,13 @@ export class ActorHandleRaw {
2730
* @protected
2831
*/
2932
public constructor(
33+
client: any,
3034
endpoint: string,
3135
private readonly params: unknown,
3236
encodingKind: Encoding,
3337
actorQuery: ActorQuery,
3438
) {
39+
this.#client = client;
3540
this.#endpoint = endpoint;
3641
this.#encodingKind = encodingKind;
3742
this.#actorQuery = actorQuery;
@@ -82,6 +87,30 @@ export class ActorHandleRaw {
8287

8388
return responseData.o as Response;
8489
}
90+
91+
/**
92+
* Establishes a persistent connection to the actor.
93+
*
94+
* @template AD The actor class that this connection is for.
95+
* @returns {ActorConn<AD>} A connection to the actor.
96+
*/
97+
connect(): ActorConn<AnyActorDefinition> {
98+
logger().debug("establishing connection from handle", {
99+
query: this.#actorQuery,
100+
});
101+
102+
const conn = new ActorConnRaw(
103+
this.#client,
104+
this.#endpoint,
105+
this.params,
106+
this.#encodingKind,
107+
this.#actorQuery,
108+
);
109+
110+
return this.#client[CREATE_ACTOR_CONN_PROXY](
111+
conn,
112+
) as ActorConn<AnyActorDefinition>;
113+
}
85114
}
86115

87116
/**
@@ -100,5 +129,10 @@ export class ActorHandleRaw {
100129
* @template AD The actor class that this handle is for.
101130
* @see {@link ActorHandleRaw}
102131
*/
103-
export type ActorHandle<AD extends AnyActorDefinition> = ActorHandleRaw &
104-
ActorDefinitionRpcs<AD>;
132+
export type ActorHandle<AD extends AnyActorDefinition> = Omit<
133+
ActorHandleRaw,
134+
"connect"
135+
> & {
136+
// Add typed version of ActorConn (instead of using AnyActorDefinition)
137+
connect(): ActorConn<AD>;
138+
} & ActorDefinitionRpcs<AD>;

0 commit comments

Comments
 (0)