rxdeep
Version:
RxJS deep state management
31 lines • 1.25 kB
JavaScript
import { Subject, defer, merge } from 'rxjs';
import { map, filter } from 'rxjs/operators';
import { State } from './state';
import { reverse } from './reverse';
import { takeUntilCompletes } from './util/take-until-completes';
export class VerifiedState extends State {
constructor(state, verifier) {
super(state.value, defer(() => merge(state.downstream.pipe(takeUntilCompletes(this._bounce)), this._bounce.pipe(map(reverse), takeUntilCompletes(state.downstream)))), {
next: change => {
if (!verifier(change)) {
this._bounce.next(change);
}
else {
state.upstream.next(change);
}
},
error: err => state.upstream.error(err),
complete: () => this._bounce.complete(),
});
this.state = state;
this.verifier = verifier;
this._bounce = new Subject();
}
bounce() {
return merge(this.downstream.pipe(filter(() => false), takeUntilCompletes(this._bounce)), this._bounce.pipe(takeUntilCompletes(this.downstream)));
}
}
export function verified(state, verifier) {
return new VerifiedState(state, verifier);
}
//# sourceMappingURL=verified.js.map