Navigated to /docs/core/api/application

MVC, stores, and services API

createTavo, controllers, application boot, services, stores, persistence, and selectors.

MVC, stores, and services exports

36 public exports, grouped under their canonical import boundary.

@tavojs/core

Canonical import boundary for every symbol in this section.

bootTavo#

bootTavo(options?: BootTavoOptions | undefined): Promise<BootTavoResult>

Boots the default Tavo.js app behavior for projects that use file-based pages.

Related guide

BootTavoOptions#

type BootTavoOptions = BootTavoClientOptions & BootTavoServerOptions;

Configures boot tavo in the application runtime.

Related guide

BootTavoResult#

type BootTavoResult = {
    mode: "client";
    root: Root;
} | {
    mode: "server";
    handle: ReturnType<typeof createNodeRequestHandler>;
    modules: PageModules;
} | {
    mode: "none";
};

Describes the result returned by boot tavo in the application runtime.

Related guide

computedStore#

computedStore<T extends Record<string, unknown>, S extends Record<string, unknown>>(source: Store<T>, selector: StoreSelector<T, S>, options?: { isEqual?: ((left: S, right: S) => boolean) | undefined; } | undefined): Store<S>

Creates a derived readonly store that updates whenever the source store's selected value changes.

Related guide

createExternalStore#

createExternalStore<T>(store: ExternalStore<T>): ExternalStore<T>

Creates external store for the application runtime.

Related guide

createServiceKey#

createServiceKey<T>(name: string): ServiceKey<T>

Creates a typed key for registering and resolving a named service.

Related guide

createStore#

createStore<T extends Record<string, unknown>>(initialState: T | StoreInitializer<T>): Store<T>

Creates store for the application runtime.

Related guide

createTavo#

createTavo<P extends AnyRecord, S extends AnyRecord, C = unknown>(definition: MvcComponentDefinition<P, S, C>): Component<P>

Creates tavo for the application runtime.

Related guide

defineGlobalStore#

defineGlobalStore<T extends AnyRecord>(name: string, initialState: T | StoreInitializer<T>): Store<T>

Defines a named global store once and returns the shared instance.

Related guide

ExternalStore#

type ExternalStore<T> = {
    getSnapshot(): T;
    getServerSnapshot?: () => T;
    subscribe(listener: () => void): Unsubscribe;
};

Defines storage behavior for external in the application runtime.

Related guide

getGlobalStore#

getGlobalStore<T extends AnyRecord>(name: string): Store<T>

Looks up a previously defined global store by name.

Related guide

getService#

getService<T>(identifier: ServiceIdentifier<T>): T

Looks up a previously registered service by name.

Related guide

getTavoBootMode#

getTavoBootMode(options?: Pick<BootTavoOptions, "root" | "rootSelector" | "hydrate"> | undefined): TavoBootMode

Returns the boot mode Tavo.js will use for the current document.

Related guide

hasGlobalStore#

hasGlobalStore(name: string): boolean

Returns true when a named global store exists in the shared registry.

Related guide

hasService#

hasService(identifier: ServiceIdentifier<unknown>): boolean

Returns true when a named service exists in the shared registry.

Related guide

listGlobalStores#

listGlobalStores(): string[]

Lists all registered global store names.

Related guide

listServices#

listServices(): string[]

Lists all registered service names.

Related guide

MvcComponentDefinition#

type MvcComponentDefinition<P extends AnyRecord, S extends AnyRecord, C> = {
    model?: (props: P) => S | Store<S>;
    controller?: new (ctx: MvcControllerContext<P, S>) => C;
    createController?: (ctx: MvcControllerContext<P, S>) => C;
    view: (ctx: MvcContext<P, S, C>) => Child;
};

Defines the mvc component definition contract used by the application runtime.

Related guide

persistStore#

persistStore<T extends Record<string, unknown>>(store: Store<T>, options: PersistStoreOptions<T>): Unsubscribe

Persists store updates to browser storage and hydrates an initial saved snapshot when available.

Related guide

PersistStoreOptions#

type PersistStoreOptions<T extends Record<string, unknown>> = {
    key: string;
    storage?: StorageLike;
    serialize?: (state: T) => string;
    deserialize?: (raw: string) => Partial<T> | T;
    pick?: (state: T) => Partial<T> | T;
};

Configures persist store in the application runtime.

Related guide

registerService#

registerService<T>(identifier: ServiceIdentifier<T>, service: T, options?: RegisterServiceOptions | undefined): T

Registers a named app/service dependency and returns the same instance.

Related guide

RegisterServiceOptions#

type RegisterServiceOptions = {
    override?: boolean;
};

Configures register service in the application runtime.

Related guide

SelectorListener#

type SelectorListener<S, T> = (selected: S, previousSelected: S, state: T) => void;

Defines a callback notified by selector in the application runtime.

Related guide

ServiceIdentifier#

type ServiceIdentifier<T = unknown> = string | ServiceKey<T>;

Defines the service identifier contract used by the application runtime.

Related guide

ServiceKey#

type ServiceKey<T> = {
    readonly name: string;
    readonly __tavoServiceType?: T;
};

Defines the service key contract used by the application runtime.

Related guide

Store#

