-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement SubscriptionService instead of takeUntilDestroy
resolves #4818
- Loading branch information
1 parent
8c50cdb
commit 5ea30d4
Showing
15 changed files
with
175 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 17 additions & 11 deletions
28
npm/ng-packs/packages/core/src/lib/components/replaceable-route-container.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,45 @@ | ||
import { Component, OnDestroy, OnInit, Type } from '@angular/core'; | ||
import { Component, OnInit, Type } from '@angular/core'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
import { Store } from '@ngxs/store'; | ||
import { distinctUntilChanged } from 'rxjs/operators'; | ||
import { ABP } from '../models/common'; | ||
import { ReplaceableComponents } from '../models/replaceable-components'; | ||
import { SubscriptionService } from '../services/subscription.service'; | ||
import { ReplaceableComponentsState } from '../states/replaceable-components.state'; | ||
import { takeUntilDestroy } from '../utils/rxjs-utils'; | ||
|
||
@Component({ | ||
selector: 'abp-replaceable-route-container', | ||
template: ` | ||
<ng-container *ngComponentOutlet="externalComponent || defaultComponent"></ng-container> | ||
`, | ||
providers: [SubscriptionService], | ||
}) | ||
export class ReplaceableRouteContainerComponent implements OnInit, OnDestroy { | ||
export class ReplaceableRouteContainerComponent implements OnInit { | ||
defaultComponent: Type<any>; | ||
|
||
componentKey: string; | ||
|
||
externalComponent: Type<any>; | ||
|
||
constructor(private route: ActivatedRoute, private store: Store) {} | ||
constructor( | ||
private route: ActivatedRoute, | ||
private store: Store, | ||
private subscription: SubscriptionService, | ||
) {} | ||
|
||
ngOnInit() { | ||
this.defaultComponent = this.route.snapshot.data.replaceableComponent.defaultComponent; | ||
this.componentKey = (this.route.snapshot.data | ||
.replaceableComponent as ReplaceableComponents.RouteData).key; | ||
|
||
this.store | ||
const component$ = this.store | ||
.select(ReplaceableComponentsState.getComponent(this.componentKey)) | ||
.pipe(takeUntilDestroy(this), distinctUntilChanged()) | ||
.subscribe((res = {} as ReplaceableComponents.ReplaceableComponent) => { | ||
.pipe(distinctUntilChanged()); | ||
|
||
this.subscription.addOne( | ||
component$, | ||
(res = {} as ReplaceableComponents.ReplaceableComponent) => { | ||
this.externalComponent = res.component; | ||
}); | ||
}, | ||
); | ||
} | ||
|
||
ngOnDestroy() {} | ||
} |
29 changes: 10 additions & 19 deletions
29
npm/ng-packs/packages/core/src/lib/directives/debounce.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,25 @@ | ||
import { | ||
Directive, | ||
ElementRef, | ||
EventEmitter, | ||
Input, | ||
OnDestroy, | ||
OnInit, | ||
Output, | ||
} from '@angular/core'; | ||
import { takeUntilDestroy } from '../utils/rxjs-utils'; | ||
import { Directive, ElementRef, EventEmitter, Input, OnInit, Output } from '@angular/core'; | ||
import { fromEvent } from 'rxjs'; | ||
import { debounceTime } from 'rxjs/operators'; | ||
import { SubscriptionService } from '../services/subscription.service'; | ||
|
||
@Directive({ | ||
// tslint:disable-next-line: directive-selector | ||
selector: '[input.debounce]', | ||
providers: [SubscriptionService], | ||
}) | ||
export class InputEventDebounceDirective implements OnInit, OnDestroy { | ||
export class InputEventDebounceDirective implements OnInit { | ||
@Input() debounce = 300; | ||
|
||
@Output('input.debounce') readonly debounceEvent = new EventEmitter<Event>(); | ||
|
||
constructor(private el: ElementRef) {} | ||
constructor(private el: ElementRef, private subscription: SubscriptionService) {} | ||
|
||
ngOnInit(): void { | ||
fromEvent(this.el.nativeElement, 'input') | ||
.pipe(debounceTime(this.debounce), takeUntilDestroy(this)) | ||
.subscribe((event: Event) => { | ||
this.debounceEvent.emit(event); | ||
}); | ||
} | ||
const input$ = fromEvent(this.el.nativeElement, 'input').pipe(debounceTime(this.debounce)); | ||
|
||
ngOnDestroy(): void {} | ||
this.subscription.addOne(input$, (event: Event) => { | ||
this.debounceEvent.emit(event); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.