Skip to content

Commit

Permalink
Simplify subscription handling #9844
Browse files Browse the repository at this point in the history
Because `NaturalAbstractPanel.initPanel()` is only ever called at most 1
time per instance of a component, we don't need to handle previous
subscriptions
  • Loading branch information
PowerKiKi committed Mar 24, 2024
1 parent 13f015e commit 748e2ec
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
20 changes: 8 additions & 12 deletions projects/natural/src/lib/modules/panels/abstract-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Directive, HostBinding, HostListener} from '@angular/core';
import {NaturalAbstractController} from '../../classes/abstract-controller';
import {NaturalPanelsService} from './panels.service';
import {NaturalPanelData} from './types';
import {Observable, Subscription, takeUntil} from 'rxjs';
import {Observable, takeUntil} from 'rxjs';

@Directive()
export class NaturalAbstractPanel extends NaturalAbstractController {
Expand All @@ -11,7 +11,6 @@ export class NaturalAbstractPanel extends NaturalAbstractController {
* When loading a component from a panel opening (dialog), receives the data provided by the service
*/
public data: any = {};
#modelSubscription: Subscription | null = null;

/**
* Bind isFrontPanel style class on root component
Expand Down Expand Up @@ -51,16 +50,13 @@ export class NaturalAbstractPanel extends NaturalAbstractController {
if (this.panelData?.data) {
if (this.panelData.data.model instanceof Observable) {
// Subscribe to model to know when Apollo cache is changed, so we can reflect it into `data.model`
this.#modelSubscription?.unsubscribe();
this.#modelSubscription = this.panelData.data.model
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(model => {
this.data = {
...this.data,
...this.panelData?.data,
model: model,
};
});
this.panelData.data.model.pipe(takeUntil(this.ngUnsubscribe)).subscribe(model => {
this.data = {
...this.data,
...this.panelData?.data,
model: model,
};
});
} else {
this.data = {
...this.data,
Expand Down
7 changes: 7 additions & 0 deletions src/app/panels-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ export const panelsRoutes: NaturalPanelsRouterRule[] = [
component: AnyComponent,
resolve: {foo: resolveMy},
},
{
path: 'modelPanel/:param',
component: AnyComponent,
resolve: {
model: route => inject(ItemService).resolve(route.params.param),
},
},
];
1 change: 1 addition & 0 deletions src/app/panels/panels.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ <h1 class="mat-headline-4">NaturalPanelsService</h1>
<div fxLayout="row" fxLayoutGap="10px">
<a mat-flat-button routerLink="panel/1">Panel 1</a>
<a mat-flat-button routerLink="panel/1/panel/2">Panel 1 & 2</a>
<a mat-flat-button routerLink="modelPanel/1">Panel with observable model</a>
<a mat-flat-button routerLink="invalid-url">invalid url</a>
</div>
<router-outlet />
1 change: 1 addition & 0 deletions src/app/shared/components/any/any.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div fxLayout="row" fxLayoutGap="10px">
<a [routerLink]="['panel', '2']" mat-flat-button>Panel 2 (dynamic)</a>
<a mat-flat-button routerLink="panel/3">Panel 3 (static)</a>
<a mat-flat-button routerLink="modelPanel/3">Panel with observable model (static)</a>
<a [routerLink]="['invalid-url']" mat-flat-button>invalid url</a>
</div>

Expand Down

0 comments on commit 748e2ec

Please sign in to comment.