1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

SwitchMap is never ran in effect

Discussão em 'Angular' iniciado por tal, Setembro 25, 2024.

  1. tal

    tal Guest

    I have a common functionality to get items from server.

    export const getAllEffect = <TEntity, TData>(args: InputArgs<TEntity, TData>) => {
    const {
    actions$,
    entity,
    subExists$,
    getAllApi,
    collectionIdFn,
    } = getParams(args)

    const getAllTrigger$ = actions$.pipe(
    ofActionAndPayload<GetAllPayload<TData>>(AsyncActions.GetAll),
    ofEntity(entity));

    // Force triggers ignore subscriptions for collection
    const forceTriggers$ = getAllTrigger$.pipe(filter(({ force }) => force))
    const softTriggers$ = getAllTrigger$.pipe(filter(({ force }) => force))
    const softTriggersFilteredBySub$ = softTriggers$.pipe(filterSubExistsWithCancellation(subExists$))

    // Emits load action when all filters are passed otherwise returns undefined (to cancel request)
    const trigger$ = merge(forceTriggers$, softTriggersFilteredBySub$)
    const cancelAction$ = of(new LoadActions.SetLoading(entity, false))
    const preLoadAction$ = of(new LoadActions.SetLoading(entity, true))
    const getPostLoadActions = (trigger: GetAllPayload<TData>) => (entities: TEntity[]): Action[] => {

    return [
    new CollectionActions.AddAll(collectionIdFn(trigger.data), entities),
    new LoadActions.SetLoaded(entity, true),
    new LoadActions.SetLoading(entity, false),
    ]
    }

    const getFailedActions = (trigger: GetAllPayload<TData>) => (response: HttpErrorResponse): Action[] => {
    return [
    new AsyncActions.GetAllFailed({ entity, response, data: trigger.data, count: trigger.count }),
    new LoadActions.SetLoading(entity, false),
    ]
    }

    return trigger$.pipe(
    switchMap((trigger) => {
    debugger;
    // Don't do any loading + cancel prev request if present
    if (!trigger) return cancelAction$
    return concat(
    preLoadAction$,
    getAllApi(trigger.data).pipe(
    retry(apiRetryCount),
    switchMap(getPostLoadActions(trigger)),
    catchError(getFailedActions(trigger)),
    ),
    )
    }),
    )
    }


    The trigger merges all effects to be executed. The effect is ran, I can debug it. but I never get to the switchMap part where debugger is written. I don't know why.

    Continue reading...

Compartilhe esta Página