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

Observable emits data only once #450

Closed
naivefun opened this issue Jul 26, 2017 · 6 comments
Closed

Observable emits data only once #450

naivefun opened this issue Jul 26, 2017 · 6 comments

Comments

@naivefun
Copy link

naivefun commented Jul 26, 2017

In the component the selector is like this:

    @select((state: IAppState) => {
        console.log('received selector', state);
        return state.solutionState.solutions;
    })
    public solutions$: Observable<Pageable<Solution>>;

The first epic will trigger the solutions$ to emit data, however if I click other pages and click back again the observable never emits data again. The log is like this:
screen shot 2017-07-26 at 11 55 18 am

The received selector log shows the behavior.

The reducer method should be alright (modified to force new object purposely):

        case SolutionActions.LOAD_SOLUTIONS_SUCCESS:
            return _.cloneDeep(mergeState(state, {
                solutions: _.cloneDeep(action.payload),
                loadingSolutions: false,
                date: Date.now()
            }));

I also tried to subscribe manually but no luck.

is it something to do with multi modules? any thoughts?

@naivefun
Copy link
Author

naivefun commented Jul 26, 2017

I notice that my 3 modules [app, dashboard, solution] (lazy loaded) all import StoreModule, which is created 3 times. I guess the reason is they all have individual store? How can I share the same store instance in this case?

I tried but failed with:

export class StoreModule {
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: StoreModule,
            providers: [
                StoreService,
                AppStore,
                {
                    provide: NgRedux,
                    useExisting: AppStore,
                },
            ],
        };
    }
}

And by extending RootState I confirm that the state is updated but RootState.dispatch is not called.

@tomacf
Copy link

tomacf commented Jul 26, 2017

If you have a main module on the top of your infrastructure, maybe importing the store only there would solve your issue

@e-schultz
Copy link
Member

You should only register NgRedux at the root AppModule level - when you specify it as a provider in a lazy loaded module - it will get it's own injector / instances, and since NgRedux relies on a static singleton internally so features like @select work - I could see this causing some weird behaviour.

The angular.io docs mention that providers that are registered at the root AppModule level will be available application wide.

Lazy-loaded modules and their components can inject AppModule services; they can't inject AppComponent services.

@naivefun
Copy link
Author

naivefun commented Jul 27, 2017

I actually tried not to import on lazy-loaded modules but the error is like this

Error: Dispatch failed: did you forget to configure your store? 
https://github.com/angular-redux/@angular-redux/core/blob/master/README.md#quick-start

The error is from

  dispatch: Dispatch<RootState> = (action: Action) => {
    assert(
      !!this._store,
      'Dispatch failed: did you forget to configure your store? ' +
      'https://github.com/angular-redux/@angular-redux/core/blob/master/' +
      'README.md#quick-start');

    if (!NgZone.isInAngularZone()) {
      return this.ngZone.run(() => this._store.dispatch(action));
    } else {
      return this._store.dispatch(action);
    }
  };

In the sub-modules, the this._store is null, not injected from root providers.

@gregkopp
Copy link

Do you have a github repo or a plunker to show your example? I'm involved in a couple multi module projects using the store and this is not an issue. I think it might be in your code or how your sub modules are set up. I am happy to lend a hand if you are willing and able to share a concrete example.

@naivefun
Copy link
Author

naivefun commented Aug 1, 2017

@gregkopp thanks a lot. However I end up with using ngrx/store so. Maybe I can reproduce it when I have more time.

@naivefun naivefun closed this as completed Aug 1, 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

4 participants