I have the following code which I expect should emit for each member in the scheduleEntries
array, and each scheduleEntry
should repeat forever, and I’m trying to get the first emission of each array member to be skipped. How can I get it to work?
Right now it doesn’t emit at all with skip(1)
import { Observable, from, mergeMap, of, repeat, skip, timer } from 'rxjs';
class Schedule {
private scheduleEntries = [
{ value: true, countdown: 48395 },
{ value: true, countdown: 38395 },
{ value: false, countdown: 42394 },
{ value: true, countdown: 4835 },
];
private lockEmitter$: Observable<ScheduleEntry>;
constructor() {
this.lockEmitter$ = from(this.scheduleEntries).pipe(
skip(1), // skip the first emission for each entry
mergeMap(entry => of(entry).pipe(
repeat({ delay: () => this.delayedObservableFactory(entry) }),
)),
);
this.lockEmitter$.subscribe(console.log);
}
private delayedObservableFactory(entry: ScheduleEntry) {
return new Observable(observer => {
timer(entry.countdown).subscribe(() => observer.next({}));
});
}
}
interface ScheduleEntry {
value: boolean;
countdown: number;
}
2
Answers
Think you need to move skip before pipe?
Actually looking again skip is applying the whole array but you may need to skip each entry as your code comment suggests (missed that detail first time round)
When you use
from
, we requireconcatMap
to ensure the items are executed in order, please find below stackblitzstackblitz