@bemedev/rx-add-ons
Version:
A collection of RxJS operators and utilities to enhance reactive programming capabilities.
40 lines (37 loc) • 1.55 kB
JavaScript
import { Subject } from 'rxjs/internal/Subject';
import { EMPTY } from 'rxjs/internal/observable/empty';
import { scan } from 'rxjs/internal/operators/scan';
import { startWith } from 'rxjs/internal/operators/startWith';
import { switchMap } from 'rxjs/internal/operators/switchMap';
const createPausable = (source$, observer) => {
// Control Subject for start, stop, pause, and resume
const control$ = new Subject();
// State management for the observable
const controlled$ = control$.pipe(startWith('stop'), // Start in "stopped" state
scan((state, action) => {
if (action === 'start' && state !== 'running')
return 'running';
if (action === 'stop')
return 'stopped';
if (action === 'pause' && state === 'running')
return 'paused';
if (action === 'resume' && state === 'paused')
return 'running';
return state; // Ignore invalid transitions
}, 'stopped'), switchMap(state => {
if (state === 'running')
return source$; // Emit values when running
return EMPTY; // Emit nothing when paused or stopped
}));
// Subscribe to the controlled Observable
controlled$.subscribe(observer);
return {
start: () => control$.next('start'),
stop: () => control$.next('stop'),
pause: () => control$.next('pause'),
resume: () => control$.next('resume'),
command: (action) => control$.next(action),
};
};
export { createPausable };
//# sourceMappingURL=pausable.js.map