Browse Source

remove subscription and use angular new features

pull/19401/head
Sinan997 3 years ago
parent
commit
3156c1a7d2
  1. 41
      npm/ng-packs/packages/theme-shared/src/lib/directives/ngx-datatable-list.directive.ts

41
npm/ng-packs/packages/theme-shared/src/lib/directives/ngx-datatable-list.directive.ts

@ -2,22 +2,20 @@ import { ListService, LocalizationService } from '@abp/ng.core';
import { import {
ChangeDetectorRef, ChangeDetectorRef,
Directive, Directive,
Inject,
Input, Input,
OnChanges, OnChanges,
OnDestroy,
OnInit, OnInit,
Optional,
DoCheck, DoCheck,
SimpleChanges, SimpleChanges,
inject,
DestroyRef
} from '@angular/core'; } from '@angular/core';
import { DatatableComponent } from '@swimlane/ngx-datatable'; import { DatatableComponent } from '@swimlane/ngx-datatable';
import { Subscription } from 'rxjs';
import { import {
defaultNgxDatatableMessages, defaultNgxDatatableMessages,
NgxDatatableMessages,
NGX_DATATABLE_MESSAGES, NGX_DATATABLE_MESSAGES,
} from '../tokens/ngx-datatable-messages.token'; } from '../tokens/ngx-datatable-messages.token';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
@Directive({ @Directive({
// eslint-disable-next-line @angular-eslint/directive-selector // eslint-disable-next-line @angular-eslint/directive-selector
@ -25,18 +23,16 @@ import {
standalone: true, standalone: true,
exportAs: 'ngxDatatableList', exportAs: 'ngxDatatableList',
}) })
export class NgxDatatableListDirective implements OnChanges, OnDestroy, OnInit, DoCheck { export class NgxDatatableListDirective implements OnChanges, OnInit, DoCheck {
private subscription = new Subscription();
private querySubscription = new Subscription();
@Input() list!: ListService; @Input() list!: ListService;
constructor( private readonly table = inject(DatatableComponent);
private table: DatatableComponent, private readonly cdRef = inject(ChangeDetectorRef);
private cdRef: ChangeDetectorRef, private readonly destroyRef = inject(DestroyRef);
private localizationService: LocalizationService, private readonly localizationService = inject(LocalizationService);
@Optional() @Inject(NGX_DATATABLE_MESSAGES) private ngxDatatableMessages: NgxDatatableMessages, private readonly ngxDatatableMessages = inject(NGX_DATATABLE_MESSAGES, { optional: true });
) {
constructor() {
this.setInitialValues(); this.setInitialValues();
} }
@ -59,11 +55,6 @@ export class NgxDatatableListDirective implements OnChanges, OnDestroy, OnInit,
this.table.offset = page; this.table.offset = page;
} }
ngOnDestroy() {
this.subscription.unsubscribe();
this.querySubscription.unsubscribe();
}
private setInitialValues() { private setInitialValues() {
this.table.externalPaging = true; this.table.externalPaging = true;
this.table.externalSorting = true; this.table.externalSorting = true;
@ -79,7 +70,7 @@ export class NgxDatatableListDirective implements OnChanges, OnDestroy, OnInit,
} }
private subscribeToSort() { private subscribeToSort() {
const sub = this.table.sort.subscribe(({ sorts: [{ prop, dir }] }) => { this.table.sort.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(({ sorts: [{ prop, dir }] }) => {
if (prop === this.list.sortKey && this.list.sortOrder === 'desc') { if (prop === this.list.sortKey && this.list.sortOrder === 'desc') {
this.list.sortKey = ''; this.list.sortKey = '';
this.list.sortOrder = ''; this.list.sortOrder = '';
@ -90,20 +81,16 @@ export class NgxDatatableListDirective implements OnChanges, OnDestroy, OnInit,
this.list.sortOrder = dir; this.list.sortOrder = dir;
} }
}); });
this.subscription.add(sub);
} }
private subscribeToPage() { private subscribeToPage() {
const sub = this.table.page.subscribe(({ offset }) => { this.table.page.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(({ offset }) => {
this.setTablePage(offset); this.setTablePage(offset);
}); });
this.subscription.add(sub);
} }
private subscribeToQuery() { private subscribeToQuery() {
if (!this.querySubscription.closed) this.querySubscription.unsubscribe(); this.list.query$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
this.querySubscription = this.list.query$.subscribe(() => {
const offset = this.list.page; const offset = this.list.page;
if (this.table.offset !== offset) this.table.offset = offset; if (this.table.offset !== offset) this.table.offset = offset;
}); });

Loading…
Cancel
Save