I have a Map that I'd like to expose to several consumers. The map contains a list of Files and their upload status:
export interface FileProgress {
file: File;
sent: boolean;
}
So this Map is stored within a BehaviorSubject that I will expose using the .asObservable() method:
private readonly fileProgressSubject: BehaviorSubject<Map<string, FileProgress>>;
My question is, what's the best way to perform updates to the underlying data structure?
If the BehaviorSubject contained a simple type, say mySubject: BehaviorSubject<boolean>, I would simply call mySubject.next(true) and voila I've updated the value.
But updating the Map within my fileProgressSubject is more hacky. I need to access the map and then perform changes on it, so this would work but it just feels wrong:
const temp = fileProgressSubject.getValue();
temp.delete(someFileKey); // EXAMPLE OPERATION - DELETION
fileProgressSubject.next(temp);
Is this the "correct" way of making updates to my underlying Map? I've read that calling .getValue() on a Subject is using reactive constructs imperatively, and probably means that you're not doing something right.
My other thought, since this BehaviorSubject will be exposed as an observable, would be this even though I can see that the Typescript compiler won't allow it:
const fileObservable = fileProgressSubject.asObservable();
fileObservable.pipe(
map(fileProgressMap => fileProgressMap.delete(someFileName)), // DELETE A FILE FROM THE MAP
).subscribe(fileProgressSubject);
So, basically, pipe the current value of the underlying data structure through the observable, make a change to it, and then stuff the new Map into the subject. But again, this doesn't seem to be possible, not sure why.
So, what is the best way to handle situations like this?