@@ -5,11 +5,16 @@ import { assertUnreachable } from "@/actor/utils";
55import { importWebSocket } from "@/common/websocket" ;
66import type { ActorQuery } from "@/manager/protocol/query" ;
77import type { ActorDefinitionActions } from "./actor-common" ;
8- import { type ActorConn , ActorConnRaw } from "./actor-conn" ;
8+ import {
9+ type ActorConn ,
10+ ActorConnRaw ,
11+ type ActorManualConn ,
12+ } from "./actor-conn" ;
913import {
1014 type ClientDriver ,
1115 type ClientRaw ,
1216 CREATE_ACTOR_CONN_PROXY ,
17+ CREATE_ACTOR_PROXY ,
1318} from "./client" ;
1419import { logger } from "./log" ;
1520import { rawHttpFetch , rawWebSocket } from "./raw-utils" ;
@@ -98,6 +103,32 @@ export class ActorHandleRaw {
98103 ) as ActorConn < AnyActorDefinition > ;
99104 }
100105
106+ /**
107+ * Creates a new connection to the actor, that should be manually connected.
108+ * This is useful for creating connections that are not immediately connected,
109+ * such as when you want to set up event listeners before connecting.
110+ *
111+ * @param AD - The actor definition for the connection.
112+ * @returns {ActorConn<AD> } A connection to the actor.
113+ */
114+ create ( ) : ActorManualConn < AnyActorDefinition > {
115+ logger ( ) . debug ( "creating a connection from handle" , {
116+ query : this . #actorQuery,
117+ } ) ;
118+
119+ const conn = new ActorConnRaw (
120+ this . #client,
121+ this . #driver,
122+ this . #params,
123+ this . #encodingKind,
124+ this . #actorQuery,
125+ ) ;
126+
127+ return this . #client[ CREATE_ACTOR_PROXY ] (
128+ conn ,
129+ ) as ActorManualConn < AnyActorDefinition > ;
130+ }
131+
101132 /**
102133 * Makes a raw HTTP request to the actor.
103134 *
@@ -188,10 +219,12 @@ export class ActorHandleRaw {
188219 */
189220export type ActorHandle < AD extends AnyActorDefinition > = Omit <
190221 ActorHandleRaw ,
191- "connect"
222+ "connect" | "create"
192223> & {
193224 // Add typed version of ActorConn (instead of using AnyActorDefinition)
194225 connect ( ) : ActorConn < AD > ;
195226 // Resolve method returns the actor ID
196227 resolve ( ) : Promise < string > ;
228+ // Add typed version of create
229+ create ( ) : ActorManualConn < AD > ;
197230} & ActorDefinitionActions < AD > ;
0 commit comments