/** * A callback function that will be called every time the state changes. * Can take the updated state as an argument. */ type SubscriptionCb = (newState: T) => void; /** * A callback function for unsubscribing. */ type UnsubscribeCb = () => void; /** Callback for state updates. */ type UpdateCb = (state: T) => void; /** * It may contain additional information about the subscriber, * as well as a condition for notification, a mark that the subscriber is protected and properties to watch. */ type SubscriptionOptions = { notifyCondition?: (state: T) => boolean; protect?: boolean; properties?: Array; }; /** * Public API. * Interacts with the @see {StatemanjsBaseAPI}. */ interface StatemanjsAPI { /** * Accepts a new state and compares it with the current one. * Nothing will happen if the passed value is equal to the current one. * @param newState New state. * @returns Status of operation. */ set(newState: T): boolean; /** Get current state */ get(): T; /** * The method of subscribing to the status change. * Accepts a callback function (subscription callback), * which will be called at each update, and a subscription options object. * In the options, you can specify information about the subscription, * as well as specify the condition under which the subscriber will be notified * and mark the subscriber as protected. All subscribers are unprotected by default. * Protected subscribers can only be unsubscribed using the unsubscribe method returned by this method. * Returns the unsubscribe callback function. * * @param subscriptionCb A function that runs on every update. * @param subscriptionOptions Additional information and notification condition. * @returns Unsubscribe callback function. */ subscribe(subscriptionCb: SubscriptionCb, subscriptionOptions?: SubscriptionOptions): UnsubscribeCb; /** Remove all unprotected subscribers */ unsubscribeAll(): void; /** * Returns count of all active subscribers. * @returns number. */ getActiveSubscribersCount(): number; /** * Flexible state update. * @param updateCb Callback for state updates. */ update(updateCb: UpdateCb, currentState?: T): void; /** * Unwrap a proxy object to a regular JavaScript object * @returns unwrapped state */ unwrap(): T; /** * Dispatch an async action * @param action An async action. It accepts a stateManager object, * which is used to access the current state. * @returns Promise. */ asyncAction(action: (stateManager: StatemanjsAPI) => Promise): Promise; /** * Create a computed state for a state property. * @param selectorFn A function that returns a value of a state property. * @returns A computed state. */ createSelector(selectorFn: (state: T) => E): StatemanjsComputedAPI; } interface StatemanjsComputedAPI { /** Get current state */ get(): T; /** * The method of subscribing to the status change. * Accepts a callback function (subscription callback), * which will be called at each update, and a subscription options object. * In the options, you can specify information about the subscription, * as well as specify the condition under which the subscriber will be notified * and mark the subscriber as protected. All subscribers are unprotected by default. * Protected subscribers can only be unsubscribed using the unsubscribe method returned by this method. * Returns the unsubscribe callback function. * * @param subscriptionCb A function that runs on every update. * @param subscriptionOptions Additional information and notification condition. * @returns Unsubscribe callback function. */ subscribe(subscriptionCb: SubscriptionCb, subscriptionOptions?: SubscriptionOptions): UnsubscribeCb; /** Remove all unprotected subscribers */ unsubscribeAll(): void; /** * Returns count of all active subscribers. * @returns number. */ getActiveSubscribersCount(): number; /** * Unwrap a proxy object to a regular JavaScript object * @returns unwrapped state */ unwrap(): T; } /** * Make the react component observe the statemanjs. * In the options, you can specify information about the observer, * as well as specify the condition under which the observer will be notified. * * @param statemanjs Statemanjs to observe. * @param subscriptionOptions Additional information and notification condition. * @returns Reactive state. */ declare function useStatemanjs(statemanjs: StatemanjsAPI | StatemanjsComputedAPI, subscriptionOptions?: SubscriptionOptions): T; export { useStatemanjs };