Skip to content

Commit

Permalink
Merge pull request #7385 from vector-im/dbkr/wasm
Browse files Browse the repository at this point in the history
Support WebAssembly version of Olm
  • Loading branch information
dbkr committed Oct 25, 2018
2 parents d080a20 + bc93501 commit 9b80aa7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"webpack-dev-server": "^3.1.9"
},
"optionalDependencies": {
"olm": "https://matrix.org/packages/npm/olm/olm-2.2.1.tgz"
"olm": "https://matrix.org/packages/npm/olm/olm-3.0.0.tgz"
},
"build": {
"appId": "im.riot.app",
Expand Down
5 changes: 5 additions & 0 deletions scripts/copy-res.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ const COPY_LIST = [
["res/themes/**", "webapp/themes"],
["node_modules/emojione/assets/svg/*", "webapp/emojione/svg/"],
["node_modules/emojione/assets/png/*", "webapp/emojione/png/"],
// XXX: This is tied quite heavily to the matching olm.js so it really should be
// in the bundle dir with the js to avoid caching issues giving us wasm that
// doesn't match our js, but I cannot find any way to get webpack to do this.
["node_modules/olm/olm.wasm", "webapp", { directwatch: 1 }],
["node_modules/olm/olm_legacy.js", "webapp", { directwatch: 1 }],
["./config.json", "webapp", { directwatch: 1 }],
];

Expand Down
40 changes: 40 additions & 0 deletions src/vector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import SettingsStore, {SettingLevel} from "matrix-react-sdk/lib/settings/Setting
import Tinter from 'matrix-react-sdk/lib/Tinter';
import SdkConfig from "matrix-react-sdk/lib/SdkConfig";

import Olm from 'olm';

import rageshake from "matrix-react-sdk/lib/rageshake/rageshake";

import CallHandler from 'matrix-react-sdk/lib/CallHandler';
Expand Down Expand Up @@ -229,6 +231,8 @@ async function loadApp() {

window.addEventListener('hashchange', onHashChange);

await loadOlm();

await loadLanguage();

const fragparts = parseQsFromFragment(window.location);
Expand Down Expand Up @@ -362,6 +366,42 @@ 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, to avoid caching issues, but as far as I
* can tell this is completely impossible with webpack.
*/
return Olm.init({
locateFile: () => 'olm.wasm',
}).then(() => {
console.log("Using WebAssembly Olm");
}).catch((e) => {
console.log("Failed to load Olm: trying legacy version");
return new Promise((resolve, reject) => {
const s = document.createElement('script');
s.src = 'olm_legacy.js';
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 = [];
Expand Down
41 changes: 0 additions & 41 deletions src/vector/olm-loader.js

This file was deleted.

18 changes: 0 additions & 18 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ module.exports = {

"mobileguide": "./src/vector/mobile_guide/index.js",

// We ship olm.js as a separate lump of javascript. This makes it get
// loaded via a separate <script/> tag in index.html (which loads it
// into the browser global `Olm`, where js-sdk expects to find it).
//
// (we should probably make js-sdk load it asynchronously at some
// point, so that it doesn't block the pageload, but that is a separate
// problem)
"olm": "./src/vector/olm-loader.js",

// CSS themes
"theme-light": "./node_modules/matrix-react-sdk/res/themes/light/css/light.scss",
"theme-dark": "./node_modules/matrix-react-sdk/res/themes/dark/css/dark.scss",
Expand Down Expand Up @@ -171,12 +162,3 @@ module.exports = {
inline: false,
},
};

// olm is an optional dependency. Ignore it if it's not installed, to avoid a
// scary-looking error.
try {
require('olm');
} catch (e) {
console.log("Olm is not installed; not shipping it");
delete(module.exports.entry["olm"]);
}

0 comments on commit 9b80aa7

Please sign in to comment.