-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk): add directive to render list with a delay (#1795)
- Loading branch information
Showing
8 changed files
with
213 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
Directive, | ||
Inject, | ||
Input, | ||
NgZone, | ||
OnChanges, | ||
OnDestroy, | ||
TemplateRef, | ||
ViewContainerRef, | ||
} from '@angular/core'; | ||
import {tuiZonefree} from '@taiga-ui/cdk/observables'; | ||
import {from, of, Subject} from 'rxjs'; | ||
import {concatMap, delay, takeUntil} from 'rxjs/operators'; | ||
|
||
@Directive({selector: '[tuiForAsync][tuiForAsyncOf]'}) | ||
export class TuiForAsyncDirective<T> implements OnChanges, OnDestroy { | ||
private readonly destroy$ = new Subject<void>(); | ||
|
||
@Input() | ||
tuiForAsyncOf: readonly T[] | undefined | null; | ||
|
||
@Input() | ||
tuiForAsyncTimeout = 10; | ||
|
||
constructor( | ||
@Inject(ViewContainerRef) private readonly view: ViewContainerRef, | ||
@Inject(TemplateRef) private readonly template: TemplateRef<unknown>, | ||
@Inject(NgZone) private readonly ngZone: NgZone, | ||
) {} | ||
|
||
ngOnChanges(): void { | ||
this.clearViewForOldNodes(); | ||
this.createAsyncViewForNewNodes(); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.clearViewForOldNodes(); | ||
this.destroy$.complete(); | ||
} | ||
|
||
private createAsyncViewForNewNodes(): void { | ||
from(this.tuiForAsyncOf || []) | ||
.pipe( | ||
tuiZonefree(this.ngZone), | ||
concatMap(item => of(item).pipe(delay(this.tuiForAsyncTimeout))), | ||
takeUntil(this.destroy$), | ||
) | ||
.subscribe(item => | ||
this.view | ||
.createEmbeddedView(this.template, {$implicit: item}) | ||
.detectChanges(), | ||
); | ||
} | ||
|
||
private clearViewForOldNodes(): void { | ||
this.destroy$.next(); | ||
this.view.clear(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {NgModule} from '@angular/core'; | ||
|
||
import {TuiForAsyncDirective} from './for-async.directive'; | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
@NgModule({ | ||
declarations: [TuiForAsyncDirective], | ||
exports: [TuiForAsyncDirective], | ||
}) | ||
export class TuiForAsyncModule {} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './for-async.directive'; | ||
export * from './for-async.module'; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"ngPackage": { | ||
"lib": { | ||
"entryFile": "index.ts" | ||
} | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
projects/cdk/directives/for-async/test/for.directive.spec.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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import {Component, ElementRef} from '@angular/core'; | ||
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; | ||
import {TuiForAsyncModule} from '@taiga-ui/cdk'; | ||
import {configureTestSuite} from '@taiga-ui/testing'; | ||
import {Subject} from 'rxjs'; | ||
|
||
describe('TuiForAsync directive', () => { | ||
@Component({ | ||
template: ` | ||
<div *tuiForAsync="let item of items$ | async; timeout: 1000"> | ||
{{ item }} | ||
</div> | ||
`, | ||
}) | ||
class TestComponent { | ||
readonly items$: Subject<string[] | null | undefined> = new Subject(); | ||
|
||
constructor(readonly elementRef: ElementRef<HTMLElement>) {} | ||
} | ||
|
||
let fixture: ComponentFixture<TestComponent>; | ||
let testComponent: TestComponent; | ||
|
||
configureTestSuite(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [TuiForAsyncModule], | ||
declarations: [TestComponent], | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TestComponent); | ||
testComponent = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('when tuiForAsync is falsy', () => { | ||
expect(text()).toBe(''); | ||
}); | ||
|
||
it('when tuiForAsync is empty shows empty content', () => { | ||
testComponent.items$.next([]); | ||
fixture.detectChanges(); | ||
|
||
expect(text()).toBe(''); | ||
}); | ||
|
||
it('when regular tuiForAsync', fakeAsync(() => { | ||
testComponent.items$.next(['1', '2', '3']); | ||
fixture.detectChanges(); | ||
|
||
expect(text()).toBe(''); | ||
|
||
tick(1000); | ||
|
||
expect(text()).toBe('1'); | ||
|
||
tick(1000); | ||
|
||
expect(text()).toBe('1 2'); | ||
|
||
tick(1000); | ||
|
||
expect(text()).toBe('1 2 3'); | ||
})); | ||
|
||
it('tuiForAsync should be re-rerender content when source is changed', fakeAsync(() => { | ||
testComponent.items$.next(['1', '2']); | ||
fixture.detectChanges(); | ||
|
||
expect(text()).toBe(''); | ||
|
||
tick(2000); | ||
|
||
expect(text()).toBe('1 2'); | ||
|
||
tick(2000); | ||
|
||
testComponent.items$.next(['5', '6']); | ||
fixture.detectChanges(); | ||
|
||
expect(text()).toBe(''); | ||
|
||
tick(2000); | ||
|
||
expect(text()).toBe('5 6'); | ||
|
||
tick(2000); | ||
|
||
testComponent.items$.next(null); | ||
fixture.detectChanges(); | ||
|
||
expect(text()).toBe(''); | ||
})); | ||
|
||
it('prevent race condition', fakeAsync(() => { | ||
testComponent.items$.next(['1', '2', '3']); | ||
fixture.detectChanges(); | ||
|
||
expect(text()).toBe(''); | ||
|
||
// race condition | ||
setTimeout(() => { | ||
testComponent.items$.next(['5', '6']); | ||
fixture.detectChanges(); | ||
}, 1500); | ||
|
||
tick(1000); | ||
|
||
expect(text()).toBe('1'); | ||
|
||
tick(1000); | ||
|
||
expect(text()).toBe(''); | ||
|
||
tick(1000); | ||
|
||
expect(text()).toBe('5'); | ||
|
||
tick(1000); | ||
|
||
expect(text()).toBe('5 6'); | ||
})); | ||
|
||
function text(): string { | ||
return testComponent.elementRef.nativeElement.textContent?.trim() || ''; | ||
} | ||
}); |
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
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