UNPKG

reblendjs

Version:

This is build using react way of handling dom but with web components

136 lines (135 loc) 6.91 kB
import { BaseComponent } from './BaseComponent'; import { ReblendTyping } from 'reblend-typing'; declare const contextValue: unique symbol; declare const contextInnerValue: unique symbol; declare const contextValueInitial: unique symbol; declare const contextSubscribers: unique symbol; declare const contextSubscribe: unique symbol; declare const contextSubscriberModificationTracker: unique symbol; /** * Enum representing different cache storage types. */ export declare enum CacheType { MEMORY = 0, SESSION = 1, LOCAL = 2 } /** * Options for configuring caching behavior for a context. * * @typedef {object} CacheOption * @property {CacheType} type - The type of cache storage (e.g., MEMORY, SESSION, LOCAL). * @property {string} key - The key used to store/retrieve the cached data. */ type CacheOption = { type: CacheType; key: string; }; type ContextSubriber = { component: BaseComponent; stateKey: string; }; /** * Represents a context object in Reblend, tracking state and subscribers. * * @template T - The type of the context value. * @typedef {object} Context<T> * @property {Set<ContextSubriber>} [contextSubscribers] - Array of components subscribed to this context and their state keys. * @property {T} [contextValue] - The current value of the context. * @property {T} [contextValueInitial] - The initial value of the context. * @property {T} [contextInnerValue] - The actual stored value, potentially synced with cache. * @property {number[]} [contextSubscriberModificationTracker] - Tracker for subscriber modifications. * @property {Function} reset - Resets the context value to the initial value. * @property {Function} getValue - Retrieves the current context value. * @property {Function} isEqual - Checks if a given value is equal to the current context value. * @property {Function} update - Updates the context value and notifies subscribers. * @property {Function} [contextSubscribe] - Subscribes a component to this context with a given state key. */ export type Context<T> = { [contextSubscribers]: Set<ContextSubriber>; [contextValue]: T; [contextValueInitial]: T; [contextInnerValue]: T; [contextSubscriberModificationTracker]: number[]; reset: () => void; getValue: () => T; isEqual: (value: T) => boolean; update(updateValue: ReblendTyping.StateFunctionValue<T>, force?: boolean): Promise<boolean>; [contextSubscribe](subscriber: ContextSubriber): void; }; /** * Hook to manage state within a Reblend component. * * @template T - The type of the state value. * @param {T} _initial - The initial state value. * @param {string[]} _dependencyStringAndOrStateKey - Optional dependencies and state keys for tracking. * @returns {[T, ReblendTyping.StateFunction<T>]} - Returns the current state and a function to update it. */ export declare function useState<T>(_initial: T, ..._dependencyStringAndOrStateKey: string[]): [T, ReblendTyping.StateFunction<T>]; /** * Hook to perform side effects within a Reblend component. * * @param {ReblendTyping.StateEffectiveFunction} _fn - The effect function to run. * @param {any[]} [_dependencies] - Optional array of dependencies to control when the effect runs. * @param {string[]} _dependencyStringAndOrStateKey - Optional dependencies and state keys for tracking. */ export declare function useEffect(_fn: ReblendTyping.StateEffectiveFunction, _dependencies?: any[], ..._dependencyStringAndOrStateKey: string[]): void; /** * Hook to manage reducer-based state within a Reblend component. * * @template T - The type of the state value. * @template I - The type of the action passed to the reducer. * @param {ReblendTyping.StateReducerFunction<T, I>} _reducer - The reducer function to apply actions to state. * @param {T} _initial - The initial state value. * @param {string[]} _dependencyStringAndOrStateKey - Optional dependencies and state keys for tracking. * @returns {[T, ReblendTyping.StateFunction<T>]} - Returns the current state and a function to dispatch actions. */ export declare function useReducer<T, I>(_reducer: ReblendTyping.StateReducerFunction<T, I>, _initial: T, ..._dependencyStringAndOrStateKey: string[]): [T, ReblendTyping.StateFunction<T>]; /** * Hook to create memoized values within a Reblend component. * * @template T - The type of the memoized value. * @param {ReblendTyping.StateEffectiveMemoFunction<T>} _fn - The function to compute the memoized value. * @param {any[]} [_dependencies] - Optional array of dependencies to control memoization. * @param {string[]} _dependencyStringAndOrStateKey - Optional dependencies and state keys for tracking. * @returns {T} - The memoized value. */ export declare function useMemo<T>(_fn: ReblendTyping.StateEffectiveMemoFunction<T>, _dependencies?: any[], ..._dependencyStringAndOrStateKey: string[]): T; /** * Hook to create a mutable reference object within a Reblend component. * * @template T - The type of the ref value. * @param {T} [_initial] - The initial ref value. * @param {string[]} _dependencyStringAndOrStateKey - Optional dependencies and state keys for tracking. * @returns {ReblendTyping.Ref<T>} - Returns a reference object with the current value. */ export declare function useRef<T>(_initial?: T, ..._dependencyStringAndOrStateKey: string[]): ReblendTyping.Ref<T>; /** * Hook to memoize a callback function within a Reblend component. * * @param {Function} _fn - The callback function to memoize. * @param {string[]} _dependencyStringAndOrStateKey - Optional dependencies and state keys for tracking. * @returns {Function} - The memoized callback function. */ export declare function useCallback<T extends Function>(_fn: T, ..._dependencyStringAndOrStateKey: string[]): T; /** * Hook to subscribe to a context and get its current value. * * @template T - The type of the context value. * @param {Context<T>} context - The context to subscribe to. * @param {string[]} dependencyStringAndOrStateKey - Optional dependencies and state keys for tracking. * @returns {[T, ReblendTyping.StateFunction<T>]} - Returns the current context value and a function to update it. * @throws Will throw an error if the context is invalid or if a state key is not provided. */ export declare function useContext<T>(context: Context<T>, ...dependencyStringAndOrStateKey: string[]): [T, ReblendTyping.StateFunction<T>]; /** * Function to create a new context with an initial value. * Optionally, you can specify cache options for storing the context value in session or local storage. * * @template T - The type of the context value. * @param {T} initial - The initial value of the context. * @param {CacheOption} [cacheOption] - Optional caching options. * @returns {Context<T>} - The created context object. */ export declare function createContext<T>(initial: T, cacheOption?: CacheOption): Context<T>; export {};