@@ -54,6 +54,8 @@ import SettingsStore, {SettingLevel} from "matrix-react-sdk/lib/settings/Setting
5454import Tinter from 'matrix-react-sdk/lib/Tinter' ;
5555import SdkConfig from "matrix-react-sdk/lib/SdkConfig" ;
5656
57+ import Olm from 'olm' ;
58+
5759import rageshake from "matrix-react-sdk/lib/rageshake/rageshake" ;
5860
5961import CallHandler from 'matrix-react-sdk/lib/CallHandler' ;
@@ -229,6 +231,8 @@ async function loadApp() {
229231
230232 window . addEventListener ( 'hashchange' , onHashChange ) ;
231233
234+ await loadOlm ( ) ;
235+
232236 await loadLanguage ( ) ;
233237
234238 const fragparts = parseQsFromFragment ( window . location ) ;
@@ -362,6 +366,42 @@ async function loadApp() {
362366 }
363367}
364368
369+ function loadOlm ( ) {
370+ /* Load Olm. We try the WebAssembly version first, and then the legacy,
371+ * asm.js version if that fails. For this reason we need to wait for this
372+ * to finish before continuing to load the rest of the app. In future
373+ * we could somehow pass a promise down to react-sdk and have it wait on
374+ * that so olm can be loading in parallel with the rest of the app.
375+ *
376+ * We also need to tell the Olm js to look for its wasm file at the same
377+ * level as index.html. It really should be in the same place as the js,
378+ * ie. in the bundle directory, to avoid caching issues, but as far as I
379+ * can tell this is completely impossible with webpack.
380+ */
381+ return Olm . init ( {
382+ locateFile : ( ) => 'olm.wasm' ,
383+ } ) . then ( ( ) => {
384+ console . log ( "Using WebAssembly Olm" ) ;
385+ } ) . catch ( ( e ) => {
386+ console . log ( "Failed to load Olm: trying legacy version" ) ;
387+ return new Promise ( ( resolve , reject ) => {
388+ const s = document . createElement ( 'script' ) ;
389+ s . src = 'olm_legacy.js' ;
390+ s . onload = resolve ;
391+ s . onerror = reject ;
392+ document . body . appendChild ( s ) ;
393+ } ) . then ( ( ) => {
394+ // Init window.Olm, ie. the one just loaded by the script tag,
395+ // not 'Olm' which is still the failed wasm version.
396+ return window . Olm . init ( ) ;
397+ } ) . then ( ( ) => {
398+ console . log ( "Using legacy Olm" ) ;
399+ } ) . catch ( ( e ) => {
400+ console . log ( "Both WebAssembly and asm.js Olm failed!" , e ) ;
401+ } ) ;
402+ } ) ;
403+ }
404+
365405async function loadLanguage ( ) {
366406 const prefLang = SettingsStore . getValue ( "language" , null , /*excludeDefault=*/ true ) ;
367407 let langs = [ ] ;
0 commit comments