UNPKG

@shined/reactive

Version:

⚛️ Proxy-driven state library for JavaScript application, Intuitive, Flexible and Written in TypeScript.

148 lines (138 loc) 5.99 kB
import { W as WithDerivedContributes, S as SnapshotOptions, V as VanillaStore, a as SubscribeListener, b as StoreCreateOptions, c as WithSubscribeContributes, d as WithSnapshotContributes, e as SnapshotSelector } from './index-DidHol2k.js'; export { x as ChangeItem, C as ConnectResponse, D as DevtoolsOptions, E as ExtConfig, L as Listener, n as ReduxExtension, R as RestoreOptions, o as StoreListener, m as StoreSubscriber, y as SubscribeCallback, i as createVanilla, v as deepCloneWithRef, f as devtools, j as getSnapshot, t as isRef, p as produce, q as proxy, r as ref, g as setGlobalNotifyInSync, h as snapshot, s as subscribe, u as useSnapshot, w as withDerived, k as withSnapshot, l as withSubscribe } from './index-DidHol2k.js'; import { E as ExpandType } from './index-DuR1RHFT.js'; export { A as AnyFunc, D as DeepExpandType, L as LISTENERS, R as REACTIVE, S as SNAPSHOT, e as canProxy, d as createObjectFromPrototype, f as deepEqual, g as get, h as hasOwn, b as isBoolean, c as isFunction, a as isObject, i as isProduction, n as noop, p as propertyKeysToPath, s as shallowEqual } from './index-DuR1RHFT.js'; interface WithUseDerivedContributes<State extends object, Derived extends object = State> extends WithDerivedContributes<State, Derived> { /** * Get the derived state. */ useDerived: (options?: SnapshotOptions<Derived>) => Derived; } /** * Enhances a store with `useDerived` & `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) * // in React component * export function App() { * const { isNegative } = store.useDerived() * } * ``` */ declare function withUseDerived<Store extends VanillaStore<object>, Derived extends object = Store['mutate']>(store: Store, mapFn?: (state: Store['mutate']) => Derived): Store & WithUseDerivedContributes<Store['mutate'], Derived>; interface WithUseSnapshotContributes<State extends object> { /** * Get the snapshot of the state. */ useSnapshot: StoreUseSnapshot<State>; } /** * Enhances a store with `useSnapshot` 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 = withUseSnapshot( * createVanilla({ * count: 123, * info: { name: 'Viki' }, * }), * ) * * // in normal JS/TS * console.log(store.snapshot(), store.snapshot((s) => s.count)) * * // in React component * export function App() { * const { count } = store.useSnapshot() * const count = store.useSnapshot((s) => s.count) * } * * ``` */ declare function withUseSnapshot<Store extends VanillaStore<object>>(store: Store): Store & WithUseSnapshotContributes<Store['mutate']>; interface WithUseSubscribeContributes<State extends object> { /** * Subscribes to the store state changes in React components. * * @param listener - The listener function. * @param notifyInSync - Whether to notify the listener in sync. */ useSubscribe: (listener: SubscribeListener<State>, notifyInSync?: boolean) => void; } /** * Enhance a store with the `useSubscribe` methods. * * @param store - The store to be enhanced. * @returns The enhanced store. * * @since 0.2.0 * * @example * * ```ts * import { createVanilla, withUseSubscribe } from '@reactive-react/core' * * const store = withUseSubscribe(createVanilla({ count: 0 })) * * // In a React component * store.useSubscribe((state) => { * console.log(state.count) * }) * ``` * */ declare function withUseSubscribe<Store extends VanillaStore<object>>(store: Store): Store & WithUseSubscribeContributes<Store['mutate']>; interface StoreUseSnapshot<State> { (): State; (options: SnapshotOptions<State>): State; <StateSlice>(selector: SnapshotSelector<State, StateSlice>): StateSlice; <StateSlice>(selector: undefined, options: SnapshotOptions<StateSlice>): State; <StateSlice>(selector: SnapshotSelector<State, StateSlice>, options: SnapshotOptions<StateSlice>): StateSlice; } interface Store<State extends object> extends ExpandType<VanillaStore<State> & WithSubscribeContributes<State> & WithSnapshotContributes<State> & WithUseSnapshotContributes<State> & WithUseSubscribeContributes<State>> { } declare function createWithHooks<State extends object>(initialState: State, options?: StoreCreateOptions): Store<State>; interface UseReactiveOptions<State> extends SnapshotOptions<State> { } /** * A React Hook that helps to use [Reactive](https://sheinsight.github.io/reactive) in React with ease. * * @since 0.2.0 */ declare function useReactive<State extends object>(initialState: State, options?: UseReactiveOptions<State>): readonly [State, State]; /** * Subscribes to the store state changes in React components. * * @param proxyState - The store state. * @param listener - The listener function. * @param notifyInSync - Whether to notify the listener in sync. * * @since 0.2.0 */ declare function useSubscribe<State extends object>(proxyState: State, listener: SubscribeListener<State>, notifyInSync?: boolean): void; export { ExpandType, SnapshotOptions, SnapshotSelector, type Store, StoreCreateOptions, type StoreUseSnapshot, SubscribeListener, type UseReactiveOptions, VanillaStore, WithDerivedContributes, WithSnapshotContributes, WithSubscribeContributes, type WithUseDerivedContributes, type WithUseSnapshotContributes, type WithUseSubscribeContributes, createWithHooks as create, createWithHooks, useReactive, useSubscribe, withUseDerived, withUseSnapshot, withUseSubscribe };