feat: add OptionsProvider

This class will replace the current PartialOptionsWatcher.

Change-Id: I09e5847e6d9d94362683629f5576e804eef20955
diff --git a/src/common/OptionsProvider.ts b/src/common/OptionsProvider.ts
new file mode 100644
index 0000000..36370a1
--- /dev/null
+++ b/src/common/OptionsProvider.ts
@@ -0,0 +1,43 @@
+import { Mutex, MutexInterface, withTimeout } from 'async-mutex';
+
+import { getOptions } from './optionsUtils';
+import { OptionCodename, OptionValues } from './optionsPrototype';
+
+export default class OptionsProvider {
+  private optionValues: OptionValues;
+  private isStale = true;
+  private mutex: MutexInterface = withTimeout(new Mutex(), 60 * 1000);
+
+  constructor() {
+    // 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((_, 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<O extends OptionCodename>(option: O): Promise<OptionValues[O]> {
+    // 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(async () => {
+      if (!this.isStale) return Promise.resolve(this.optionValues[option]);
+
+      this.optionValues = await getOptions();
+      this.isStale = false;
+      return this.optionValues[option];
+    });
+  }
+
+  // Returns a promise resolving to whether the |feature| is enabled.
+  async isEnabled(option: OptionCodename) {
+    const value = await this.getOption(option);
+    return value === true;
+  }
+}
diff --git a/src/common/architecture/dependenciesProvider/DependenciesProvider.ts b/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
index b6af2a1..105e6ac 100644
--- a/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
+++ b/src/common/architecture/dependenciesProvider/DependenciesProvider.ts
@@ -1,11 +1,14 @@
 import ExtraInfo from '../../../features/extraInfo/core';
 import AutoRefresh from '../../../features/autoRefresh/core/autoRefresh';
+import OptionsProvider from '../../OptionsProvider';
 
 export const AutoRefreshDependency = 'autoRefresh';
 export const ExtraInfoDependency = 'extraInfo';
+export const OptionsProviderDependency = 'optionsProvider';
 export const DependenciesToClass = {
   [AutoRefreshDependency]: AutoRefresh,
   [ExtraInfoDependency]: ExtraInfo,
+  [OptionsProviderDependency]: OptionsProvider,
 };
 
 interface OurWindow extends Window {
diff --git a/src/common/partialOptionsWatcher.js b/src/common/partialOptionsWatcher.js
index 3c6cec2..261a6d4 100644
--- a/src/common/partialOptionsWatcher.js
+++ b/src/common/partialOptionsWatcher.js
@@ -2,6 +2,9 @@
 
 import {getOptions} from './optionsUtils.js';
 
+/**
+ * @deprecated Use {@link OptionsProvider} instead.
+ */
 export default class PartialOptionsWatcher {
   constructor(options) {
     this.watchedOptions = options;
diff --git a/src/scripts/Scripts.ts b/src/scripts/Scripts.ts
index dca90d6..39dd9d6 100644
--- a/src/scripts/Scripts.ts
+++ b/src/scripts/Scripts.ts
@@ -2,12 +2,14 @@
 import ScriptFilterListProvider from '../common/architecture/scripts/ScriptFilterListProvider';
 import MWI18nServerScript from './mainWorldServers/MWI18nServerScript.script';
 import MWOptionsWatcherServerScript from './mainWorldServers/MWOptionsWatcherServerScript.script';
+import OptionsProviderSetUpScript from './optionsProvider/optionsProvider.script';
 import XHRInterceptorScript from './xhrInterceptor/xhrInterceptor.script';
 
 export default class StandaloneScripts extends ScriptFilterListProvider {
   private scripts: ConcreteScript[] = [
     MWI18nServerScript,
     MWOptionsWatcherServerScript,
+    OptionsProviderSetUpScript,
     XHRInterceptorScript,
   ];
   private initializedScripts: Script[];
diff --git a/src/scripts/optionsProvider/optionsProvider.script.ts b/src/scripts/optionsProvider/optionsProvider.script.ts
new file mode 100644
index 0000000..d468a06
--- /dev/null
+++ b/src/scripts/optionsProvider/optionsProvider.script.ts
@@ -0,0 +1,11 @@
+import { Dependency, OptionsProviderDependency } from "../../common/architecture/dependenciesProvider/DependenciesProvider";
+import { ScriptEnvironment, ScriptPage, ScriptRunPhase } from "../../common/architecture/scripts/Script";
+import SetUpDependenciesScript from "../../common/architecture/scripts/setUpDependencies/SetUpDependenciesScript";
+
+export default class OptionsProviderSetUpScript extends SetUpDependenciesScript {
+  public priority = 99;
+  public page = ScriptPage.CommunityConsole;
+  public environment = ScriptEnvironment.ContentScript;
+  public runPhase = ScriptRunPhase.Start;
+  public dependencies: Dependency[] = [OptionsProviderDependency];
+}