I'm having a problem that I'm sure is entirely based on my (lack of) a complete understanding of Observables.
This is an Angular2 (v4.0.3) project using rx/js and Observables.
I have a state store that holds events in a state service:
// Observable string sources
private currentEventStore = new BehaviorSubject<string>("");
// Observable string streams
public currentEvent$ = this.currentEventStore.asObservable();
// Service message commands
setCurrentEvent(nextEvent: string) {
this.currentEventStore.next(nextEvent);
}
I subscribe to currentEvent$ in my components to listen for events, and then act on them.
this.stateSvc.currentEvent$
.subscribe(
currentEvent => {
this.currentEvent = currentEvent;
if (currentEvent != '') {
this.handleCurrentEvent(currentEvent);
}
});
The problem I'm having is that the store is accumulating all events and returning all of them each time, so that when I spawn a 'create' event, my subscribed function picks up the event and creates a record - perfect on the first pass, but if I spawn a second 'create' event, my subscribed function picks up 2 events, and creates 2 records, and on the next 'create' event it creates 3 records, and so on.
What I need to happen is for the event to be flushed out of the stream so that the currentEvent$ store only holds events that haven't been addressed.
Is there a way to flush the store? Or is there something I'm missing here in my expectations and implementation?