From 7035cc8bf76a8a4296b9a8c6edd4137461c9fdcb Mon Sep 17 00:00:00 2001 From: bnymncoskuner Date: Tue, 30 Mar 2021 18:27:14 +0300 Subject: [PATCH 1/3] feat: improve page render strategy interface --- .../page/src/page-part.directive.ts | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/npm/ng-packs/packages/components/page/src/page-part.directive.ts b/npm/ng-packs/packages/components/page/src/page-part.directive.ts index 5d2273f26b..74ad59c26c 100644 --- a/npm/ng-packs/packages/components/page/src/page-part.directive.ts +++ b/npm/ng-packs/packages/components/page/src/page-part.directive.ts @@ -13,12 +13,13 @@ import { SimpleChanges, SimpleChange, } from '@angular/core'; +import { Observable, Subscription, of } from 'rxjs'; export interface PageRenderStrategy { - shouldRender(type: string); - onInit?(type: string, injector: Injector, context?: any); - onDestroy?(type: string, injector?: Injector, context?: any); - onContextUpdate?(change: SimpleChange); + shouldRender(type?: string): boolean | Observable; + onInit?(type?: string, injector?: Injector, context?: any): void; + onDestroy?(type: string, injector?: Injector, context?: any): void; + onContextUpdate?(change?: SimpleChange): void; } export const PAGE_RENDER_STRATEGY = new InjectionToken('PAGE_RENDER_STRATEGY'); @@ -27,11 +28,15 @@ export const PAGE_RENDER_STRATEGY = new InjectionToken('PAGE export class PagePartDirective implements OnInit, OnDestroy, OnChanges { hasRendered = false; type: string; + subscription: Subscription; + @Input('abpPagePartContext') context: any; @Input() set abpPagePart(type: string) { this.type = type; - const shouldRender = this.shouldRender(type); + this.createRenderStream(type); + } + render = (shouldRender: boolean) => { if (shouldRender && !this.hasRendered) { this.viewContainer.createEmbeddedView(this.templateRef); this.hasRendered = true; @@ -39,9 +44,7 @@ export class PagePartDirective implements OnInit, OnDestroy, OnChanges { this.viewContainer.clear(); this.hasRendered = false; } - } - - @Input('abpPagePartContext') context: any; + }; constructor( private templateRef: TemplateRef, @@ -66,12 +69,27 @@ export class PagePartDirective implements OnInit, OnDestroy, OnChanges { if (this.renderLogic?.onDestroy) { this.renderLogic.onDestroy(this.type, this.injector, this.context); } + + this.clearSubscription(); } shouldRender(type: string) { if (this.renderLogic) { - return this.renderLogic.shouldRender(type); + const willRender = this.renderLogic.shouldRender(type); + return willRender instanceof Observable ? willRender : of(willRender); + } + return of(true); + } + + protected createRenderStream(type: string) { + this.clearSubscription(); + + this.shouldRender(type).subscribe(this.render); + } + + protected clearSubscription() { + if (this.subscription) { + this.subscription.unsubscribe(); } - return true; } } From ff0e79425bf6c31721346f6036011713f730a561 Mon Sep 17 00:00:00 2001 From: bnymncoskuner Date: Tue, 30 Mar 2021 18:34:32 +0300 Subject: [PATCH 2/3] fix: store subscription of shouldRender --- .../packages/components/page/src/page-part.directive.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/components/page/src/page-part.directive.ts b/npm/ng-packs/packages/components/page/src/page-part.directive.ts index 74ad59c26c..3e59b7aee2 100644 --- a/npm/ng-packs/packages/components/page/src/page-part.directive.ts +++ b/npm/ng-packs/packages/components/page/src/page-part.directive.ts @@ -18,7 +18,7 @@ import { Observable, Subscription, of } from 'rxjs'; export interface PageRenderStrategy { shouldRender(type?: string): boolean | Observable; onInit?(type?: string, injector?: Injector, context?: any): void; - onDestroy?(type: string, injector?: Injector, context?: any): void; + onDestroy?(type?: string, injector?: Injector, context?: any): void; onContextUpdate?(change?: SimpleChange): void; } @@ -84,7 +84,7 @@ export class PagePartDirective implements OnInit, OnDestroy, OnChanges { protected createRenderStream(type: string) { this.clearSubscription(); - this.shouldRender(type).subscribe(this.render); + this.subscription = this.shouldRender(type).subscribe(this.render); } protected clearSubscription() { From 218e3c1b3ec1ff0042a14c90f41f3604e68890c5 Mon Sep 17 00:00:00 2001 From: bnymncoskuner Date: Tue, 30 Mar 2021 18:35:12 +0300 Subject: [PATCH 3/3] refactor: clear sub first on destroy for page-part --- .../packages/components/page/src/page-part.directive.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/components/page/src/page-part.directive.ts b/npm/ng-packs/packages/components/page/src/page-part.directive.ts index 3e59b7aee2..c228189e0f 100644 --- a/npm/ng-packs/packages/components/page/src/page-part.directive.ts +++ b/npm/ng-packs/packages/components/page/src/page-part.directive.ts @@ -66,11 +66,11 @@ export class PagePartDirective implements OnInit, OnDestroy, OnChanges { } ngOnDestroy() { + this.clearSubscription(); + if (this.renderLogic?.onDestroy) { this.renderLogic.onDestroy(this.type, this.injector, this.context); } - - this.clearSubscription(); } shouldRender(type: string) {