Verified Commit 901df0d6 authored by Merzough Münker's avatar Merzough Münker
Browse files

fix: support data source trackBy

parent 2206cd04
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -68,6 +68,9 @@ export class DataSourceDirective<Data = any>

  public connection$!: Observable<Data>;

  @Input('rxapDataSourceTrackBy')
  public trackBy?: (data: Data) => any;

  /**
   * @deprecated removed
   * @protected
@@ -113,18 +116,32 @@ export class DataSourceDirective<Data = any>
  }

  public embedTemplate(response: any) {
    const context = {
      $implicit:   response,
      connection$: this.connection$
    };
    if (this.embeddedViewRef && !this.hasChanged(this.embeddedViewRef.context.$implicit, response)) {
      this.embeddedViewRef.context = context;
    } else {
      this.embeddedViewRef?.destroy();
      this.embeddedViewRef = this.viewContainerRef.createEmbeddedView(
        this.template,
        {
          $implicit:   response,
        connection$: this.connection$,
          connection$: this.connection$
        }
      );
    }
    this.embedded.emit(response);
    this.cdr.detectChanges();
  }

  private hasChanged(last: Data, current: Data): boolean {
    const lastKey    = this.trackBy ? this.trackBy(last) : last;
    const currentKey = this.trackBy ? this.trackBy(current) : current;
    return lastKey !== currentKey;
  }

  public ngOnDestroy(): void {
    this.dataSource?.disconnect(this.viewer);
    this.subscription.unsubscribe();