import { Injectable } from '@angular/core' ; import { BehaviorSubject , from , Observable } from 'rxjs' ; import { tap } from 'rxjs/operators' ; import { Context } from './context' ; import { ContextFactory } from './context.factory' ; @ Injectable ( { providedIn : 'root' } ) export class ContextService { private readonly context$ = new BehaviorSubject < Context > ( undefined ) ; constructor ( private readonly contextFactory ) { } public init ( defaultContext ) { return from ( this . contextFactory . create ( defaultContext ) . pipe ( tap ( createdContext => this . context$ . next ( createdContext ) ) ) . toPromise ( ) ) ; } public get ( ) { return { ... this . context$ . getValue ( ) } ; } public update ( context ) { this . context$ . next ( context ) ; } }