Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 1 | import { Mutex, MutexInterface, withTimeout } from 'async-mutex'; |
| 2 | |
Adrià Vilanova Martínez | 7c941c7 | 2024-10-26 20:23:34 +0200 | [diff] [blame] | 3 | import { getOptions } from '../../../common/options/optionsUtils'; |
| 4 | import { OptionCodename, OptionsValues } from '../../../common/options/optionsPrototype'; |
| 5 | import { OptionsConfiguration } from '../../../common/options/OptionsConfiguration'; |
Adrià Vilanova Martínez | 6ecaa0d | 2024-10-26 17:04:32 +0200 | [diff] [blame] | 6 | import { |
| 7 | OptionsChangeListener, |
| 8 | OptionsProviderPort, |
Adrià Vilanova Martínez | 7c941c7 | 2024-10-26 20:23:34 +0200 | [diff] [blame] | 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 | c8628b1 | 2024-11-10 22:29:45 +0100 | [diff] [blame] | 58 | this.isSetUp = true; |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 59 | this.listenForStorageChanges(); |
| 60 | this.updateValues(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Sets up a listener to update the current cached configuration when there |
| 65 | * are changes to the underlying storage where options are persisted. |
| 66 | * |
| 67 | * We could try only doing this only when we're sure it has changed, but |
| 68 | * there are many factors (if the user has changed it manually, if a kill |
| 69 | * switch was activated, etc.) so we do it every time there is any change in |
| 70 | * the underlying storage. |
| 71 | */ |
| 72 | private listenForStorageChanges() { |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 73 | chrome.storage.onChanged.addListener((_, areaName) => { |
| 74 | if (areaName !== 'sync') return; |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 75 | console.debug('[OptionsProvider] Retrieving updated options.'); |
| 76 | this.updateValues(); |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 77 | }); |
| 78 | } |
| 79 | |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 80 | private async updateValues() { |
| 81 | await this.mutex.runExclusive(async () => { |
| 82 | await this.nonSafeUpdateValues(); |
| 83 | }, kWritePriority); |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Adrià Vilanova Martínez | ad69676 | 2024-05-25 19:32:36 +0200 | [diff] [blame] | 86 | private async nonSafeUpdateValues() { |
| 87 | const previousConfiguration = this.optionsConfiguration; |
| 88 | const currentOptionsValues = await getOptions(null); |
| 89 | this.optionsConfiguration = new OptionsConfiguration(currentOptionsValues); |
| 90 | |
| 91 | this.notifyListenersIfApplicable(previousConfiguration); |
| 92 | } |
| 93 | |
| 94 | private async notifyListenersIfApplicable( |
| 95 | previousOptionsConfiguration: OptionsConfiguration, |
| 96 | ) { |
| 97 | if ( |
| 98 | !previousOptionsConfiguration || |
| 99 | this.optionsConfiguration.isEqualTo(previousOptionsConfiguration) |
| 100 | ) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | for (const listener of this.listeners) { |
| 105 | listener(previousOptionsConfiguration, this.optionsConfiguration); |
| 106 | } |
| 107 | } |
Adrià Vilanova Martínez | f7e8685 | 2024-05-11 14:16:38 +0200 | [diff] [blame] | 108 | } |