Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 1 | import { Mutex, MutexInterface, withTimeout } from 'async-mutex'; |
| 2 | |
| 3 | import { getOptions } from './optionsUtils'; |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 4 | import { OptionCodename, OptionsValues } from './optionsPrototype'; |
| 5 | import { OptionsConfiguration } from './OptionsConfiguration'; |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 6 | |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 7 | // Prioritize reads before writes. |
| 8 | const kReadPriority = 10; |
| 9 | const kWritePriority = 0; |
| 10 | |
| 11 | /** |
| 12 | * Class which provides option values and a way to listen to option changes. |
| 13 | */ |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 14 | export default class OptionsProvider { |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 15 | private optionsConfiguration: OptionsConfiguration; |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 16 | private mutex: MutexInterface = withTimeout(new Mutex(), 60 * 1000); |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 17 | private listeners: Set<OptionsChangeListener> = new Set(); |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 18 | |
| 19 | constructor() { |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 20 | this.listenForStorageChanges(); |
| 21 | this.updateValues(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Sets up a listener to update the current cached configuration when there |
| 26 | * are changes to the underlying storage where options are persisted. |
| 27 | * |
| 28 | * We could try only doing this only when we're sure it has changed, but |
| 29 | * there are many factors (if the user has changed it manually, if a kill |
| 30 | * switch was activated, etc.) so we do it every time there is any change in |
| 31 | * the underlying storage. |
| 32 | */ |
| 33 | private listenForStorageChanges() { |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 34 | chrome.storage.onChanged.addListener((_, areaName) => { |
| 35 | if (areaName !== 'sync') return; |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 36 | console.debug('[OptionsProvider] Retrieving updated options.'); |
| 37 | this.updateValues(); |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 38 | }); |
| 39 | } |
| 40 | |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 41 | private async updateValues() { |
| 42 | await this.mutex.runExclusive(async () => { |
| 43 | await this.nonSafeUpdateValues(); |
| 44 | }, kWritePriority); |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 47 | private async nonSafeUpdateValues() { |
| 48 | const previousConfiguration = this.optionsConfiguration; |
| 49 | const currentOptionsValues = await getOptions(null); |
| 50 | this.optionsConfiguration = new OptionsConfiguration(currentOptionsValues); |
| 51 | |
| 52 | this.notifyListenersIfApplicable(previousConfiguration); |
| 53 | } |
| 54 | |
| 55 | private async notifyListenersIfApplicable( |
| 56 | previousOptionsConfiguration: OptionsConfiguration, |
| 57 | ) { |
| 58 | if ( |
| 59 | !previousOptionsConfiguration || |
| 60 | this.optionsConfiguration.isEqualTo(previousOptionsConfiguration) |
| 61 | ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | for (const listener of this.listeners) { |
| 66 | listener(previousOptionsConfiguration, this.optionsConfiguration); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the value of option |option|. |
| 72 | */ |
| 73 | async getOptionValue<O extends OptionCodename>( |
| 74 | option: O, |
| 75 | ): Promise<OptionsValues[O]> { |
| 76 | return this.mutex.runExclusive( |
| 77 | () => this.optionsConfiguration.getOptionValue(option), |
| 78 | kReadPriority, |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns whether |feature| is enabled. |
| 84 | */ |
| 85 | async isEnabled(option: OptionCodename): Promise<boolean> { |
| 86 | return this.mutex.runExclusive( |
| 87 | () => this.optionsConfiguration.isEnabled(option), |
| 88 | kReadPriority, |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | async getOptionsValues(): Promise<OptionsValues> { |
| 93 | return this.mutex.runExclusive( |
| 94 | () => this.optionsConfiguration.optionsValues, |
| 95 | kReadPriority, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Adds a listener for changes in the options configuration. |
| 101 | */ |
| 102 | addListener(listener: OptionsChangeListener) { |
| 103 | this.listeners.add(listener); |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 104 | } |
| 105 | } |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 106 | |
| 107 | export type OptionsChangeListener = ( |
| 108 | previousOptionValues: OptionsConfiguration, |
| 109 | currentOptionValues: OptionsConfiguration, |
| 110 | ) => void; |