Skip to content

Commit

Permalink
Export actor proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia committed Oct 20, 2024
1 parent 1014754 commit 9a7c780
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/actors/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export interface Actor {
}

export type { ActorProxy } from "./proxyutil.ts";
export { ActorRuntime } from "./runtime.ts";
export type { ActorConstructor } from "./runtime.ts";
export { ActorState } from "./state.ts";
Expand Down
4 changes: 2 additions & 2 deletions src/actors/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// deno-lint-ignore-file no-explicit-any
import { create, createHttpInvoker, type Promisify } from "./proxyutil.ts";
import { create, createHttpInvoker, type ActorProxy } from "./proxyutil.ts";
import type { Actor, ActorConstructor } from "./runtime.ts";

export interface ActorsServer {
Expand All @@ -18,7 +18,7 @@ export const actors = {
proxy: <TInstance extends Actor>(
actor: ActorConstructor<TInstance> | string,
options?: ActorsOptions | undefined,
): { id: (id: string) => Promisify<TInstance> } => {
): { id: (id: string) => ActorProxy<TInstance> } => {
const factory = (id: string) => createHttpInvoker(id, options);
return create(actor, factory);
},
Expand Down
13 changes: 8 additions & 5 deletions src/actors/proxyutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,15 @@ export type PromisifyKey<key extends keyof Actor, Actor> = Actor[key] extends
: { (...args: Args): Promise<Return> }
: Actor[key];

export type Promisify<Actor> =
/**
* Represents an actor proxy.
*/
export type ActorProxy<Actor> =
& {
[key in keyof Actor]: PromisifyKey<key, Actor>;
}
& (Actor extends { metadata?: infer TMetadata } ? {
withMetadata(metadata: TMetadata): Promisify<Actor>;
withMetadata(metadata: TMetadata): ActorProxy<Actor>;
}
// deno-lint-ignore ban-types
: {});
Expand Down Expand Up @@ -288,11 +291,11 @@ export const create = <TInstance extends Actor>(
actor: ActorConstructor<TInstance> | string,
invokerFactory: (id: string) => ActorInvoker,
metadata?: unknown,
): { id: (id: string) => Promisify<TInstance> } => {
): { id: (id: string) => ActorProxy<TInstance> } => {
const name = typeof actor === "string" ? actor : actor.name;
return {
id: (id: string): Promisify<TInstance> => {
return new Proxy<Promisify<TInstance>>({} as Promisify<TInstance>, {
id: (id: string): ActorProxy<TInstance> => {
return new Proxy<ActorProxy<TInstance>>({} as ActorProxy<TInstance>, {
get: (_, method) => {
if (method === "withMetadata") {
return (m: unknown) => create(actor, invokerFactory, m).id(id);
Expand Down

0 comments on commit 9a7c780

Please sign in to comment.