ngrx / store

RxJS powered state management for Angular applications, inspired by Redux
MIT License
3.9k stars 311 forks source link

How can I build a Service observing one Store and dispatching to another one? #374

Closed matheo closed 7 years ago

matheo commented 7 years ago

Help please! I have no clue why I cannot dispatch on my service as I do on another of my components that dispatch anything:

import { InputState, ResultState, ResultAction } from '../states';

@Injectable()
export class CalculationsService {
  private subscription;

  constructor(
    private _store: Store<InputState>
  ) {
    this.subscription = this._store
      .select('input')
      .subscribe((state: InputState) => {
        this.validateInput(state)
      });
  }

  validateInput(input: InputState): void {
    if (input['mode'] && input[input['mode']]) {
      this._store.dispatch( new ResultAction(input['mode']) );
    }
  }
}

ResultAction implements Action and receives the payload with a fixed type.

If I comment out this._store.dispatch my Service resolves succesfully, if not, the Service is not instanced and a Component cannot resolve it as parameter:

Uncaught Error: Can't resolve all parameters for InitialStepComponent: (?).

no weird stuff, just the dispatch line is screwing me :(

export class InitialStepComponent extends WizardStepComponent {

  constructor(
    private calc: CalculationsService
  ) {
    super();
  }
}

If this._store is Store<InputState> it can dispatch ResultStates right? I'm using the latest @angular/cli with ng serve

fxck commented 7 years ago

Look at @ngrx/effects.

matheo commented 7 years ago

@fxck Thanks, checking it out :)