UNPKG

ngx-observe

Version:

Angular Structural Directive for Observables

132 lines (127 loc) 5.28 kB
import * as i0 from '@angular/core'; import { ɵstringify as _stringify, Input, Directive } from '@angular/core'; import { Subject, AsyncSubject } from 'rxjs'; import { concatMap, takeUntil } from 'rxjs/operators'; function assertTemplate(property, templateRef) { const isTemplateRefOrNull = !!(!templateRef || templateRef.createEmbeddedView); if (!isTemplateRefOrNull) { throw new Error(`${property} must be a TemplateRef, but received '${_stringify(templateRef)}'.`); } } class ObserveContext { constructor(value) { this.$implicit = value; this.ngxObserve = value; } } class ErrorContext { constructor(error) { this.$implicit = error; } } class NgxObserveDirective { constructor(view, changes, nextTemplateRef) { this.view = view; this.changes = changes; this.unsubscribe = new Subject(); this.init = new AsyncSubject(); this.nextTemplateRef = nextTemplateRef; } set ngxObserve(source) { if (this.source && source !== this.source) { this.unsubscribe.next(undefined); } if (source && source !== this.source) { if (this.beforeTemplateRef) { this.view.clear(); this.nextViewRef = undefined; this.errorViewRef = undefined; this.beforeViewRef = this.view.createEmbeddedView(this.beforeTemplateRef); } this.init.pipe(concatMap(() => { if (this.beforeTemplateRef) { this.view.clear(); this.nextViewRef = undefined; this.errorViewRef = undefined; this.beforeViewRef = this.view.createEmbeddedView(this.beforeTemplateRef); } return source; }), takeUntil(this.unsubscribe)).subscribe(value => { this.view.clear(); this.errorViewRef = undefined; this.beforeViewRef = undefined; this.nextViewRef = this.view.createEmbeddedView(this.nextTemplateRef, new ObserveContext(value)); this.changes.markForCheck(); }, error => { if (this.errorTemplateRef) { this.view.clear(); this.beforeViewRef = undefined; this.nextViewRef = undefined; this.errorViewRef = this.view.createEmbeddedView(this.errorTemplateRef, new ErrorContext(error)); this.changes.markForCheck(); } }); } this.source = source; } set ngxObserveError(ref) { assertTemplate('ngxObserveError', ref); this.errorTemplateRef = ref; if (this.errorViewRef) { this.view.clear(); this.errorViewRef = this.view.createEmbeddedView(this.errorTemplateRef, this.errorViewRef.context); } } set ngxObserveBefore(ref) { assertTemplate('ngxObserveBefore', ref); this.beforeTemplateRef = ref; if (this.beforeViewRef) { this.view.clear(); this.beforeViewRef = this.view.createEmbeddedView(this.beforeTemplateRef); } } set ngxObserveNext(ref) { assertTemplate('ngxObserveNext', ref); this.nextTemplateRef = ref; if (this.nextViewRef) { this.view.clear(); this.nextViewRef = this.view.createEmbeddedView(this.nextTemplateRef, this.nextViewRef.context); this.changes.markForCheck(); } } static ngTemplateContextGuard(dir, ctx) { return true; } ngOnDestroy() { this.unsubscribe.next(undefined); } ngOnInit() { this.init.next(undefined); this.init.complete(); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.7", ngImport: i0, type: NgxObserveDirective, deps: [{ token: i0.ViewContainerRef }, { token: i0.ChangeDetectorRef }, { token: i0.TemplateRef }], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.7", type: NgxObserveDirective, isStandalone: true, selector: "[ngxObserve]", inputs: { ngxObserve: "ngxObserve", ngxObserveError: "ngxObserveError", ngxObserveBefore: "ngxObserveBefore", ngxObserveNext: "ngxObserveNext" }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.7", ngImport: i0, type: NgxObserveDirective, decorators: [{ type: Directive, args: [{ selector: '[ngxObserve]', standalone: true }] }], ctorParameters: () => [{ type: i0.ViewContainerRef }, { type: i0.ChangeDetectorRef }, { type: i0.TemplateRef }], propDecorators: { ngxObserve: [{ type: Input }], ngxObserveError: [{ type: Input }], ngxObserveBefore: [{ type: Input }], ngxObserveNext: [{ type: Input }] } }); /* * Public API Surface of ngx-observe */ /** * Generated bundle index. Do not edit. */ export { ErrorContext, NgxObserveDirective, ObserveContext }; //# sourceMappingURL=ngx-observe.mjs.map