@zubridge/electron
Version:
A streamlined state management library for Electron applications using Zustand.
44 lines (43 loc) • 1.68 kB
TypeScript
import type { AnyState, Handlers } from '@zubridge/types';
import { type StoreApi } from 'zustand';
import type { ExtractState, ReadonlyStoreApi, DispatchFunc } from '@zubridge/types';
export type * from '@zubridge/types';
declare global {
interface Window {
zubridge: Handlers<AnyState>;
}
}
type UseBoundStore<S extends ReadonlyStoreApi<unknown>> = {
(): ExtractState<S>;
<U>(selector: (state: ExtractState<S>) => U): U;
} & S;
export declare const createHandlers: <S extends AnyState>() => Handlers<S>;
/**
* Creates a hook for accessing the store state in React components
*/
export declare const createUseStore: <S extends AnyState>(customHandlers?: Handlers<S>) => UseBoundStore<StoreApi<S>>;
/**
* Creates a dispatch function for sending actions to the main process
*
* @template S The state type
* @template TActions A record of action types to payload types mapping (optional)
* @param customHandlers Optional custom handlers to use instead of window.zubridge
* @returns A typed dispatch function
*
* @example
* // Basic usage
* const dispatch = useDispatch();
*
* @example
* // With typed actions
* type CounterActions = {
* 'COUNTER:INCREMENT': void;
* 'COUNTER:DECREMENT': void;
* 'COUNTER:SET': number;
* };
* const dispatch = useDispatch<State, CounterActions>();
* dispatch({ type: 'COUNTER:SET', payload: 5 }); // Type-safe payload
* dispatch({ type: 'UNKNOWN' }); // Type error
*/
export declare const useDispatch: <S extends AnyState = AnyState, TActions extends Record<string, any> = Record<string, any>>(customHandlers?: Handlers<S>) => DispatchFunc<S, TActions>;
export * from './utils/environment';