Adrià Vilanova Martínez | 8300cc4 | 2024-05-11 12:42:52 +0200 | [diff] [blame] | 1 | import PartialOptionsWatcher from '../partialOptionsWatcher.js'; |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 2 | import MainWorldContentScriptBridgeServer from '../mainWorldContentScriptBridge/Server.js'; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 3 | |
| 4 | // Main World OptionsWatcher server (used in content scripts to be able to serve |
| 5 | // the options to Main World (MW) scripts). |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 6 | export default class MWOptionsWatcherServer extends |
| 7 | MainWorldContentScriptBridgeServer { |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 8 | constructor(CSTarget, MWTarget) { |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 9 | super(CSTarget, MWTarget); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 10 | this.optionsWatcher = null; |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 11 | this.setUpHandler(this.handleMessage); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 12 | } |
| 13 | |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 14 | handleMessage(uuid, action, request) { |
| 15 | if (action === 'setUp') { |
Adrià Vilanova Martínez | 8300cc4 | 2024-05-11 12:42:52 +0200 | [diff] [blame] | 16 | this.optionsWatcher = new PartialOptionsWatcher(request?.options); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 17 | return; |
| 18 | } |
| 19 | |
| 20 | if (!this.optionsWatcher) { |
| 21 | console.warn(`[MWOptionsWatcherServer] Action '${ |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 22 | action}' called before setting up options watcher.`); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 23 | return; |
| 24 | } |
| 25 | |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 26 | switch (action) { |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 27 | case 'getOption': |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 28 | this.optionsWatcher.getOption(request?.option).then(value => { |
| 29 | this._respond(uuid, value); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 30 | }); |
| 31 | return; |
| 32 | |
| 33 | case 'getOptions': |
| 34 | var promises = []; |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 35 | var options = request?.options ?? []; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 36 | for (const option of options) { |
| 37 | promises.push(this.optionsWatcher.getOption(option)); |
| 38 | } |
| 39 | Promise.all(promises).then(responses => { |
| 40 | const response = {}; |
| 41 | for (let i = 0; i < responses.length; i++) { |
| 42 | response[options[i]] = responses[i]; |
| 43 | } |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 44 | this._respond(uuid, response); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 45 | }); |
| 46 | return; |
| 47 | |
| 48 | case 'isEnabled': |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 49 | this.optionsWatcher.isEnabled(request?.option).then(value => { |
| 50 | this._respond(uuid, value); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 51 | }); |
| 52 | return; |
| 53 | |
| 54 | case 'areEnabled': |
| 55 | var promises = []; |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 56 | var options = request?.options ?? []; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 57 | for (const option of options) { |
| 58 | promises.push(this.optionsWatcher.isEnabled(option)); |
| 59 | } |
| 60 | Promise.all(promises).then(responses => { |
| 61 | const response = {}; |
| 62 | for (let i = 0; i < responses.length; i++) { |
| 63 | response[options[i]] = responses[i]; |
| 64 | } |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 65 | this._respond(uuid, response); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 66 | }); |
| 67 | return; |
| 68 | |
| 69 | default: |
Adrià Vilanova Martínez | 2548e44 | 2023-03-05 18:45:30 +0100 | [diff] [blame] | 70 | console.error( |
| 71 | `[MWOptionsWatcherServer] Invalid action received (${action})`); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 74 | } |