I have a project with this code that I inherited and for the life of me, I cannot understand what exactly is going on and there are some bugs with the teacher profile so I'm trying to sort out what the developer may have been trying to do. If you can please help review and provide any tips or issues that you see. I would like to make this code follow best practices and hope to document for the next guys how to do it.
So my questions are:
Does it make sense to create a new BehaviorSubject upon each call to the getTeacherProfile(), getTeacherProfileInfo(), getTeacherProfileByProfileId() because doesn't that create a new variable and any previous subscribe won't be notified?
Would the correct best pattern be to initialize once in constructor like
this.broadCast = new BehaviorSubject<TeacherProfileModel>();
and then in inside each method that wants to update, push, send any new data to call:
this.broadCast.next(data);
Instead of a subscribe inside the getter methods of Promise .then() , isn't it standard practice to make all the .subscribe((data) => { from inside the ngOnInit() one time for the component life cycle?
Also there is no ngOnDestroy() or unsubscribe, which I believe is standard practice when subscribing to any BehaviorSubject because if you do not it can create memory leaks and unexpected results?
Could it be caused by the order in which the methods are called by the related code that is making an inconsistent user experience from the dependent code that is subscribing to BehaviorSubjects which may be changed/reassigned and forgotten causing unexpected and unpredictable results?
Thanks for your help in advance!
CODE EXCERPTS
teacher.service.ts
async getTeacherProfile(): Promise<TeacherProfileModel> {
if (this.teacherProfileId > 0) {
var data = await this.crudService.get<TeacherProfileModel>("/tProfile/GetTeacherProfileData",
new HttpParams().set('teacherProfileId', this.teacherProfileId.toString())).toPromise();
this.broadCast = new BehaviorSubject<TeacherProfileModel>(data);
this.teacherProfile = data;
this.personalizedURL = this.teacherProfile.PersonalizedUrl;
}
else {
if (this.teacherProfileId === 0) {
this.initialiseTeacherProfile();
this.commonService.isDefaultImageFlag = true;
}
this.broadCast = new BehaviorSubject<TeacherProfileModel>(this.teacherProfileData);
}
return this.teacherProfile;
}
async getTeacherProfileInfo(url: string): Promise<TeacherProfileModel> {
var data = await this.crudService.get<TeacherProfileModel>("/teacherProfile/GetTeacherProfileInfo",
new HttpParams().set('personlizedUrl', url)).toPromise();
this.broadCast = new BehaviorSubject<TeacherProfileModel>(data);
this.teacherProfile = data;
this.personalizedURL = this.teacherProfile.PersonalizedUrl;
return this.teacherProfile;
}
async getTeacherProfileByProfileId(teacherProfileId:any): Promise<TeacherProfileModel> {
this.teacherProfileId = teacherProfileId;
this.imageUrl = localStorage.getItem("userImageUrl");
var data = await this.crudService.get<TeacherProfileModel>("/teacherProfile/GetTeacherProfileData",
new HttpParams().set('teacherProfileId', this.teacherProfileId.toString())).toPromise();
this.broadCast = new BehaviorSubject<TeacherProfileModel>(data);
this.teacherProfile = data;
this.personalizedURL = this.teacherProfile.PersonalizedUrl;
return this.teacherProfile;
}
profile.component.ts
ngOnInit() {
this.teacherService.getTeacherProfile().then((response: any) => {
this.teacherService.broadCast.subscribe(data => {
if(data !== undefined) {
//do something
}
});
});
}
teacher-profile-edit.component.ts
previewChanges() {
this.commonService.userProfilePic = localStorage.getItem('userImageUrl');
this.commonService.isStudentUser = false;
this.teacherService.broadCast.next(this.teacherProfile);
}
private getTeacherProfileData() {
this.teacherService.getTeacherProfile().then((response: any) => {
this.teacherService.broadCast.subscribe(data => {
if(data !== undefined) {
this.teacherProfile = data;
this.onProfileStatusChange(this.teacherProfile.IsProfileStatusPrivate);
this.shortIntroLength = this.introLength - data.ShortIntroduction.length;
if (data.TeacherQualifications.length === 0) {
this.addQualification();
}
} else {
var err = 'Error'; this.toastr.error('Oops! Something went wrong! '+err); throw err;
}
});
});
}