UNPKG

ng-config-cat

Version:

An Angular service for ConfigCat

48 lines 1.83 kB
import { Directive, TemplateRef, ViewContainerRef, } from '@angular/core'; import { Subject } from 'rxjs'; import { map, takeUntil } from 'rxjs/operators'; import { NgConfigCatService } from '../services/ng-config-cat.service'; export class NgConfigCatFeatureBaseDirective { constructor(viewContainerRef, templateRef, ngConfigCatService) { this.viewContainerRef = viewContainerRef; this.templateRef = templateRef; this.ngConfigCatService = ngConfigCatService; this.destroy$ = new Subject(); this.viewRef = null; } ngOnInit() { if (!this.featureName) { throw new Error('Attribute `ngConfigCatFeatureEnabled` should not be null or empty'); } this.render(); } ngOnDestroy() { this.destroy$.next(); } render() { this.ngConfigCatService.getValue(this.featureName, this.defaultValue, this.user).pipe(takeUntil(this.destroy$), map((isFeatureEnabled) => isFeatureEnabled === this.shouldFeatureBeEnabled)).subscribe((isVisible) => { if (isVisible) { this.viewRef = this.viewContainerRef.createEmbeddedView(this.templateRef); this.viewRef.markForCheck(); } else { this.viewContainerRef.clear(); if (this.viewRef) { this.viewRef.destroy(); this.viewRef = null; } } }); } } NgConfigCatFeatureBaseDirective.decorators = [ { type: Directive, args: [{ selector: '[ngConfigCatFeatureBase]' },] } ]; NgConfigCatFeatureBaseDirective.ctorParameters = () => [ { type: ViewContainerRef }, { type: TemplateRef }, { type: NgConfigCatService } ]; //# sourceMappingURL=ng-config-cat-feature-base.directive.js.map