|
1 | | -import {CollectionViewer, DataSource, MdPaginator, MdSort} from '@angular/material'; |
| 1 | +import {DataSource, MdPaginator, MdSort} from '@angular/material'; |
2 | 2 | import {Observable} from 'rxjs/Observable'; |
3 | 3 | import {PeopleDatabase, UserData} from './people-database'; |
4 | | -import {BehaviorSubject} from 'rxjs/BehaviorSubject'; |
5 | | -import 'rxjs/add/observable/combineLatest'; |
6 | 4 | import 'rxjs/add/observable/merge'; |
7 | 5 | import 'rxjs/add/operator/map'; |
8 | | -import 'rxjs/add/observable/merge'; |
9 | | -import 'rxjs/add/observable/combineLatest'; |
10 | | - |
11 | 6 |
|
12 | 7 | export class PersonDataSource extends DataSource<any> { |
13 | | - /** Data that should be displayed by the table. */ |
14 | | - _displayData = new BehaviorSubject<UserData[]>([]); |
15 | | - |
16 | | - /** Cached data provided by the display data. */ |
17 | | - _renderedData: any[] = []; |
18 | | - |
19 | 8 | constructor(private _peopleDatabase: PeopleDatabase, |
20 | 9 | private _paginator: MdPaginator, |
21 | 10 | private _sort: MdSort) { |
22 | 11 | super(); |
23 | | - |
24 | | - // Subscribe to paging, sorting, and database changes by clearing the cached data and |
25 | | - // determining the updated display data. |
26 | | - Observable.merge(this._paginator.page, |
27 | | - this._peopleDatabase.dataChange, |
28 | | - this._sort.mdSortChange).subscribe(() => { |
29 | | - this._renderedData = []; |
30 | | - this.updateDisplayData(); |
31 | | - }); |
32 | | - } |
33 | | - |
34 | | - connect(collectionViewer: CollectionViewer): Observable<UserData[]> { |
35 | | - this.updateDisplayData(); |
36 | | - |
37 | | - const streams = [collectionViewer.viewChange, this._displayData]; |
38 | | - return Observable.combineLatest(streams) |
39 | | - .map((results: [{start: number, end: number}, UserData[]]) => { |
40 | | - const [view, data] = results; |
41 | | - |
42 | | - // Set the rendered rows length to the virtual page size. Fill in the data provided |
43 | | - // from the index start until the end index or pagination size, whichever is smaller. |
44 | | - this._renderedData.length = data.length; |
45 | | - |
46 | | - const buffer = 20; |
47 | | - let rangeStart = Math.max(0, view.start - buffer); |
48 | | - let rangeEnd = Math.min(data.length, view.end + buffer); |
49 | | - |
50 | | - for (let i = rangeStart; i < rangeEnd; i++) { |
51 | | - this._renderedData[i] = data[i]; |
52 | | - } |
53 | | - |
54 | | - return this._renderedData; |
55 | | - }); |
56 | 12 | } |
57 | 13 |
|
58 | | - updateDisplayData() { |
59 | | - const data = this.getSortedData(); |
60 | | - |
61 | | - // Grab the page's slice of data. |
62 | | - const startIndex = this._paginator.pageIndex * this._paginator.pageSize; |
63 | | - const paginatedData = data.splice(startIndex, this._paginator.pageSize); |
64 | | - |
65 | | - this._displayData.next(paginatedData); |
| 14 | + connect(): Observable<UserData[]> { |
| 15 | + const displayDataChanges = [ |
| 16 | + this._paginator.page, |
| 17 | + this._sort.mdSortChange, |
| 18 | + this._peopleDatabase.dataChange |
| 19 | + ]; |
| 20 | + return Observable.merge(...displayDataChanges).map(() => { |
| 21 | + const data = this.getSortedData(); |
| 22 | + |
| 23 | + // Grab the page's slice of data. |
| 24 | + const startIndex = this._paginator.pageIndex * this._paginator.pageSize; |
| 25 | + return data.splice(startIndex, this._paginator.pageSize); |
| 26 | + }); |
66 | 27 | } |
67 | 28 |
|
68 | 29 | /** Returns a sorted copy of the database data. */ |
|
0 commit comments