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