import { Injectable , isDevMode } from '@angular/core' ; import { Session } from './session' ; import { Router } from '@angular/router' ; import { ConfigsService } from '../common/services/configs.service' ; import { Subscription , Observable } from 'rxjs' ; import { first } from 'rxjs/operators' ; @ Injectable ( ) export class FeaturesService { protected _warnedCache : { [ key ] : number ; } = { } ; private features = { } ; constructor ( private session , private router , private configs ) { } has ( feature ) { const features = this . configs . get ( 'features' ) ; if ( ! features ) return false ; if ( ! feature ) { throw new Error ( `Invalid feature ID ${ feature } not found` ) ; } if ( feature . indexOf ( '!' ) === 0 ) { return ! this . has ( feature . substring ( 1 ) ) ; } if ( typeof features [ feature ] === 'undefined' ) { if ( isDevMode ( ) && ! this . _hasWarned ( feature ) ) { this . _warnedCache [ feature ] = Date . now ( ) ; } return false ; } if ( features [ feature ] === 'admin' && this . session . isAdmin ( ) ) { return true ; } if ( features [ feature ] === 'canary' && this . session . getLoggedInUser ( ) . canary ) { return true ; } return features [ feature ] === true ; } async check ( feature , { redirectTo } = { } ) { if ( feature . indexOf ( '!' ) === 0 ) { throw new Error ( 'Cannot negate feature when using check()' ) ; } const has = this . has ( feature ) ; if ( ! has && redirectTo ) { this . router . navigate ( redirectTo , { replaceUrl : true } ) ; } return has ; } protected _hasWarned ( feature ) { if ( ! this . _warnedCache [ feature ] ) { return false ; } return this . _warnedCache [ feature ] + 5000 < Date . now ( ) ; } static _ ( session , router , configs ) { return new FeaturesService ( session , router , configs ) ; } }