@obsidize/rx-map
Version:
ES6 Map with rxjs extensions for change detection
44 lines (43 loc) • 1.44 kB
JavaScript
import { map, filter, startWith } from 'rxjs/operators';
import { forKey, forKeyIn, pluckValue } from '../common/operators';
import { identity } from '../common/utility';
import { EntityMap } from './entity-map';
import { RxMap } from './rx-map';
/**
* Combinatory entity map that uses an RxMap as the store.
*/
export class RxEntityMap extends EntityMap {
constructor(selectKey, store) {
super(store, selectKey);
}
/**
* Generate an RxEntityMap instance with a standard (mutable) Map store.
*/
static mutable(selectKey) {
return new RxEntityMap(selectKey, RxMap.mutable());
}
/**
* Generate an RxEntityMap instance with an immutable backend store.
*/
static immutable(selectKey) {
return new RxEntityMap(selectKey, RxMap.immutable());
}
get allChanges() {
return this.store.allChanges;
}
get changes() {
return this.store.changes;
}
destroy() {
this.store.destroy();
}
watchOne(key) {
return this.changes.pipe(forKey(key), pluckValue(), startWith(this.getOne(key)), filter(identity));
}
watchMany(keys) {
return this.changes.pipe(forKeyIn(keys), map(() => this.getManyExisting(keys)), startWith(this.getManyExisting(keys)));
}
watchAll() {
return this.changes.pipe(map(() => this.values()), startWith(this.values()));
}
}