Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface ListenerEffectAPI<State, Dispatch, ExtraArgument>

Type Parameters

Hierarchy

Index

Properties

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.

Example

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.
}
}
})
dispatch: Dispatch
extra: ExtraArgument
signal: AbortSignal

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

see

https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

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,

Example

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));

Methods

  • cancelActiveListeners(): void
  • Cancels all other running instances of this same listener except for the one that made this call.

    Returns void

  • delay(timeoutMs: number): Promise<void>
  • Returns a promise that resolves after timeoutMs or rejects if the listener has been cancelled or is completed.

    Parameters

    • timeoutMs: number

    Returns Promise<void>

  • Queues in the next microtask the execution of a task.

    Type Parameters

    • T

    Parameters

    Returns ForkedTask<T>

  • getOriginalState(): State
  • Returns the store state as it existed when the action was originally dispatched, before the reducers ran.

    Synchronous invocation

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

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

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

    await Promise().resolve();

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

    Returns State

  • getState(): State
  • Returns State

  • pause<M>(promise: Promise<M>): Promise<M>
  • 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>

  • subscribe(): void
  • It will subscribe a listener if it was previously removed, noop otherwise.

    Returns void

  • unsubscribe(): 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

Generated using TypeDoc