@@ -4,7 +4,7 @@ import type { ActorQuery } from "@/manager/protocol/query";
44import * as errors from "./errors" ;
55import { ActorConn , ActorConnRaw , CONNECT_SYMBOL } from "./actor_conn" ;
66import { ActorHandle , ActorHandleRaw } from "./actor_handle" ;
7- import { ActorRPCFunction } from "./actor_common" ;
7+ import { ActorRPCFunction , resolveActorId } from "./actor_common" ;
88import { logger } from "./log" ;
99import type { ActorCoreApp } from "@/mod" ;
1010import type { AnyActorDefinition } from "@/actor/definition" ;
@@ -55,14 +55,17 @@ export interface ActorAccessor<AD extends AnyActorDefinition> {
5555
5656 /**
5757 * Creates a new actor with the name automatically injected from the property accessor,
58- * and returns a stateless handle to it.
58+ * and returns a stateless handle to it with the actor ID resolved .
5959 *
6060 * @template AD The actor class that this handle is for.
6161 * @param {string | string[] } key - The key to identify the actor. Can be a single string or an array of strings.
6262 * @param {CreateOptions } [opts] - Options for creating the actor (excluding name and key).
63- * @returns {ActorHandle<AD> } - A handle to the actor.
63+ * @returns {Promise< ActorHandle<AD>> } - A promise that resolves to a handle to the actor.
6464 */
65- create ( key : string | string [ ] , opts ?: CreateOptions ) : ActorHandle < AD > ;
65+ create (
66+ key : string | string [ ] ,
67+ opts ?: CreateOptions ,
68+ ) : Promise < ActorHandle < AD > > ;
6669}
6770
6871/**
@@ -286,18 +289,19 @@ export class ClientRaw {
286289
287290 /**
288291 * Creates a new actor with the provided key and returns a stateless handle to it.
292+ * Resolves the actor ID and returns a handle with getForId query.
289293 *
290294 * @template AD The actor class that this handle is for.
291295 * @param {string } name - The name of the actor.
292296 * @param {string | string[] } key - The key to identify the actor. Can be a single string or an array of strings.
293297 * @param {CreateOptions } [opts] - Options for creating the actor (excluding name and key).
294- * @returns {ActorHandle<AD> } - A handle to the actor.
298+ * @returns {Promise< ActorHandle<AD>> } - A promise that resolves to a handle to the actor.
295299 */
296- create < AD extends AnyActorDefinition > (
300+ async create < AD extends AnyActorDefinition > (
297301 name : string ,
298302 key : string | string [ ] ,
299303 opts : CreateOptions = { } ,
300- ) : ActorHandle < AD > {
304+ ) : Promise < ActorHandle < AD > > {
301305 // Convert string to array of strings
302306 const keyArray : string [ ] = typeof key === "string" ? [ key ] : key ;
303307
@@ -316,17 +320,36 @@ export class ClientRaw {
316320 create,
317321 } ) ;
318322
319- const actorQuery = {
323+ // Create the actor
324+ const createQuery = {
320325 create,
321- } ;
326+ } satisfies ActorQuery ;
327+ const actorId = await resolveActorId (
328+ this . #managerEndpoint,
329+ createQuery ,
330+ this . #encodingKind,
331+ ) ;
332+ logger ( ) . debug ( "created actor with ID" , {
333+ name,
334+ key : keyArray ,
335+ actorId,
336+ } ) ;
322337
323- const managerEndpoint = this . #managerEndpoint;
338+ // Create handle with actor ID
339+ const getForIdQuery = {
340+ getForId : {
341+ actorId,
342+ } ,
343+ } satisfies ActorQuery ;
324344 const handle = this . #createHandle(
325- managerEndpoint ,
345+ this . # managerEndpoint,
326346 opts ?. params ,
327- actorQuery ,
347+ getForIdQuery ,
328348 ) ;
329- return createActorProxy ( handle ) as ActorHandle < AD > ;
349+
350+ const proxy = createActorProxy ( handle ) as ActorHandle < AD > ;
351+
352+ return proxy ;
330353 }
331354
332355 #createHandle(
@@ -454,11 +477,11 @@ export function createClient<A extends ActorCoreApp<any>>(
454477 opts ,
455478 ) ;
456479 } ,
457- create : (
480+ create : async (
458481 key : string | string [ ] ,
459482 opts : CreateOptions = { } ,
460- ) : ActorHandle < ExtractActorsFromApp < A > [ typeof prop ] > => {
461- return target . create < ExtractActorsFromApp < A > [ typeof prop ] > (
483+ ) : Promise < ActorHandle < ExtractActorsFromApp < A > [ typeof prop ] > > => {
484+ return await target . create < ExtractActorsFromApp < A > [ typeof prop ] > (
462485 prop ,
463486 key ,
464487 opts ,
@@ -499,6 +522,9 @@ function createActorProxy<AD extends AnyActorDefinition>(
499522
500523 // Create RPC function that preserves 'this' context
501524 if ( typeof prop === "string" ) {
525+ // If JS is attempting to calling this as a promise, ignore it
526+ if ( prop === "then" ) return undefined ;
527+
502528 let method = methodCache . get ( prop ) ;
503529 if ( ! method ) {
504530 method = ( ...args : unknown [ ] ) => target . action ( prop , ...args ) ;
0 commit comments