I have a unique service dedicated to managing app configurations
class Configuration {
get setting() {
return dataStore.fetchSetting();
}
set setting(value) {
dataStore.saveSetting(value);
}
}
This configuration is linked to components through configuration.setting
.
Due to the potentially expensive and asynchronous nature of retrieving settings from the storage, I am considering updating the getter/setter with an RxJS subject that can handle updates and reads from the storage efficiently.
My refactored version looks like this:
class Configuration {
constructor() {
this.settingSubject = new ReplaySubject(1);
settingSubject.subscribe((value) => {
dataStore.saveSetting(value);
});
this.setting$ = this.settingSubject
.map(() => dataStore.fetchSetting())
.publishReplay(1).refCount();
}
Now, I can use it as configuration.setting$ | async
and update it using
configuration.settingSubject.next(newSetting)
. This method seems to have effectively cached the costly storage retrievals.
However, two issues have surfaced.
The first problem is that both the settingSubject
subject and setting$
observable need to be publicly accessible for this operation, even though a subject was intended to act as both an observable and an observer.
Is there a way to convert setting$
into a single Subject property within the Configuration
service, allowing it to be subscribed using subscribe(...)
and updated with next(...)
?
The second concern is about the synchronous behavior of the code.
How can we address this issue when dealing with promises returned by both datastore.retrieve
and datastore.save
?