Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reducer Factories and AOT #145

Closed
Renader opened this issue Jul 21, 2017 · 3 comments
Closed

Reducer Factories and AOT #145

Renader opened this issue Jul 21, 2017 · 3 comments

Comments

@Renader
Copy link

Renader commented Jul 21, 2017

Currently I'm experimenting with Reducer reuse (see #110). Now I've come across problems with Reducers and AOT.

Given example:

We have invoices and orders that both have an identical model. For this, we have a single reducer that is attached to both entities in the store. For sure we want to keep the Types. Therefore we need the tagged union types. But I want to reuse my reducer. So I introduced a scope field for all actions.

But with this setup I'm currently receiving ERROR in Error encountered resolving symbol values statically.. The problem causing part is the anonymous function in the Factory. If I extract it, everything is fine but i'd loose the option to have a parameterized reducer.

Is there a nicer way?

I want to write my reducer only once, reuse actions, keep typing.

Here i prepared a minimal code example:



interface State<T> {
  entities: T[];
}

const orderInitState:State<Order> = {
  entities: []
};

const invoiceInitState:State<Invoice> = {
  entities: []
};


abstract class MyActions implements Action {
  abstract scope: string;
}

const store = {
  orders: reducerFactory<Order>('orders'),
  invoices: reducerFactory<Invoice>('invoices')
};


function reducerFactory<T>(scope: string){

  return function actualReducer (state: State<T>, action: MyActions<T>) {
    if (action.scope !== scope) {
      return state;
    }
    switch (action.type) {

      case ADD:
        return addEntity<T>(state, action);

        [...]

      default:
        return state;

    }
  };
}
@victornoel
Copy link

Is it the same as #116?

@Renader
Copy link
Author

Renader commented Jul 21, 2017

It is indeed very similar. But the offered workaround doesn't work for me, also it looks to me as if the user gave up on the advantage of a typed reduce (which I presuppose).

I've already tried to generate a reducerToken, assign my reducer to it, provide it in app.module and pass it into the store module.
export const reducerToken = new InjectionToken<ActionReducerMap<RootState>>('Registered Reducers');
Object.assign(reducerToken, reducers);

  providers: [
    {
      provide: reducerToken,
      useValue: reducers
    }
  ],

StoreModule.forRoot(reducerToken),

@Renader
Copy link
Author

Renader commented Jul 21, 2017

My problem was with a call to createSeletor, what i could solve otherwise. Workaround is working! Closed.

@Renader Renader closed this as completed Jul 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants