@laserware/stasis
    Preparing search index...

    Interface ListenerEffectAPI<State, DispatchType, ExtraArgument>

    interface ListenerEffectAPI<
        State,
        DispatchType extends Dispatch,
        ExtraArgument = unknown,
    > {
        cancel: () => void;
        cancelActiveListeners: () => void;
        condition: ConditionFunction<State>;
        getOriginalState: () => State;
        signal: AbortSignal;
        take: TakePattern<State>;
        throwIfCancelled: () => void;
        delay(timeoutMs: number): Promise<void>;
        fork<T>(
            executor: ForkedTaskExecutor<T>,
            options?: ForkOptions,
        ): ForkedTask<T>;
        pause<M>(promise: Promise<M>): Promise<M>;
        subscribe(): void;
        unsubscribe(): void;
    }

    Type Parameters

    • State
    • DispatchType extends Dispatch
    • ExtraArgument = unknown
    Index

    Properties

    cancel: () => void

    Cancels the instance of this listener that made this call.

    cancelActiveListeners: () => void

    Cancels all other running instances of this same listener except for the one that made this call.

    condition: ConditionFunction<State>

    Returns a promise that resolves when the input predicate returns true or rejects if the listener has been cancelled or is completed.

    The return value is true if the predicate succeeds or false if a timeout is provided and expires first.

    const updateBy = createAction<number>('counter/updateBy');

    middleware.startListening({
    actionCreator: updateBy,
    async effect(_, { condition }) {
    // wait at most 3s for `updateBy` actions.
    if(await condition(updateBy.match, 3_000)) {
    // `updateBy` has been dispatched twice in less than 3s.
    }
    }
    })
    getOriginalState: () => State

    Returns the store state as it existed when the action was originally dispatched, before the reducers ran.

    This function can only be invoked synchronously, it throws error otherwise.

    middleware.startListening({
    predicate: () => true,
    async effect(_, { getOriginalState }) {
    getOriginalState(); // sync: OK!

    setTimeout(getOriginalState, 0); // async: throws Error

    await Promise().resolve();

    getOriginalState() // async: throws Error
    }
    })
    signal: AbortSignal

    An abort signal whose aborted property is set to true if the listener execution is either aborted or completed.

    take: TakePattern<State>

    Returns a promise that resolves when the input predicate returns true or rejects if the listener has been cancelled or is completed.

    The return value is the [action, currentState, previousState] combination that the predicate saw as arguments.

    The promise resolves to null if a timeout is provided and expires first,

    const updateBy = createAction<number>('counter/updateBy');

    middleware.startListening({
    actionCreator: updateBy,
    async effect(_, { take }) {
    const [{ payload }] = await take(updateBy.match);
    console.log(payload); // logs 5;
    }
    })

    store.dispatch(updateBy(5));
    throwIfCancelled: () => void

    Throws a TaskAbortError if this listener has been cancelled

    Methods

    • Returns a promise that resolves after timeoutMs or rejects if the listener has been cancelled or is completed.

      Parameters

      • timeoutMs: number

      Returns Promise<void>

    • Returns a promise that resolves when waitFor resolves or rejects if the listener has been cancelled or is completed.

      Type Parameters

      • M

      Parameters

      • promise: Promise<M>

      Returns Promise<M>

    • It will subscribe a listener if it was previously removed, noop otherwise.

      Returns void

    • Removes the listener entry from the middleware and prevent future instances of the listener from running.

      It does not cancel any active instances.

      Returns void