Platform: [Feature Request] Implement aggregate operator for aggregator pattern

Created on 17 Apr 2018  ·  5Comments  ·  Source: ngrx/platform

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] Feature request
[ ] Documentation issue or request

What is the current behavior?


Aggregator pattern requires a lot of boilerplate code.

Expected behavior:


Add aggregate operator to reduce boilerplate code.
https://blog.nrwl.io/ngrx-patterns-and-techniques-f46126e2b1e5

All 5 comments

This is not a pattern we are going to include in the core APIs

so how do we make a contiguous set of actions synchronous ?

this is not synchronous

switchMap((state: EStates) => [
new myActions.Find(),
new yourActions.Find(),
new statesActions.ChangeSuccess(state)
]),

I find it strange the lead tech on this would do a series of presentation abroad and article and the core team omit the whole idea totally

is there a solution to this ?

Im attempting to implement the suggested operator but was hoping to find <aggregate> operator in latest release.

Does the omission of this suggested operator mean its not a real solution ?

How can I solve making a sequence of actions in an effect contiguously synchronous?

thanks for any help

export class CorrelationParams {
    public correlationId?: string
    public parentActionType?: string
}

const correlationParams: CorrelationParams = {
    correlationId: new uuid()
}


import { filter, first, switchMap } from 'rxjs/operators'
import { forkJoin, Observable, OperatorFunction, race } from 'rxjs'
import { Action } from '@ngrx/store'
import { CorrelationParams } from './correlation-params'

export type AggregatableAction = Action & { correlationParams?: CorrelationParams }

export type FailActionForAggregation = Action & { error?: Error, correlationParams?: CorrelationParams }

export function aggregate<T extends AggregatableAction,
    TAction1 extends AggregatableAction,
    TAction2 extends AggregatableAction,
    TFailAction extends FailActionForAggregation>
(
    action1$: Observable<TAction1>,
    action2$: Observable<TAction2>,
    failAction$: Observable<TFailAction>
): OperatorFunction<T, [TAction1, TAction2]> {

    const filterAction = (sourceAction: AggregatableAction, t: AggregatableAction) =>
        t.correlationParams && sourceAction.correlationParams &&
        t.correlationParams.correlationId === sourceAction.correlationParams.correlationId &&
        t.correlationParams.parentActionType === sourceAction.type

    const getAggregatedActions = (sourceAction: AggregatableAction): Observable<[TAction1, TAction2]> => {
        let a1$ = action1$
            .pipe(
                filter(a => {
                    return filterAction(sourceAction, a)
                }),
                first()
            )
        let a2$ = action2$
            .pipe(
                filter(a => {
                    return filterAction(sourceAction, a)
                }),
                first()
            )

        let f$ = failAction$
            .pipe(
                filter(a => {
                    return filterAction(sourceAction, a)
                }),
                first(),
                switchMap(b => {
                    return Observable.throw(b.error)
                })
            )

        return race(forkJoin([a1$, a2$]), f$)
    }

    return (source: Observable<AggregatableAction>) => source.pipe(
        switchMap(sourceAction => getAggregatedActions(sourceAction))
    )
}

i dont understand why we dont have this feature as well.

i dont understand why we dont have this feature as well.

I don’t understand the need for aggregator....just add as many actions into the on method in the reducer (or switch case fall though in earlier ngrx) as you need and similarly in the ofType method for effects. Example above isn’t using actions as intended... _unique events that are dispatched from components and services_, but as commands.

See this talk on good action hygiene —> https://www.youtube.com/watch?v=22K-cftN-e0

Ask in the gitter channel if unsure.

@AdditionAddict you seem to not understand the purpose of the aggregate pattern, it wouldn't have been created if it didn't solve a particular issue.

Go through this presentation where Victor Savkin ex google developer who explains the usage.

https://youtu.be/FQ6fzkHvCEY?list=WL

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bhaidar picture bhaidar  ·  3Comments

dollyshah-02-zz picture dollyshah-02-zz  ·  3Comments

mappedinn picture mappedinn  ·  3Comments

ghost picture ghost  ·  3Comments

gperdomor picture gperdomor  ·  3Comments