refactor: rename OptionsWatcher to PartialOptionsWatcher
We will create a new OptionsProvider class which will eventually replace
PartialOptionsWatcher. This CL renames the latter class so we don't
confuse it with the new one.
Change-Id: Ie28feeb8e2d78e84e812bf937f0ccb08569c8c9c
diff --git a/src/common/partialOptionsWatcher.js b/src/common/partialOptionsWatcher.js
new file mode 100644
index 0000000..3c6cec2
--- /dev/null
+++ b/src/common/partialOptionsWatcher.js
@@ -0,0 +1,49 @@
+import {Mutex, withTimeout} from 'async-mutex';
+
+import {getOptions} from './optionsUtils.js';
+
+export default class PartialOptionsWatcher {
+ constructor(options) {
+ this.watchedOptions = options;
+ this.options = [];
+ for (let o of options) this.options[o] = false;
+ this.isStale = true;
+ this.mutex = withTimeout(new Mutex(), 60 * 1000);
+
+ // If the extension settings change, set the current cached value as stale.
+ // We could try only doing this only when we're sure it has changed, but
+ // there are many factors (if the user has changed it manually, if a kill
+ // switch was activated, etc.) so we'll do it every time.
+ chrome.storage.onChanged.addListener((changes, areaName) => {
+ if (areaName !== 'sync') return;
+ console.debug('[optionsWatcher] Marking options as stale.');
+ this.isStale = true;
+ });
+ }
+
+ // Returns a promise resolving to the value of option |option|.
+ getOption(option) {
+ if (!this.watchedOptions.includes(option))
+ return Promise.reject(new Error(
+ '[optionsWatcher] We\'re not watching option ' + option + '.'));
+
+ // When the cached value is marked as stale, it might be possible that there
+ // is a flood of calls to isEnabled(), which in turn causes a flood of calls
+ // to getOptions() because it takes some time for it to be marked as not
+ // stale. Thus, hiding the logic behind a mutex fixes this.
+ return this.mutex.runExclusive(() => {
+ if (!this.isStale) return Promise.resolve(this.options[option]);
+
+ return getOptions(this.watchedOptions).then(options => {
+ this.options = options;
+ this.isStale = false;
+ return this.options[option];
+ });
+ });
+ }
+
+ // Returns a promise resolving to whether the |feature| is enabled.
+ isEnabled(feature) {
+ return this.getOption(feature).then(option => option === true);
+ }
+}