@magnusbag/livets-core
Version:
TypeScript API layer for LiveTS framework
103 lines • 3.11 kB
TypeScript
/**
* LiveView base class - The foundation for all LiveTS components
*/
import type { LiveViewState, EventPayload, ComponentProps, EventHandler, PubSubHandler, LiveViewMetadata, PubSubMessage, ComponentId } from './types';
export declare abstract class LiveView {
protected state: LiveViewState;
protected readonly componentId: ComponentId;
protected props: ComponentProps;
protected metadata: LiveViewMetadata;
protected eventHandlers: Map<string, EventHandler>;
protected subscriptions: Map<string, PubSubHandler>;
constructor(props?: ComponentProps);
/**
* Called when the component is first mounted
* Use this to initialize state and set up subscriptions
*/
abstract mount(): void | Promise<void>;
/**
* Renders the component to an HTML string
* This method is called whenever the component needs to re-render
*/
abstract render(): string;
/**
* Called after the component state has been updated
* Use this for side effects after state changes
*/
updated?(): void | Promise<void>;
/**
* Called when the component is being unmounted
* Use this to clean up subscriptions and resources
*/
unmount?(): void | Promise<void>;
/**
* Updates the component state and triggers a re-render
*/
protected setState(updates: Partial<LiveViewState>): void;
/**
* Gets the current state (read-only)
*/
protected getState(): Readonly<LiveViewState>;
/**
* Handles events from the client
* Override this method to handle custom events
*/
handleEvent(event: string, payload: EventPayload): void | Promise<void>;
/**
* Registers an event handler
*/
protected on(event: string, handler: EventHandler): void;
/**
* Removes an event handler
*/
protected off(event: string): void;
/**
* Subscribes to a pub/sub channel
*/
protected subscribe(channel: string, handler: PubSubHandler): void;
/**
* Unsubscribes from a pub/sub channel
*/
protected unsubscribe(channel: string): void;
/**
* Broadcasts a message to a pub/sub channel
*/
protected broadcast(channel: string, data: any): void;
/**
* Handles incoming pub/sub messages
*/
handlePubSubMessage(message: PubSubMessage): void;
/**
* Mounts the component (internal use)
*/
_mount(): Promise<void>;
/**
* Unmounts the component (internal use)
*/
_unmount(): Promise<void>;
/**
* Renders the component (internal use)
*/
_render(): string;
/**
* Gets the component ID
*/
getComponentId(): ComponentId;
/**
* Gets the component props
*/
getProps(): Readonly<ComponentProps>;
/**
* Checks if the component is mounted
*/
isMounted(): boolean;
/**
* Schedules a re-render (internal use)
*/
private scheduleRerender;
/**
* Wraps the rendered HTML with component identification
*/
private wrapWithComponentId;
}
//# sourceMappingURL=live-view.d.ts.map