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 guideBootTavoOptions#
type BootTavoOptions = BootTavoClientOptions & BootTavoServerOptions;Configures boot tavo in the application runtime.
Related guideBootTavoResult#
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 guidecomputedStore#
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 guidecreateExternalStore#
createExternalStore<T>(store: ExternalStore<T>): ExternalStore<T>Creates external store for the application runtime.
Related guidecreateServiceKey#
createServiceKey<T>(name: string): ServiceKey<T>Creates a typed key for registering and resolving a named service.
Related guidecreateStore#
createStore<T extends Record<string, unknown>>(initialState: T | StoreInitializer<T>): Store<T>Creates store for the application runtime.
Related guidecreateTavo#
createTavo<P extends AnyRecord, S extends AnyRecord, C = unknown>(definition: MvcComponentDefinition<P, S, C>): Component<P>Creates tavo for the application runtime.
Related guidedefineGlobalStore#
defineGlobalStore<T extends AnyRecord>(name: string, initialState: T | StoreInitializer<T>): Store<T>Defines a named global store once and returns the shared instance.
Related guideExternalStore#
type ExternalStore<T> = {
getSnapshot(): T;
getServerSnapshot?: () => T;
subscribe(listener: () => void): Unsubscribe;
};Defines storage behavior for external in the application runtime.
Related guidegetGlobalStore#
getGlobalStore<T extends AnyRecord>(name: string): Store<T>Looks up a previously defined global store by name.
Related guidegetService#
getService<T>(identifier: ServiceIdentifier<T>): TLooks up a previously registered service by name.
Related guidegetTavoBootMode#
getTavoBootMode(options?: Pick<BootTavoOptions, "root" | "rootSelector" | "hydrate"> | undefined): TavoBootModeReturns the boot mode Tavo.js will use for the current document.
Related guidehasGlobalStore#
hasGlobalStore(name: string): booleanReturns true when a named global store exists in the shared registry.
Related guidehasService#
hasService(identifier: ServiceIdentifier<unknown>): booleanReturns true when a named service exists in the shared registry.
Related guideMvcComponentDefinition#
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 guidepersistStore#
persistStore<T extends Record<string, unknown>>(store: Store<T>, options: PersistStoreOptions<T>): UnsubscribePersists store updates to browser storage and hydrates an initial saved snapshot when available.
Related guidePersistStoreOptions#
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 guideregisterService#
registerService<T>(identifier: ServiceIdentifier<T>, service: T, options?: RegisterServiceOptions | undefined): TRegisters a named app/service dependency and returns the same instance.
Related guideRegisterServiceOptions#
type RegisterServiceOptions = {
override?: boolean;
};Configures register service in the application runtime.
Related guideSelectorListener#
type SelectorListener<S, T> = (selected: S, previousSelected: S, state: T) => void;Defines a callback notified by selector in the application runtime.
Related guideServiceIdentifier#
type ServiceIdentifier<T = unknown> = string | ServiceKey<T>;Defines the service identifier contract used by the application runtime.
Related guideServiceKey#
type ServiceKey<T> = {
readonly name: string;
readonly __tavoServiceType?: T;
};Defines the service key contract used by the application runtime.
Related guideStore#
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 guideStoreInitializer#
type StoreInitializer<T extends Record<string, unknown>> = (set: StoreInitializerSet<T>, get: () => T) => T;Defines the store initializer contract used by the application runtime.
Related guideStoreInitializerSet#
type StoreInitializerSet<T extends Record<string, unknown>> = (partial: StatePatch<T>) => T;Defines the store initializer set contract used by the application runtime.
Related guideStoreListener#
type StoreListener<T> = (state: T, previous: T) => void;Defines a callback notified by store in the application runtime.
Related guideStorePath#
type StorePath = StorePathSegment | readonly StorePathSegment[];Defines the store path contract used by the application runtime.
Related guideStorePathSegment#
type StorePathSegment = string | number;Defines the store path segment contract used by the application runtime.
Related guideStoreSelector#
type StoreSelector<T, S> = (state: T) => S;Defines the store selector contract used by the application runtime.
Related guideStoreWatchListener#
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 guideTavoBootMode#
type TavoBootMode = "server" | "ssr" | "csr" | "none";Defines the tavo boot mode contract used by the application runtime.
Related guideTavoController#
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 guidetryGetService#
tryGetService<T>(identifier: ServiceIdentifier<T>): T | undefinedLooks up an optional service by name without throwing when it is missing.
Related guide