From e669c681e29e4b757cf7c593ad6878c985011827 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 25 Mar 2020 12:39:42 +0000 Subject: [PATCH 1/8] Fetch both config.json-s at the same time, first one fails 99% of the time Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/vector/getconfig.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/vector/getconfig.js b/src/vector/getconfig.js index 6fb74d38e..c0f81f6cc 100644 --- a/src/vector/getconfig.js +++ b/src/vector/getconfig.js @@ -21,15 +21,19 @@ import request from 'browser-request'; export async function getVectorConfig(relativeLocation) { if (relativeLocation === undefined) relativeLocation = ''; if (relativeLocation !== '' && !relativeLocation.endsWith('/')) relativeLocation += '/'; + + const specificConfigPromise = getConfig(`${relativeLocation}config.${document.domain}.json`); + const generalConfigPromise = getConfig(relativeLocation + "config.json"); + try { - const configJson = await getConfig(`${relativeLocation}config.${document.domain}.json`); + const configJson = await specificConfigPromise; // 404s succeed with an empty json config, so check that there are keys if (Object.keys(configJson).length === 0) { throw new Error(); // throw to enter the catch } return configJson; } catch (e) { - return await getConfig(relativeLocation + "config.json"); + return await generalConfigPromise; } } From a808d26764396e083b7ba0b98e9cde009dfc8c08 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 25 Mar 2020 12:41:33 +0000 Subject: [PATCH 2/8] Enable webpack reuseExistingChunk Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- webpack.config.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/webpack.config.js b/webpack.config.js index f54655b5b..b9713c05d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -56,6 +56,9 @@ module.exports = (env, argv) => { enforce: true, // Do not add `chunks: 'all'` here because you'll break the app entry point. }, + default: { + reuseExistingChunk: true, + }, }, }, From df2b966acde9cd7f83c35435c5d6a0a77b63014d Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 25 Mar 2020 13:53:28 +0000 Subject: [PATCH 3/8] move loadOlm to init.ts Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/@types/global.d.ts | 21 +++++++++++++++ src/vector/app.js | 42 +---------------------------- src/vector/init.ts | 60 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 src/@types/global.d.ts create mode 100644 src/vector/init.ts diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts new file mode 100644 index 000000000..7aec4de95 --- /dev/null +++ b/src/@types/global.d.ts @@ -0,0 +1,21 @@ +/* +Copyright 2020 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +interface Window { + Olm: { + init: () => Promise; + }; +} diff --git a/src/vector/app.js b/src/vector/app.js index 6a1635dd5..8bf735313 100644 --- a/src/vector/app.js +++ b/src/vector/app.js @@ -18,8 +18,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -import olmWasmPath from 'olm/olm.wasm'; - import React from 'react'; // add React and ReactPerf to the global namespace, to make them easier to // access via the console @@ -44,10 +42,9 @@ import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore"; import SdkConfig from "matrix-react-sdk/src/SdkConfig"; import {setTheme} from "matrix-react-sdk/src/theme"; -import Olm from 'olm'; - import CallHandler from 'matrix-react-sdk/src/CallHandler'; import {loadConfig, preparePlatform} from "./initial-load"; +import {loadOlm} from "./init"; let lastLocationHashSet = null; @@ -308,43 +305,6 @@ export async function loadApp() { } } -function loadOlm() { - /* Load Olm. We try the WebAssembly version first, and then the legacy, - * asm.js version if that fails. For this reason we need to wait for this - * to finish before continuing to load the rest of the app. In future - * we could somehow pass a promise down to react-sdk and have it wait on - * that so olm can be loading in parallel with the rest of the app. - * - * We also need to tell the Olm js to look for its wasm file at the same - * level as index.html. It really should be in the same place as the js, - * ie. in the bundle directory, but as far as I can tell this is - * completely impossible with webpack. We do, however, use a hashed - * filename to avoid caching issues. - */ - return Olm.init({ - locateFile: () => olmWasmPath, - }).then(() => { - console.log("Using WebAssembly Olm"); - }).catch((e) => { - console.log("Failed to load Olm: trying legacy version", e); - return new Promise((resolve, reject) => { - const s = document.createElement('script'); - s.src = 'olm_legacy.js'; // XXX: This should be cache-busted too - s.onload = resolve; - s.onerror = reject; - document.body.appendChild(s); - }).then(() => { - // Init window.Olm, ie. the one just loaded by the script tag, - // not 'Olm' which is still the failed wasm version. - return window.Olm.init(); - }).then(() => { - console.log("Using legacy Olm"); - }).catch((e) => { - console.log("Both WebAssembly and asm.js Olm failed!", e); - }); - }); -} - async function loadLanguage() { const prefLang = SettingsStore.getValue("language", null, /*excludeDefault=*/true); let langs = []; diff --git a/src/vector/init.ts b/src/vector/init.ts new file mode 100644 index 000000000..a51987b19 --- /dev/null +++ b/src/vector/init.ts @@ -0,0 +1,60 @@ +/* +Copyright 2015, 2016 OpenMarket Ltd +Copyright 2017 Vector Creations Ltd +Copyright 2018, 2019 New Vector Ltd +Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> +Copyright 2020 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// @ts-ignore +import olmWasmPath from "olm/olm.wasm"; +import Olm from 'olm'; + +export function loadOlm() { + /* Load Olm. We try the WebAssembly version first, and then the legacy, + * asm.js version if that fails. For this reason we need to wait for this + * to finish before continuing to load the rest of the app. In future + * we could somehow pass a promise down to react-sdk and have it wait on + * that so olm can be loading in parallel with the rest of the app. + * + * We also need to tell the Olm js to look for its wasm file at the same + * level as index.html. It really should be in the same place as the js, + * ie. in the bundle directory, but as far as I can tell this is + * completely impossible with webpack. We do, however, use a hashed + * filename to avoid caching issues. + */ + return Olm.init({ + locateFile: () => olmWasmPath, + }).then(() => { + console.log("Using WebAssembly Olm"); + }).catch((e) => { + console.log("Failed to load Olm: trying legacy version", e); + return new Promise((resolve, reject) => { + const s = document.createElement('script'); + s.src = 'olm_legacy.js'; // XXX: This should be cache-busted too + s.onload = resolve; + s.onerror = reject; + document.body.appendChild(s); + }).then(() => { + // Init window.Olm, ie. the one just loaded by the script tag, + // not 'Olm' which is still the failed wasm version. + return window.Olm.init(); + }).then(() => { + console.log("Using legacy Olm"); + }).catch((e) => { + console.log("Both WebAssembly and asm.js Olm failed!", e); + }); + }); +} From 6fb9fc4e6f104fe4fe4850323aeecc9d6c487efa Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 25 Mar 2020 13:55:25 +0000 Subject: [PATCH 4/8] move loadLanguage to init.ts Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/vector/app.js | 23 +---------------------- src/vector/init.ts | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/vector/app.js b/src/vector/app.js index 8bf735313..6099b3aa0 100644 --- a/src/vector/app.js +++ b/src/vector/app.js @@ -27,7 +27,6 @@ import ReactDOM from 'react-dom'; import * as sdk from 'matrix-react-sdk'; import PlatformPeg from 'matrix-react-sdk/src/PlatformPeg'; import * as VectorConferenceHandler from 'matrix-react-sdk/src/VectorConferenceHandler'; -import * as languageHandler from 'matrix-react-sdk/src/languageHandler'; import {_t, _td, newTranslatableError} from 'matrix-react-sdk/src/languageHandler'; import AutoDiscoveryUtils from 'matrix-react-sdk/src/utils/AutoDiscoveryUtils'; import {AutoDiscovery} from "matrix-js-sdk/src/autodiscovery"; @@ -38,13 +37,12 @@ import url from 'url'; import {parseQs, parseQsFromFragment} from './url_utils'; import {MatrixClientPeg} from 'matrix-react-sdk/src/MatrixClientPeg'; -import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore"; import SdkConfig from "matrix-react-sdk/src/SdkConfig"; import {setTheme} from "matrix-react-sdk/src/theme"; import CallHandler from 'matrix-react-sdk/src/CallHandler'; import {loadConfig, preparePlatform} from "./initial-load"; -import {loadOlm} from "./init"; +import {loadLanguage, loadOlm} from "./init"; let lastLocationHashSet = null; @@ -305,25 +303,6 @@ export async function loadApp() { } } -async function loadLanguage() { - const prefLang = SettingsStore.getValue("language", null, /*excludeDefault=*/true); - let langs = []; - - if (!prefLang) { - languageHandler.getLanguagesFromBrowser().forEach((l) => { - langs.push(...languageHandler.getNormalizedLanguageKeys(l)); - }); - } else { - langs = [prefLang]; - } - try { - await languageHandler.setLanguage(langs); - document.documentElement.setAttribute("lang", languageHandler.getCurrentLanguage()); - } catch (e) { - console.error("Unable to set language", e); - } -} - async function verifyServerConfig() { let validatedConfig; try { diff --git a/src/vector/init.ts b/src/vector/init.ts index a51987b19..96fd57b00 100644 --- a/src/vector/init.ts +++ b/src/vector/init.ts @@ -22,6 +22,10 @@ limitations under the License. import olmWasmPath from "olm/olm.wasm"; import Olm from 'olm'; +import * as languageHandler from 'matrix-react-sdk/src/languageHandler'; +import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore"; + + export function loadOlm() { /* Load Olm. We try the WebAssembly version first, and then the legacy, * asm.js version if that fails. For this reason we need to wait for this @@ -58,3 +62,22 @@ export function loadOlm() { }); }); } + +export async function loadLanguage() { + const prefLang = SettingsStore.getValue("language", null, /*excludeDefault=*/true); + let langs = []; + + if (!prefLang) { + languageHandler.getLanguagesFromBrowser().forEach((l) => { + langs.push(...languageHandler.getNormalizedLanguageKeys(l)); + }); + } else { + langs = [prefLang]; + } + try { + await languageHandler.setLanguage(langs); + document.documentElement.setAttribute("lang", languageHandler.getCurrentLanguage()); + } catch (e) { + console.error("Unable to set language", e); + } +} From 63159075857ca954d8073005aaab40f52439bdb0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 25 Mar 2020 14:07:22 +0000 Subject: [PATCH 5/8] Typescript stuff. Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/@types/global.d.ts | 1 + src/vector/{getconfig.js => getconfig.ts} | 5 ++--- src/vector/init.ts | 2 +- src/vector/{rageshakesetup.js => rageshakesetup.ts} | 2 +- src/vector/url_utils.ts | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) rename src/vector/{getconfig.js => getconfig.ts} (94%) rename src/vector/{rageshakesetup.js => rageshakesetup.ts} (97%) diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts index 7aec4de95..00373a689 100644 --- a/src/@types/global.d.ts +++ b/src/@types/global.d.ts @@ -18,4 +18,5 @@ interface Window { Olm: { init: () => Promise; }; + mxSendRageshake: (text: string, withLogs?: boolean) => void; } diff --git a/src/vector/getconfig.js b/src/vector/getconfig.ts similarity index 94% rename from src/vector/getconfig.js rename to src/vector/getconfig.ts index c0f81f6cc..36c36ccea 100644 --- a/src/vector/getconfig.js +++ b/src/vector/getconfig.ts @@ -18,8 +18,7 @@ import request from 'browser-request'; // Load the config file. First try to load up a domain-specific config of the // form "config.$domain.json" and if that fails, fall back to config.json. -export async function getVectorConfig(relativeLocation) { - if (relativeLocation === undefined) relativeLocation = ''; +export async function getVectorConfig(relativeLocation: string='') { if (relativeLocation !== '' && !relativeLocation.endsWith('/')) relativeLocation += '/'; const specificConfigPromise = getConfig(`${relativeLocation}config.${document.domain}.json`); @@ -37,7 +36,7 @@ export async function getVectorConfig(relativeLocation) { } } -function getConfig(configJsonFilename) { +function getConfig(configJsonFilename: string): Promise<{}> { return new Promise(function(resolve, reject) { request( { method: "GET", url: configJsonFilename, qs: { cachebuster: Date.now() } }, diff --git a/src/vector/init.ts b/src/vector/init.ts index 96fd57b00..16a6a8853 100644 --- a/src/vector/init.ts +++ b/src/vector/init.ts @@ -26,7 +26,7 @@ import * as languageHandler from 'matrix-react-sdk/src/languageHandler'; import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore"; -export function loadOlm() { +export function loadOlm(): Promise { /* Load Olm. We try the WebAssembly version first, and then the legacy, * asm.js version if that fails. For this reason we need to wait for this * to finish before continuing to load the rest of the app. In future diff --git a/src/vector/rageshakesetup.js b/src/vector/rageshakesetup.ts similarity index 97% rename from src/vector/rageshakesetup.js rename to src/vector/rageshakesetup.ts index e9ce1c3b8..6445f4e95 100644 --- a/src/vector/rageshakesetup.js +++ b/src/vector/rageshakesetup.ts @@ -50,7 +50,7 @@ function initRageshake() { initRageshake(); -global.mxSendRageshake = function(text, withLogs) { +window.mxSendRageshake = function(text: string, withLogs?: boolean) { if (withLogs === undefined) withLogs = true; if (!text || !text.trim()) { console.error("Cannot send a rageshake without a message - please tell us what went wrong"); diff --git a/src/vector/url_utils.ts b/src/vector/url_utils.ts index 935167aa8..6a08ec75a 100644 --- a/src/vector/url_utils.ts +++ b/src/vector/url_utils.ts @@ -20,7 +20,7 @@ import * as qs from 'querystring'; // so we're re-using query string like format // // returns {location, params} -export function parseQsFromFragment(location) { +export function parseQsFromFragment(location: Location) { // if we have a fragment, it will start with '#', which we need to drop. // (if we don't, this will return ''). const fragment = location.hash.substring(1); @@ -41,6 +41,6 @@ export function parseQsFromFragment(location) { return result; } -export function parseQs(location) { +export function parseQs(location: Location) { return qs.parse(location.search.substring(1)); } From 1f94b25d2546255b02fb57529274a78af798213a Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 25 Mar 2020 14:28:07 +0000 Subject: [PATCH 6/8] Fix copyrights Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/@types/global.d.ts | 2 +- src/vector/getconfig.ts | 2 +- src/vector/init.ts | 3 +-- src/vector/url_utils.ts | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts index 00373a689..646fe6ea1 100644 --- a/src/@types/global.d.ts +++ b/src/@types/global.d.ts @@ -1,5 +1,5 @@ /* -Copyright 2020 The Matrix.org Foundation C.I.C. +Copyright 2020 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/vector/getconfig.ts b/src/vector/getconfig.ts index 36c36ccea..84b6d47d2 100644 --- a/src/vector/getconfig.ts +++ b/src/vector/getconfig.ts @@ -1,5 +1,5 @@ /* -Copyright 2018 New Vector Ltd +Copyright 2018, 2020 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/vector/init.ts b/src/vector/init.ts index 16a6a8853..8ec45df0f 100644 --- a/src/vector/init.ts +++ b/src/vector/init.ts @@ -1,9 +1,8 @@ /* Copyright 2015, 2016 OpenMarket Ltd Copyright 2017 Vector Creations Ltd -Copyright 2018, 2019 New Vector Ltd +Copyright 2018, 2019, 2020 New Vector Ltd Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> -Copyright 2020 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/vector/url_utils.ts b/src/vector/url_utils.ts index 6a08ec75a..d35de505e 100644 --- a/src/vector/url_utils.ts +++ b/src/vector/url_utils.ts @@ -1,5 +1,5 @@ /* -Copyright 2018 New Vector Ltd +Copyright 2018, 2020 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From 92d8ee355d7767f9b4a5f4bbefe1148ab809894d Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 25 Mar 2020 14:32:37 +0000 Subject: [PATCH 7/8] merge initial-load.ts into init.ts as its no longer used by Jitsi Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/vector/app.js | 3 +- src/vector/init.ts | 42 +++++++++++++++++++++++++++ src/vector/initial-load.ts | 58 -------------------------------------- 3 files changed, 43 insertions(+), 60 deletions(-) delete mode 100644 src/vector/initial-load.ts diff --git a/src/vector/app.js b/src/vector/app.js index 6099b3aa0..b7ad872ab 100644 --- a/src/vector/app.js +++ b/src/vector/app.js @@ -41,8 +41,7 @@ import SdkConfig from "matrix-react-sdk/src/SdkConfig"; import {setTheme} from "matrix-react-sdk/src/theme"; import CallHandler from 'matrix-react-sdk/src/CallHandler'; -import {loadConfig, preparePlatform} from "./initial-load"; -import {loadLanguage, loadOlm} from "./init"; +import {loadConfig, preparePlatform, loadLanguage, loadOlm} from "./init"; let lastLocationHashSet = null; diff --git a/src/vector/init.ts b/src/vector/init.ts index 8ec45df0f..04db8801c 100644 --- a/src/vector/init.ts +++ b/src/vector/init.ts @@ -23,8 +23,50 @@ import Olm from 'olm'; import * as languageHandler from 'matrix-react-sdk/src/languageHandler'; import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore"; +import ElectronPlatform from "./platform/ElectronPlatform"; +import WebPlatform from "./platform/WebPlatform"; +import PlatformPeg from 'matrix-react-sdk/src/PlatformPeg'; +import SdkConfig from "matrix-react-sdk/src/SdkConfig"; +export function preparePlatform() { + if ((window).ipcRenderer) { + console.log("Using Electron platform"); + const plaf = new ElectronPlatform(); + PlatformPeg.set(plaf); + } else { + console.log("Using Web platform"); + PlatformPeg.set(new WebPlatform()); + } +} + +export async function loadConfig(): Promise<{configError?: Error, configSyntaxError: boolean}> { + const platform = PlatformPeg.get(); + + let configJson; + let configError; + let configSyntaxError = false; + try { + configJson = await platform.getConfig(); + } catch (e) { + configError = e; + + if (e && e.err && e.err instanceof SyntaxError) { + console.error("SyntaxError loading config:", e); + configSyntaxError = true; + configJson = {}; // to prevent errors between here and loading CSS for the error box + } + } + + // XXX: We call this twice, once here and once in MatrixChat as a prop. We call it here to ensure + // granular settings are loaded correctly and to avoid duplicating the override logic for the theme. + // + // Note: this isn't called twice for some wrappers, like the Jitsi wrapper. + SdkConfig.put(configJson); + + return {configError, configSyntaxError}; +} + export function loadOlm(): Promise { /* Load Olm. We try the WebAssembly version first, and then the legacy, * asm.js version if that fails. For this reason we need to wait for this diff --git a/src/vector/initial-load.ts b/src/vector/initial-load.ts deleted file mode 100644 index 7bd73c5a4..000000000 --- a/src/vector/initial-load.ts +++ /dev/null @@ -1,58 +0,0 @@ -/* -Copyright 2020 New Vector Ltd - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -import ElectronPlatform from './platform/ElectronPlatform'; -import WebPlatform from './platform/WebPlatform'; -import PlatformPeg from 'matrix-react-sdk/src/PlatformPeg'; -import SdkConfig from "matrix-react-sdk/src/SdkConfig"; - -export function preparePlatform() { - if ((window).ipcRenderer) { - console.log("Using Electron platform"); - const plaf = new ElectronPlatform(); - PlatformPeg.set(plaf); - } else { - console.log("Using Web platform"); - PlatformPeg.set(new WebPlatform()); - } -} - -export async function loadConfig(): Promise<{configError?: Error, configSyntaxError: boolean}> { - const platform = PlatformPeg.get(); - - let configJson; - let configError; - let configSyntaxError = false; - try { - configJson = await platform.getConfig(); - } catch (e) { - configError = e; - - if (e && e.err && e.err instanceof SyntaxError) { - console.error("SyntaxError loading config:", e); - configSyntaxError = true; - configJson = {}; // to prevent errors between here and loading CSS for the error box - } - } - - // XXX: We call this twice, once here and once in MatrixChat as a prop. We call it here to ensure - // granular settings are loaded correctly and to avoid duplicating the override logic for the theme. - // - // Note: this isn't called twice for some wrappers, like the Jitsi wrapper. - SdkConfig.put(configJson); - - return {configError, configSyntaxError}; -} From fd385f8450485faff820b1c093323dccf1d889af Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 25 Mar 2020 14:37:28 +0000 Subject: [PATCH 8/8] simplify loadConfig Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/vector/app.js | 8 ++++---- src/vector/init.ts | 26 ++++++++------------------ 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/vector/app.js b/src/vector/app.js index b7ad872ab..131e1ca41 100644 --- a/src/vector/app.js +++ b/src/vector/app.js @@ -187,7 +187,7 @@ export async function loadApp() { const platform = PlatformPeg.get(); // Load the config from the platform - const configInfo = await loadConfig(); + const configError = await loadConfig(); // Load language after loading config.json so that settingsDefaults.language can be applied await loadLanguage(); @@ -216,7 +216,7 @@ export async function loadApp() { await setTheme(); // Now that we've loaded the theme (CSS), display the config syntax error if needed. - if (configInfo.configSyntaxError) { + if (configError && configError.err && configError.err instanceof SyntaxError) { const errorMessage = (

@@ -228,7 +228,7 @@ export async function loadApp() {

{_t( "The message from the parser is: %(message)s", - {message: configInfo.configError.err.message || _t("Invalid JSON")}, + {message: configError.err.message || _t("Invalid JSON")}, )}

@@ -248,7 +248,7 @@ export async function loadApp() { const urlWithoutQuery = window.location.protocol + '//' + window.location.host + window.location.pathname; console.log("Vector starting at " + urlWithoutQuery); - if (configInfo.configError) { + if (configError) { window.matrixChat = ReactDOM.render(
Unable to load config file: please refresh the page to try again.
, document.getElementById('matrixchat')); diff --git a/src/vector/init.ts b/src/vector/init.ts index 04db8801c..96745f53c 100644 --- a/src/vector/init.ts +++ b/src/vector/init.ts @@ -40,31 +40,21 @@ export function preparePlatform() { } } -export async function loadConfig(): Promise<{configError?: Error, configSyntaxError: boolean}> { +export async function loadConfig(): Promise { const platform = PlatformPeg.get(); let configJson; - let configError; - let configSyntaxError = false; try { configJson = await platform.getConfig(); } catch (e) { - configError = e; - - if (e && e.err && e.err instanceof SyntaxError) { - console.error("SyntaxError loading config:", e); - configSyntaxError = true; - configJson = {}; // to prevent errors between here and loading CSS for the error box - } + return e; + } finally { + // XXX: We call this twice, once here and once in MatrixChat as a prop. We call it here to ensure + // granular settings are loaded correctly and to avoid duplicating the override logic for the theme. + // + // Note: this isn't called twice for some wrappers, like the Jitsi wrapper. + SdkConfig.put(configJson || {}); } - - // XXX: We call this twice, once here and once in MatrixChat as a prop. We call it here to ensure - // granular settings are loaded correctly and to avoid duplicating the override logic for the theme. - // - // Note: this isn't called twice for some wrappers, like the Jitsi wrapper. - SdkConfig.put(configJson); - - return {configError, configSyntaxError}; } export function loadOlm(): Promise {