@shined/reactive
Version:
⚛️ Proxy-driven state library for JavaScript application, Intuitive, Flexible and Written in TypeScript.
251 lines (237 loc) • 8.79 kB
TypeScript
import { D as DeepExpandType } from './index-DuR1RHFT.js';
interface SnapshotOptions<StateSlice> {
/**
* Whether to notify updates synchronously, default is false, which means updates are batched asynchronously to improve performance.
*
* In some scenarios (e.g., using Chinese input method in a text box), it is necessary to immediately obtain the latest state, in which case this can be set to true.
*
* @defaultValue false
*/
sync?: boolean;
/**
* Custom equality function to compare the previous and next state slices.
*
* @defaultValue Object.is
*/
isEqual?: (a: StateSlice, b: StateSlice) => boolean;
}
type SnapshotSelector<State, StateSlice> = (state: State) => StateSlice;
/**
* Returns a snapshot of the store state.
*
* @since 0.2.0
*/
declare function useSnapshot<State extends object>(state: State): State;
declare function useSnapshot<State extends object>(state: State, options: SnapshotOptions<State>): State;
declare function useSnapshot<State extends object, StateSlice>(state: State, selector: SnapshotSelector<State, StateSlice>): StateSlice;
declare function useSnapshot<State extends object, StateSlice>(state: State, selector: undefined, options: SnapshotOptions<StateSlice>): State;
declare function useSnapshot<State extends object, StateSlice>(state: State, selector?: SnapshotSelector<State, StateSlice>, options?: SnapshotOptions<StateSlice>): StateSlice;
interface WithSnapshotContributes<State extends object> {
/**
* Get the snapshot of the state.
*/
snapshot: <StateSlice = State>(selector?: SnapshotSelector<State, StateSlice>) => StateSlice;
}
/**
* Enhances a store with a `snapshot` method that returns a snapshot of the store's state.
*
* @param store - The store to enhance.
* @returns The enhanced store.
*
* @since 0.2.0
*
* @example
*
* ```tsx
* const store = withSnapshot(
* createVanilla({
* count: 123,
* info: { name: 'Viki' },
* }),
* )
*
* // in normal JS/TS
* console.log(store.snapshot(), store.snapshot((s) => s.count))
* ```
*/
declare function withSnapshot<Store extends VanillaStore<object>>(store: Store): Store & WithSnapshotContributes<Store['mutate']>;
interface WithSubscribeContributes<State extends object> {
/**
* Subscribe to the store's state changes.
*
* @param listener - The listener to be called when the state changes.
* @param sync - Whether to call the listener synchronously.
* @param selector - A selector to select a part of the state to be passed to the listener.
*/
subscribe: (listener: SubscribeListener<State>, sync?: boolean, selector?: (state: State) => object) => () => void;
}
/**
* Enhance a store with the `subscribe` method.
*
* @param store - The store to be enhanced.
* @returns The enhanced store.
*
* @since 0.2.0
*
* @example
*
* ```ts
* import { createVanilla, withSubscribe } from '@reactive-react/core'
*
* const store = withSubscribe(createVanilla({ count: 0 }))
*
* const unsubscribe = store.subscribe((state) => {
* console.log(state.count)
* })
* ```
*
*/
declare function withSubscribe<Store extends VanillaStore<object>>(store: Store): Store & WithSubscribeContributes<Store['mutate']>;
interface ChangeItem<State> {
props: PropertyKey[];
propsPath: string;
previous: unknown;
current: unknown;
snapshot: State;
}
/**
* @deprecated Use `SubscribeListener` instead. Will be removed in the next major version.
*/
type SubscribeCallback<State> = SubscribeListener<State>;
type SubscribeListener<State> = (changes: ChangeItem<State>, version?: number) => void;
/**
* Set the global default `sync` option.
*
* when the`sync` option is not set, this value will be used as fallback.
*
* set to `true` to notify subscribers in sync by default, or `false` to notify them asynchronously by default.
*
* @default false
*
* @since 0.3.0
*/
declare function setGlobalNotifyInSync(value: boolean): void;
/**
* Subscribe to the store's state changes.
*/
declare function subscribe<State extends object>(proxyState: State, callback: SubscribeListener<State>, notifyInSync?: boolean): () => void;
interface StoreCreateOptions {
}
type StoreSubscriber<State extends object> = (
/**
* Callback to be called when state changes.
*/
callback: SubscribeListener<State>,
/**
* Whether to sync the callback with the current state.
*/
sync?: boolean,
/**
* @deprecated It is confusing, and makes TS type wrong in callback's `changes` argument. It will be removed in the next major version.
*/
selector?: (state: State) => object) => () => void;
interface VanillaStore<State extends object> {
/**
* The mutable state object, whose changes will trigger subscribers.
*/
mutate: State;
/**
* Restore to initial state.
*/
restore: (options?: RestoreOptions<State>) => void;
}
interface RestoreOptions<State extends object> {
/**
* Exclude some **top** keys from restoring.
*
* @since 0.2.5
*/
exclude?: (keyof State)[];
}
/**
* Create a vanilla store.
*
* @param initState The initial state object.
* @param options Options for creating the store.
*
* @since 0.2.0
*/
declare function createVanilla<State extends object>(initState: State, _options?: StoreCreateOptions): VanillaStore<State> & WithSubscribeContributes<State> & WithSnapshotContributes<State>;
declare const create: typeof createVanilla;
type ReduxExtension = (Window extends {
__REDUX_DEVTOOLS_EXTENSION__?: infer T;
} ? T : {
connect: (param: any) => any;
})['connect'];
type ExtConfig = Parameters<ReduxExtension>[0];
type ConnectResponse = ReturnType<ReduxExtension> & {
unsubscribe: () => void;
subscribe: (fn: (message: any) => void) => void;
error: (message: string) => void;
};
/** redux devtool options, if set, will enable redux devtool */
type DevtoolsOptions = DeepExpandType<{
name: string;
/** @default true */
enable?: boolean;
} & ExtConfig>;
declare function devtools(store: {
mutate: object;
}, options: DevtoolsOptions): () => void;
/**
* Get a snapshot of the store state.
*
* @param proxyState The store state.
*
* @since 0.2.0
*/
declare function snapshot<State extends object, StateSlice = State>(proxyState: State, selector?: SnapshotSelector<State, StateSlice>): StateSlice;
/**
* @deprecated Use `snapshot` instead. Will be removed in the next major version.
*/
declare const getSnapshot: typeof snapshot;
declare function produce<State extends object>(obj: State, draftHandler: (draft: State) => void): State;
type StoreListener = (props: PropertyKey[], version?: number) => void;
/**
* @deprecated use `StoreListener` instead
*/
type Listener = StoreListener;
declare function proxy<State extends object>(initState: State, parentProps?: PropertyKey[]): State;
declare function ref<T extends object>(o: T): T;
declare function isRef<T extends object>(k: T): boolean;
declare function deepCloneWithRef<State extends object>(initialState: State): State;
interface WithDerivedContributes<State extends object, D extends object = State> {
/**
* Get the derived state.
*/
derived: () => D;
}
/**
*
* Enhances a store with `derived` method that returns the derived state.
*
* @param store - The store to enhance.
* @param mapFn - The function to map the state to the derived state.
* @returns The enhanced store.
*
* @since 0.2.0
*
* @example
*
* ```tsx
* const store = withDerived(
* create({
* count: 123,
* info: { name: 'Viki' },
* }),
* (s) => ({
* isViki: s.info.name === 'Viki',
* isNegative: s.count < 0,
* }),
* )
* // in normal JS/TS
* console.log(store.derived().isViki, store.derived().isNegative)
* ```
*/
declare function withDerived<Store extends VanillaStore<object>, Derived extends object = Store['mutate']>(store: Store, mapFn?: (state: Store['mutate']) => Derived): Store & WithDerivedContributes<Store['mutate'], Derived>;
export { type ConnectResponse as C, type DevtoolsOptions as D, type ExtConfig as E, type Listener as L, type RestoreOptions as R, type SnapshotOptions as S, type VanillaStore as V, type WithDerivedContributes as W, type SubscribeListener as a, type StoreCreateOptions as b, type WithSubscribeContributes as c, type WithSnapshotContributes as d, type SnapshotSelector as e, devtools as f, setGlobalNotifyInSync as g, snapshot as h, createVanilla as i, getSnapshot as j, withSnapshot as k, withSubscribe as l, type StoreSubscriber as m, type ReduxExtension as n, type StoreListener as o, produce as p, proxy as q, ref as r, subscribe as s, isRef as t, useSnapshot as u, deepCloneWithRef as v, withDerived as w, type ChangeItem as x, type SubscribeCallback as y, create as z };