@@ -5,11 +5,16 @@ import { assertUnreachable } from "@/actor/utils";
5
5
import { importWebSocket } from "@/common/websocket" ;
6
6
import type { ActorQuery } from "@/manager/protocol/query" ;
7
7
import 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" ;
9
13
import {
10
14
type ClientDriver ,
11
15
type ClientRaw ,
12
16
CREATE_ACTOR_CONN_PROXY ,
17
+ CREATE_ACTOR_PROXY ,
13
18
} from "./client" ;
14
19
import { logger } from "./log" ;
15
20
import { rawHttpFetch , rawWebSocket } from "./raw-utils" ;
@@ -98,6 +103,32 @@ export class ActorHandleRaw {
98
103
) as ActorConn < AnyActorDefinition > ;
99
104
}
100
105
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
+
101
132
/**
102
133
* Makes a raw HTTP request to the actor.
103
134
*
@@ -188,10 +219,12 @@ export class ActorHandleRaw {
188
219
*/
189
220
export type ActorHandle < AD extends AnyActorDefinition > = Omit <
190
221
ActorHandleRaw ,
191
- "connect"
222
+ "connect" | "create"
192
223
> & {
193
224
// Add typed version of ActorConn (instead of using AnyActorDefinition)
194
225
connect ( ) : ActorConn < AD > ;
195
226
// Resolve method returns the actor ID
196
227
resolve ( ) : Promise < string > ;
228
+ // Add typed version of create
229
+ create ( ) : ActorManualConn < AD > ;
197
230
} & ActorDefinitionActions < AD > ;
0 commit comments