type Store<T extends Record<string, unknown>> = {
    getState(): T;
    setState(next: StateUpdater<T>): T;
    set<K extends keyof T>(key: K, value: StoreSetValue<T, T[K]>): T;
    set<S = unknown>(path: StorePath, value: StoreSetValue<T, S>): T;
    patch(partial: StatePatch<T>): T;
    subscribe(listener: StoreListener<T>, options?: {
        immediate?: boolean;
    }): Unsubscribe;
    subscribeSelector<S>(selector: StoreSelector<T, S>, listener: SelectorListener<S, T>, options?: {
        immediate?: boolean;
        isEqual?: (a: S, b: S) => boolean;
    }): Unsubscribe;
    watch<K extends keyof T>(key: K, listener: StoreWatchListener<T[K], T>, options?: {
        immediate?: boolean;
        isEqual?: (a: T[K], b: T[K]) => boolean;
    }): Unsubscribe;
    watch<S>(selector: StoreSelector<T, S>, listener: StoreWatchListener<S, T>, options?: {
        immediate?: boolean;
        isEqual?: (a: S, b: S) => boolean;
    }): Unsubscribe;
    watch<S = unknown>(path: StorePath, listener: StoreWatchListener<S, T>, options?: {
        immediate?: boolean;
        isEqual?: (a: S, b: S) => boolean;
    }): Unsubscribe;
};

Defines storage behavior for application runtime in the application runtime.

Related guide

StoreInitializer#

type StoreInitializer<T extends Record<string, unknown>> = (set: StoreInitializerSet<T>, get: () => T) => T;

Defines the store initializer contract used by the application runtime.

Related guide

StoreInitializerSet#

type StoreInitializerSet<T extends Record<string, unknown>> = (partial: StatePatch<T>) => T;

Defines the store initializer set contract used by the application runtime.

Related guide

StoreListener#

type StoreListener<T> = (state: T, previous: T) => void;

Defines a callback notified by store in the application runtime.

Related guide

StorePath#

type StorePath = StorePathSegment | readonly StorePathSegment[];

Defines the store path contract used by the application runtime.

Related guide

StorePathSegment#

type StorePathSegment = string | number;

Defines the store path segment contract used by the application runtime.

Related guide

StoreSelector#

type StoreSelector<T, S> = (state: T) => S;

Defines the store selector contract used by the application runtime.

Related guide

StoreWatchListener#

type StoreWatchListener<S, T> = (selected: S, previousSelected: S, state: T, previousState: T) => void;

Defines a callback notified by store watch in the application runtime.

Related guide

TavoBootMode#

type TavoBootMode = "server" | "ssr" | "csr" | "none";

Defines the tavo boot mode contract used by the application runtime.

Related guide

TavoController#

class TavoController implements MvcControllerTools {
    #private;
    model: Store<any>;
    props: AnyRecord;
    router: MvcControllerFrameworkContext["router"];
    stores: MvcControllerFrameworkContext["stores"];
    services: MvcControllerFrameworkContext["services"];
    capabilities: MvcControllerFrameworkContext["capabilities"];
    page: MvcControllerFrameworkContext["page"];
    __setTavoControllerContext(context: Pick<MvcControllerContext<AnyRecord, AnyRecord>, "model" | "props">): void;
    __setTavoControllerTools(tools: MvcControllerTools): void;
    cleanup(fn: Unsubscribe): Unsubscribe;
    createId(prefix?: string): string;
    setTimeout(fn: () => void, delay?: number): Unsubscribe;
    setInterval(fn: () => void, delay?: number): Unsubscribe;
    action<TResult, TArgs extends unknown[]>(fn: (...args: TArgs) => Promise<TResult> | TResult): TavoAction<TResult, TArgs>;
    scheduleLayoutEffect(fn: () => void | Unsubscribe): Unsubscribe;
    scheduleAfterRender(fn: () => void): Unsubscribe;
    scheduleOnMount(fn: () => void | Unsubscribe): Unsubscribe;
    listen<T extends AnyRecord>(store: Store<T>, listener: StoreListener<T>, options?: {
        immediate?: boolean;
    }): Unsubscribe;
    select<T extends AnyRecord, S>(store: Store<T>, selector: StoreSelector<T, S>, listener: SelectorListener<S, T>, options?: {
        immediate?: boolean;
        isEqual?: (a: S, b: S) => boolean;
    }): Unsubscribe;
    watch<T extends AnyRecord, S>(store: Store<T>, target: StorePath | StoreSelector<T, S>, listener: StoreWatchListener<S, T>, options?: {
        immediate?: boolean;
        isEqual?: (a: S, b: S) => boolean;
    }): Unsubscribe;
    listenExternal<T>(store: ExternalStore<T>, listener: (snapshot: T, previousSnapshot: T) => void, options?: {
        immediate?: boolean;
        isEqual?: (a: T, b: T) => boolean;
    }): Unsubscribe;
    observeResize<T extends Element>(target: ElementTarget<T>, listener: ResizeObserverCallback, options?: ResizeObserverOptions): Unsubscribe;
    observeIntersection<T extends Element>(target: ElementTarget<T>, listener: IntersectionObserverCallback, options?: IntersectionObserverInit): Unsubscribe;
    observeMutation<T extends Node>(target: T | {
        current: T | null;
    }, listener: MutationCallback, options?: MutationObserverInit): Unsubscribe;
}

Defines the tavo controller contract used by the application runtime.

Related guide

tryGetService#

tryGetService<T>(identifier: ServiceIdentifier<T>): T | undefined

Looks up an optional service by name without throwing when it is missing.

Related guide