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

support meta reducer factory for feature #1414

Closed
itayod opened this issue Nov 11, 2018 · 8 comments · Fixed by #1445
Closed

support meta reducer factory for feature #1414

itayod opened this issue Nov 11, 2018 · 8 comments · Fixed by #1445

Comments

@itayod
Copy link
Contributor

itayod commented Nov 11, 2018

If accepted, I would be willing to submit a PR for this feature

[x ] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No

@brandonroberts
Copy link
Member

Will you explain further what you're asking for?

@itayod
Copy link
Contributor Author

itayod commented Nov 12, 2018

well,
I would like to be able to use meta reducer factory for feature.
meaning the following is currently working only for root and I need that to work also for feature:

import { MetaReducer, META_REDUCERS } from '@ngrx/store';
import { SomeService } from './some.service';
import * as fromRoot from './reducers';

export function getMetaReducers(some: SomeService): MetaReducer<fromRoot.State>[] {
  // return array of meta reducers;
}

@NgModule({
  providers: [
    {
      provide: META_REDUCERS,
      deps: [SomeService],
      useFactory: getMetaReducers,
    },
  ],
})
export class AppModule {}

Thanks a lot for the quick response.

@timdeschryver
Copy link
Member

As a workaround you could use:

StoreModule.forFeature('featureFoo', reducers, {
  metaReducers: [logger],
}),

@itayod
Copy link
Contributor Author

itayod commented Nov 18, 2018

What I am trying to do is saving parts of the store to the server.
Therefor I wanted to create a configurable meta reducer, pass him the keys that we want to save from the store and inject it the angular http client service and use it in every feature store.
In order to do so I need to be able to create a meta reducer with factory and provide it the http service and the keys.

right now this is my work around:

export function myMetaReducer(reducer: ActionReducer<any>): ActionReducer<any> {
  const keys = ['stuff.from.state', 'another.stuff.form.state', ...];
  return createMyGerenricReducer(key)(reducer);
}

...
StoreModule.forFeature('feature', reducers, {metaReducers: [myMetaReducer]}),



@timdeschryver
Copy link
Member

I'm happy you found a way to solve your problem.

Currently, the META_REDUCERS token is only used within forRoot(), I'm not sure if it would be possible to use the same token.

To go a bit further I also think it won't be possible, even if we introduce a new FEATURE_META_REDUCERS token to handle feature states. Because it's impossible (?) to bind the meta reducer to the right feature module when multiple feature states are being registered at once.

I'll leave this open in case someone has a solution. If you have one feel free to mention it here/create a PR.

@itayod
Copy link
Contributor Author

itayod commented Nov 21, 2018

Thanks for the reply,

as you can see my solution is not perfect yet because I still can't use angular's http service and currently I am using the browser's fetch Api.

Either way I see why you think it could be impossible to bind the meta reducer to the right feature with the way it works right now...

I will try look up to a solution in the code and make a PR if I find one :)

@itayod
Copy link
Contributor Author

itayod commented Nov 22, 2018

I think I made a working solution...
but I think I will need permissions in order to add my branch.

@timdeschryver
Copy link
Member

timdeschryver commented Nov 22, 2018

@itayod you can't push your branch into the ngrx repo.
You'll have to fork the ngrx repo, clone this repo, create a feature branch on your personal repo, push commits to this branch. For some help you can take a look at the contributing file. Once you're done, you can create a Pull Request starting from your feature branch intro the NgRx repo.

itayod pushed a commit to itayod/platform that referenced this issue Nov 24, 2018
itayod pushed a commit to itayod/platform that referenced this issue Nov 24, 2018
itayod pushed a commit to itayod/platform that referenced this issue Nov 25, 2018
itayod pushed a commit to itayod/platform that referenced this issue Nov 30, 2018
itayod pushed a commit to itayod/platform that referenced this issue Nov 30, 2018
itayod pushed a commit to itayod/platform that referenced this issue Dec 14, 2018
itayod pushed a commit to itayod/platform that referenced this issue Dec 14, 2018
itayod pushed a commit to itayod/platform that referenced this issue Dec 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants