-
Hello i have a question about createNotifier. I have an app that has a component called DataList in this component in use rxjs to make backend calls. I also have a child component of the DataList component called AddData where i add a new entry to the db. I then need to make a refetch in DataList to refresh the data. I was using computedAsync with a repeat operator and a subject to trigger the refetch from AddData component but i decided to start using a notifier to rereun computedAsync. The thing is i put the createNotifier in the service so it can be used by both DataList and AddData components DataList would listen and AddData would notify when a new entry was added. Is this considered bad practice? Right now it looks something like this for DataListComponent: @Component
export class DataListComponent{
user = input.required();
dataService = inject(DataService);
userData = computedAsync(()=> {
this.dataService.refetchNotifier.listen();
return this.#fetchData(this.user().id); //function that makes the call to backend
})
} DataService: @Injectable
export class DataService {
readonly #http = inject(HttpClient);
refetchNotifier = createNotifier();
fetchUserData(userId: number): Observable<Data[]> {
return this.#http.get<Data[]>(`${apiDataPath}/${userId}`);
}
addData(userId: number, data: Data): Observable<Data[]> {
return this.#http.post<Data[]>(`${apiDataPath}/${userId}`, data);
}
} AddDataComponent: @Component
export class AddDataComponent{
user = input.required();
dataService = inject(DataService);
data: Data; //Data comes from user input
addData(userId){
this.dataService.addData(this.user().id);
this.dataService.refetchNotifier.notify();
}
} This is not my actual code and is just provided to show what im talking about. Sorry if my explanation was a bit rough. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Hello @jurck222 @sysmat Btw, it's been 3 weeks since this was opened. What has worked well for you, and what not? |
Beta Was this translation helpful? Give feedback.
Hello @jurck222 @sysmat
Personally, I think both ways are fine. I'm doing both and haven't decided yet myself to only keep one or another. Do whatever works for you.
Btw, it's been 3 weeks since this was opened. What has worked well for you, and what not?