Last active
December 6, 2019 16:35
-
-
Save reed-lawrence/0d227eccc177305108778f3e1676c974 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface IAppStateService { | |
user: IUser; | |
onUserChanges: Subject<IUser>; | |
} | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class AppStateService implements IAppStateService { | |
constructor() { } | |
private _user: User = new User(); | |
private _userSub: Subscription; | |
onUserChanges = new Subject<User>(); | |
get user() { | |
return this._user; | |
} | |
set user(val: User) { | |
this._user = val; | |
if (this._userSub) { | |
this._userSub.unsubscribe(); | |
} | |
if (this._user) { | |
this._userSub = this._user.onChanges.subscribe(user => { | |
this.onUserChanges.next(user); | |
}); | |
} | |
this.onUserChanges.next(this._user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment