@stacksjs/stx
Version:
A performant UI Framework. Powered by Bun.
70 lines • 2.07 kB
TypeScript
/**
* Define a reactive store with state, getters, and actions.
*/
export declare function defineStore<S extends object, G extends Record<string, (state: S) => any> = Record<string, never>, A extends Record<string, (...args: any[]) => any> = Record<string, never>>(id: string, options: StoreOptions<S, G, A>): Store<S> & { [K in keyof G]: ReturnType<G[K]> } & { [K in keyof A]: A[K] };
/**
* Register multiple stores at once.
*/
export declare function registerStores(stores: Record<string, any>): void;
/**
* Get a store by name.
*/
export declare function getStore<T = any>(name: string): T | undefined;
/**
* Wait for a store to be available.
*/
export declare function waitForStore<T = any>(name: string, timeout?: any): Promise<T>;
/**
* STX Stores - Client Runtime
*
* This module provides the client-side store functionality for STX applications.
* It enables clean imports like: import { appStore } from '@stores'
*
* @example
* ```html
* <!-- In your layout or main template -->
* <script src="/js/stx-stores.js"></script>
*
* <!-- In your stores definition file -->
* <script>
* const { defineStore, registerStores } = window.STX;
*
* const appStore = defineStore('app', {
* state: { count: 0 },
* actions: {
* increment() { this.count++ }
* }
* });
*
* registerStores({ appStore });
* </script>
*
* <!-- In your components -->
* <script client>
* import { appStore } from '@stores'
* appStore.increment()
* </script>
* ```
*/
// Type definitions
export declare interface Subscriber<T> {
(value: T, previousValue?: T): void
}
export declare interface PersistOptions {
storage?: 'local' | 'session' | 'memory'
key?: string
}
export declare interface StoreOptions<S, G, A> {
state: S | (() => S)
getters?: G
actions?: A
persist?: PersistOptions | boolean
}
export declare interface Store<S> {
$state: S
$subscribe: (callback: Subscriber<S>) => Unsubscribe
$reset: () => void
$patch: (partial: Partial<S> | ((state: S) => void)) => void
$id: string
}
export type Unsubscribe = () => void