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