Options
All
  • Public
  • Public/Protected
  • All
Menu

@fp51/store - v2.0.0

Index

Type aliases

Listener<State>: ({ oldState, newState, }: { newState: State; oldState: State }) => void

Type parameters

  • State

Type declaration

    • ({ oldState, newState, }: { newState: State; oldState: State }): void
    • Parameters

      • { oldState, newState, }: { newState: State; oldState: State }
        • newState: State
        • oldState: State

      Returns void

Reducer<State>: (state: State) => State

Type parameters

  • State

Type declaration

    • (state: State): State
    • Parameters

      • state: State

      Returns State

Store<State>: { apply: any; read: any; subscribe: any }

Type parameters

  • State

Type declaration

  • apply:function
    • apply(reducer: Reducer<State>): () => void
    • Apply a reducer on the state and update the store current state with the result

      example

      import * as Store from 'store';

      const store = Store.create(() => 2)();

      store.apply(state => state + 1)();

      store.read() // 3

      Parameters

      Returns () => void

        • (): void
        • Returns void

  • read:function
    • read(): State
    • Read the store current state

      example

      import * as Store from 'store';

      const store = Store.create(() => 2)();

      store.read() // 2

      Returns State

  • subscribe:function
    • Subscribe a listener to state changes

      The subscription returns a function to call to unsubscribe.

      example

      import * as Store from 'store';

      const store = Store.create(() => 2)();

      const subscribe = store.subscribe(() => console.log('hello world'));

      const unsubscribe = subscribe();

      store.apply(state => state + 1)();

      // hello world

      unsubscribe();

      store.apply(state => state + 1)();

      Parameters

      Returns () => Unsubscribe

Unsubscribe: () => void

Type declaration

    • (): void
    • Returns void

Functions

  • create<State>(setupInitialState: () => State): () => Store<State>
  • Store creation with a initialState

    example

    import * as Store from 'store';

    const createNumberStore = Store.create(() => 2);

    const store = createNumberStore();

    Type parameters

    • State

    Parameters

    • setupInitialState: () => State
        • (): State
        • Returns State

    Returns () => Store<State>

      • Store creation with a initialState

        example

        import * as Store from 'store';

        const createNumberStore = Store.create(() => 2);

        const store = createNumberStore();

        Returns Store<State>

  • liftAndApply<State, Fn>(fn: Fn, store: Store<State>): (...args: Parameters<Fn>) => () => void
  • Lift and apply a reducer factory, returning a function that, given some arguments, will apply the reducer on the state and update the store current state with the result

    example

    import * as Store from 'store';

    const store = Store.create(() => 2)();

    // instead of: const add = (n: number) => store.apply(state => state + n);

    // write: const add = Store.liftAndApply((n: number) => state => state + n, store);

    add(2)(); store.read() // 4

    Type parameters

    • State

    • Fn: (...args: any[]) => Reducer<State>

    Parameters

    • fn: Fn
    • store: Store<State>

    Returns (...args: Parameters<Fn>) => () => void

      • (...args: Parameters<Fn>): () => void
      • Lift and apply a reducer factory, returning a function that, given some arguments, will apply the reducer on the state and update the store current state with the result

        example

        import * as Store from 'store';

        const store = Store.create(() => 2)();

        // instead of: const add = (n: number) => store.apply(state => state + n);

        // write: const add = Store.liftAndApply((n: number) => state => state + n, store);

        add(2)(); store.read() // 4

        Parameters

        • Rest ...args: Parameters<Fn>

        Returns () => void

          • (): void
          • Returns void

Generated using TypeDoc