-1

I'm new to Angular, just some questions on rxjs.

Let's say I use a provider in the root module:

@NgModule({
    imports: [BrowserModule, FormsModule, ModelModule, MessageModule],
    declarations: [TableComponent, FormComponent, StatePipe],
    exports: [ModelModule, TableComponent, FormComponent],
    providers: [{
        provide: SHARED_STATE,
        deps: [MessageService, Model],
        useFactory: (messageService, model) => {
            let subject = new Subject<XXX>();
            subject.subscribe(funcA); 
            return subject;
        }
    }]
})
export class AppModule { }

then in the other component, I subscribe it again:

constructor( @Inject(SHARED_STATE) private stateEvents: Observable<SharedState>) {
   stateEvents.subscribe(funcB);
}

below is my questions:

  1. Is it OK and valid to subscribe multiple times?

  2. If that's ok and valid, funcA invoked before funcB, so is it possible to make funcB invoked before funcA? how can I make

1 Answers1

1

Disclaimer

The solution given in this post is not the unique way to achieve what you want. From the code provided in your post, we can't know if it's an error of conception and what is intended is right.

1. Is it OK and valid to subscribe multiple times?

Yes, it is the purpose of observables. You can have multiple Observers that will be notified.

2. If that's ok and valid, funcA invoked before funcB, so is it possible to make funcB invoked before funcA?

Usually we use pipe() operator to perform funcA before funcB but from the code you provided it seems that the function are not located in the same component. Two components seems to be notified from the subject and you want to bind the execution of the two in order. Be careful with this, that can be a misleading of conception. Here a solution if you are sure about your design.

There is no guarantee about the order of execution from what I see from RXJS documentation for two subscribers.

One way to achieve (maybe not the best way) what you want, ordering funcA and funcB is to have two Observables. For instance, the first one that you are using to fire funcA

let subject = new Subject<XXX>();
            subject.subscribe(funcA); 

And inside the funcA, you can have an EventEmitter that notifies Subscribers for the end of the operation :

funcAEventEmitter = new EventEmitter<any>();
funcAEventEmitter.subscribe(funcB);

At funcAEventEmitter emission, the funcB will be called.

funcA(){
//execution of the funcA execution
...
this.funcAEventEmitter.emit('funcA is done');
}

You need to see how to pass the funcAEventEmitter at the location needed maybe from @Input()(based on what you gave us as code).

Another tip

Don't use constructor to make subscriptions (or any methods). The purpose of constructor is for Dependencies injection, not to work on anything.

Use ngOnInit() to perform initialization of your component, like subscribe() For more information: Difference between Constructor and ngOnInit

Community
  • 1
  • 1
Axiome
  • 695
  • 5
  • 18