Skip to content

Commit df2b966

Browse files
committed
move loadOlm to init.ts
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent b09b475 commit df2b966

3 files changed

Lines changed: 82 additions & 41 deletions

File tree

‎src/@types/global.d.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2020 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
interface Window {
18+
Olm: {
19+
init: () => Promise<void>;
20+
};
21+
}

‎src/vector/app.js‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ See the License for the specific language governing permissions and
1818
limitations under the License.
1919
*/
2020

21-
import olmWasmPath from 'olm/olm.wasm';
22-
2321
import React from 'react';
2422
// add React and ReactPerf to the global namespace, to make them easier to
2523
// access via the console
@@ -44,10 +42,9 @@ import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore";
4442
import SdkConfig from "matrix-react-sdk/src/SdkConfig";
4543
import {setTheme} from "matrix-react-sdk/src/theme";
4644

47-
import Olm from 'olm';
48-
4945
import CallHandler from 'matrix-react-sdk/src/CallHandler';
5046
import {loadConfig, preparePlatform} from "./initial-load";
47+
import {loadOlm} from "./init";
5148

5249
let lastLocationHashSet = null;
5350

@@ -308,43 +305,6 @@ export async function loadApp() {
308305
}
309306
}
310307

311-
function loadOlm() {
312-
/* Load Olm. We try the WebAssembly version first, and then the legacy,
313-
* asm.js version if that fails. For this reason we need to wait for this
314-
* to finish before continuing to load the rest of the app. In future
315-
* we could somehow pass a promise down to react-sdk and have it wait on
316-
* that so olm can be loading in parallel with the rest of the app.
317-
*
318-
* We also need to tell the Olm js to look for its wasm file at the same
319-
* level as index.html. It really should be in the same place as the js,
320-
* ie. in the bundle directory, but as far as I can tell this is
321-
* completely impossible with webpack. We do, however, use a hashed
322-
* filename to avoid caching issues.
323-
*/
324-
return Olm.init({
325-
locateFile: () => olmWasmPath,
326-
}).then(() => {
327-
console.log("Using WebAssembly Olm");
328-
}).catch((e) => {
329-
console.log("Failed to load Olm: trying legacy version", e);
330-
return new Promise((resolve, reject) => {
331-
const s = document.createElement('script');
332-
s.src = 'olm_legacy.js'; // XXX: This should be cache-busted too
333-
s.onload = resolve;
334-
s.onerror = reject;
335-
document.body.appendChild(s);
336-
}).then(() => {
337-
// Init window.Olm, ie. the one just loaded by the script tag,
338-
// not 'Olm' which is still the failed wasm version.
339-
return window.Olm.init();
340-
}).then(() => {
341-
console.log("Using legacy Olm");
342-
}).catch((e) => {
343-
console.log("Both WebAssembly and asm.js Olm failed!", e);
344-
});
345-
});
346-
}
347-
348308
async function loadLanguage() {
349309
const prefLang = SettingsStore.getValue("language", null, /*excludeDefault=*/true);
350310
let langs = [];

‎src/vector/init.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2015, 2016 OpenMarket Ltd
3+
Copyright 2017 Vector Creations Ltd
4+
Copyright 2018, 2019 New Vector Ltd
5+
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
6+
Copyright 2020 The Matrix.org Foundation C.I.C.
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
*/
20+
21+
// @ts-ignore
22+
import olmWasmPath from "olm/olm.wasm";
23+
import Olm from 'olm';
24+
25+
export function loadOlm() {
26+
/* Load Olm. We try the WebAssembly version first, and then the legacy,
27+
* asm.js version if that fails. For this reason we need to wait for this
28+
* to finish before continuing to load the rest of the app. In future
29+
* we could somehow pass a promise down to react-sdk and have it wait on
30+
* that so olm can be loading in parallel with the rest of the app.
31+
*
32+
* We also need to tell the Olm js to look for its wasm file at the same
33+
* level as index.html. It really should be in the same place as the js,
34+
* ie. in the bundle directory, but as far as I can tell this is
35+
* completely impossible with webpack. We do, however, use a hashed
36+
* filename to avoid caching issues.
37+
*/
38+
return Olm.init({
39+
locateFile: () => olmWasmPath,
40+
}).then(() => {
41+
console.log("Using WebAssembly Olm");
42+
}).catch((e) => {
43+
console.log("Failed to load Olm: trying legacy version", e);
44+
return new Promise((resolve, reject) => {
45+
const s = document.createElement('script');
46+
s.src = 'olm_legacy.js'; // XXX: This should be cache-busted too
47+
s.onload = resolve;
48+
s.onerror = reject;
49+
document.body.appendChild(s);
50+
}).then(() => {
51+
// Init window.Olm, ie. the one just loaded by the script tag,
52+
// not 'Olm' which is still the failed wasm version.
53+
return window.Olm.init();
54+
}).then(() => {
55+
console.log("Using legacy Olm");
56+
}).catch((e) => {
57+
console.log("Both WebAssembly and asm.js Olm failed!", e);
58+
});
59+
});
60+
}

0 commit comments

Comments
 (0)