RxJs BehaviorSubject's next() method in service

心已入冬 提交于 2019-12-23 06:07:56

问题


I use the following approach in order to refresh Details page after a new record added. I also need to refresh after updating a record as well.

EventProxyService

export class EventProxyService {
    private eventTracker = new BehaviorSubject<any>();

    getEvent(): Observable<any> {
        return this.eventTracker.asObservable();
    }

    setEvent(param: any): void {
        this.eventTracker.next(param);
    }
}

CreateComponent:

export class CreateComponent implements OnInit {

    constructor(private eventProxyService: EventProxyService) { }

    create(param: any): void {
        //other staff related to creating record

        this.eventProxyService.setEvent(param);
    }
}

DetailsComponent:

export class DetailsComponent implements OnInit {

    subscription;

    constructor(private eventProxyService: EventProxyService) { }

    ngOnInit() {
        this.subscription = this.eventProxyService.getEvent().subscribe((param: any) => {
            this.refresh(param);
        );
    }

    refresh(param) {
        this.record = param; //update record via new one passed from service
    }

    ngOnDestroy(): void {
        this.subscription.unsubscribe();
    }
}

The approach works well but I am a little bit confused regarding to the following issues:

1) Because I also call this.eventProxyService.setEvent(param); in update() method like in create() method, would it be good idea to call this in my service level instead of component level?

2) Is there anything wrong when firing next() method in CreateComponent above?


回答1:


I think your design is pretty much fine with BehaviourSubject (at least in my prospective).

One thing I can advice is use BehaviourSubject in the root scope singleton service. (which i can't notice in your case)

what if I call the setEvent() in the create() method of my service (not this proxyService, I meant other service where I keep my CRUD methods)

If I Understood your question properly, Answer is NO.

Because calling a service method from another service is not Advisable, So keep things simple.In real world projects once the CRUD methods gets executed then we will try to refresh the data from the Component itself with the help of Service methods (but not in the service).

I call the next() via setEvent() method in the CRUD methods, after create, update, delete operations are completed. Is it a good approach, it is there a better way in real world examples e.g. calling next() in the service (CRUD) as I asked in the first question

Yes, it is a good approach.




回答2:


I personally think your current approach is totally valid. You could consider adding restriction rules, like you already mentioned, to just call setEvent from your service layer. This would improve the single responsibility of your component. Anyway I think this really depends on the size and the lifetime of your project. For beginning a project so would definitely do it the same way you did!



来源:https://stackoverflow.com/questions/57401705/rxjs-behaviorsubjects-next-method-in-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!