All files ref.ts

73.17% Statements 150/205
100% Branches 0/0
0% Functions 0/10
73.17% Lines 150/205

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 2061x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x  
import {
  ActrRef as NativeActrRef,
  ActrId,
  ActrType,
  PayloadType,
} from '../index';
import { callNative } from './error';
 
/**
 * ActrRef – reference to a running actor.
 *
 * Provides methods to interact with the actor: RPC calls, messaging, discovery, etc.
 */
export class ActrRef {
  constructor(private native: NativeActrRef) {}
 
  /**
   * Get the actor ID.
   *
   * @returns The actor's unique identifier
   */
  actorId(): ActrId {
    return this.native.actorId();
  }
 
  /**
   * Discover actors of the given type.
   *
   * @param targetType - Target actor type (manufacturer + name)
   * @param count - Number of actors to discover
   * @returns List of actor IDs
   *
   * @example
   * ```typescript
   * const servers = await actorRef.discover(
   *   { manufacturer: 'acme', name: 'EchoTwiceService' },
   *   1
   * );
   * ```
   */
  async discover(targetType: ActrType, count: number): Promise<ActrId[]> {
    return await callNative(() => this.native.discover(targetType, count));
  }
 
  /**
   * Call remote actor (RPC).
   *
   * @param target - Target actor ID
   * @param routeKey - Route key (e.g. 'service.Method')
   * @param payloadType - Payload type
   * @param requestPayload - Request payload (protobuf-encoded)
   * @param timeoutMs - Timeout in ms, default 30000
   * @returns Response payload
   *
   * @example
   * ```typescript
   * const request = Buffer.from('Hello');
   * const response = await actorRef.call(
   *   serverId,
   *   'echo_twice.EchoTwiceService.EchoTwice',
   *   PayloadType.RpcReliable,
   *   request,
   *   5000
   * );
   * ```
   */
  async call(
    target: ActrId,
    routeKey: string,
    payloadType: PayloadType,
    requestPayload: Buffer,
    timeoutMs: number = 30000,
  ): Promise<Buffer> {
    return await callNative(() =>
      this.native.call(
        target,
        routeKey,
        payloadType,
        requestPayload,
        timeoutMs,
      ),
    );
  }
 
  /**
   * Type-safe RPC call using Protobuf messages.
   *
   * @param routeKey - Route key
   * @param request - Request object (must have encode() method)
   * @param responseType - Response type (must have decode() method)
   * @param payloadType - Payload type, default RpcReliable
   * @param timeoutMs - Timeout in ms, default 30000
   * @returns Decoded response object
   *
   * @example
   * ```typescript
   * import { EchoTwiceRequest, EchoTwiceResponse } from './proto/echo-twice';
   *
   * const request = new EchoTwiceRequest({ message: 'Hello' });
   * const response = await actorRef.callTyped(
   *   serverId,
   *   'echo_twice.EchoTwiceService.EchoTwice',
   *   request,
   *   EchoTwiceResponse,
   *   PayloadType.RpcReliable,
   *   5000
   * );
   * console.log(response.message);
   * ```
   */
  async callTyped<Req, Res>(
    target: ActrId,
    routeKey: string,
    request: Req & EncodableRequest,
    responseType: { decode: (buf: Buffer) => Res },
    payloadType: PayloadType = PayloadType.RpcReliable,
    timeoutMs: number = 30000,
  ): Promise<Res> {
    const requestBuf = request.encode();
    const responseBuf = await this.call(
      target,
      routeKey,
      payloadType,
      requestBuf,
      timeoutMs,
    );
    return responseType.decode(responseBuf);
  }
 
  /**
   * Send one-way message (fire-and-forget).
   *
   * @param target - Target actor ID
   * @param routeKey - Route key
   * @param payloadType - Payload type
   * @param messagePayload - Message payload
   *
   * @example
   * ```typescript
   * await actorRef.tell(
   *   serverId,
   *   'notification.Service/Notify',
   *   PayloadType.RpcSignal,
   *   Buffer.from('Event occurred')
   * );
   * ```
   */
  async tell(
    target: ActrId,
    routeKey: string,
    payloadType: PayloadType,
    messagePayload: Buffer,
  ): Promise<void> {
    await callNative(() =>
      this.native.tell(target, routeKey, payloadType, messagePayload),
    );
  }
 
  /**
   * Trigger shutdown.
   *
   * Starts the shutdown process but does not wait for completion.
   */
  shutdown(): void {
    this.native.shutdown();
  }
 
  /**
   * Wait for shutdown to complete.
   *
   * Blocks until the actor has fully shut down.
   */
  async waitForShutdown(): Promise<void> {
    await this.native.waitForShutdown();
  }
 
  /**
   * Check if shutdown is in progress.
   *
   * @returns true if shutting down
   */
  isShuttingDown(): boolean {
    return this.native.isShuttingDown();
  }
 
  /**
   * Stop the actor (shutdown and wait).
   *
   * Convenience for shutdown() + waitForShutdown().
   *
   * @example
   * ```typescript
   * await actorRef.stop();
   * console.log('Actor stopped');
   * ```
   */
  async stop(): Promise<void> {
    this.shutdown();
    await this.waitForShutdown();
  }
}
 
type EncodableRequest = {
  encode(): Buffer;
};