AshTypescript.TypedChannel.Codegen (ash_typescript v0.17.3)

Copy Markdown View Source

Generates TypeScript types and functions for typed channel event subscriptions.

For each declared event in a typed channel module, introspects the matching Ash PubSub publication's returns type (auto-derived via transform :calc or explicitly set) and maps it to a TypeScript type.

Generated Output

Types (emitted into ash_types.ts):

// Branded channel type  only creatable via createOrgChannel
export type OrgChannel = {
  readonly __channelType: "OrgChannel";
  on(event: string, callback: (payload: unknown) => void): number;
  off(event: string, ref: number): void;
};

// Payload types
export type ItemCreatedPayload = string;

// Events map
export type OrgChannelEvents = { item_created: ItemCreatedPayload; };

// Utility types for onOrgChannelMessages / unsubscribeOrgChannel
export type OrgChannelHandlers = { ... };
export type OrgChannelRefs = { ... };

Functions (emitted into the typed channels output file, e.g. ash_typed_channels.ts):

// Factory  the only way to obtain an OrgChannel
export function createOrgChannel(socket, suffix: string): OrgChannel { ... }

// Single-event subscription
export function onOrgChannelMessage<E extends keyof OrgChannelEvents>(
  channel: OrgChannel, event: E, handler: ...
): number { ... }

// Multi-event subscription
export function onOrgChannelMessages(channel: OrgChannel, handlers: OrgChannelHandlers): OrgChannelRefs { ... }

// Cleanup
export function unsubscribeOrgChannel(channel: OrgChannel, refs: OrgChannelRefs): void { ... }

Summary

Functions

Generates TypeScript subscription helper functions for all configured typed channel modules.

Generates TypeScript type declarations for all configured typed channel modules.

Generates TypeScript subscription helper functions for a single typed channel module.

Generates TypeScript type declarations for a single typed channel module.

Functions

generate_all_channel_functions(channel_entries)

@spec generate_all_channel_functions([{module(), String.t()}]) :: String.t()

Generates TypeScript subscription helper functions for all configured typed channel modules.

Accepts a list of {module, topic} tuples. Emits, per channel:

  • createOrgChannel factory (uses the topic pattern to construct the Phoenix topic string)
  • onOrgChannelMessage single-event subscription
  • onOrgChannelMessages multi-event subscription
  • unsubscribeOrgChannel cleanup

Returns only the function bodies without import statements. The caller is responsible for prepending imports from ash_types.ts.

generate_all_channel_types(channel_entries)

@spec generate_all_channel_types([{module(), String.t()}]) :: String.t()

Generates TypeScript type declarations for all configured typed channel modules.

Accepts a list of {module, topic} tuples. The topic (e.g. "org:*") is used to generate a branded OrgChannel type that prevents mixing channel instances.

Emits:

  • Deduplicated payload type aliases
  • Per-channel branded type, events map, handlers type, and refs type

Subscription helper functions are NOT included — use generate_all_channel_functions/1.

generate_channel_functions(channel_module, topic \\ nil)

@spec generate_channel_functions(module(), String.t() | nil) :: String.t()

Generates TypeScript subscription helper functions for a single typed channel module.

The optional topic causes a create* factory to be emitted and function signatures to use the branded channel type instead of a structural type.

generate_channel_types(channel_module, topic \\ nil)

@spec generate_channel_types(module(), String.t() | nil) :: String.t()

Generates TypeScript type declarations for a single typed channel module.

The optional topic (e.g. "org:*") causes a branded OrgChannel type to be emitted. Without a topic only payload aliases, the events map, and utility types are emitted — useful for isolated unit tests.