Last active
April 18, 2018 10:33
-
-
Save Stuff90/309fe147eb6ad02da44c8d21607336c4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Observable } from 'rxjs/Observable'; | |
import { DataSource } from '@angular/cdk/collections'; | |
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | |
export class EntityDataSource<T extends { id: string }> extends DataSource<T> { | |
private source: BehaviorSubject<T[]> = new BehaviorSubject<T[]>([]); | |
get data(): T[] { | |
return this.source.value; | |
} | |
addOne(item: T): void { | |
this.source.next([...this.data, item]); | |
} | |
addMany(items: T[]): void { | |
this.source.next([...this.data, ...items]); | |
} | |
remove(...ids: string[]): void { | |
const duplicatedData = [...this.data]; | |
this.source.next(duplicatedData.filter(item => !ids.includes(item.id))); | |
} | |
connect(): Observable<T[]> { | |
return this.source.asObservable(); | |
} | |
disconnect() { | |
this.source.complete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment