Merge pull request #688 from robintown/load-olm-once
Ensure that Olm is only loaded once
This commit is contained in:
		
				commit
				
					
						54d903933b
					
				
			
		
					 5 changed files with 67 additions and 52 deletions
				
			
		| 
						 | 
				
			
			@ -106,7 +106,8 @@
 | 
			
		|||
    ],
 | 
			
		||||
    "moduleNameMapper": {
 | 
			
		||||
      "\\.(css|less|svg)+$": "identity-obj-proxy",
 | 
			
		||||
      "./IndexedDBWorker\\?worker": "<rootDir>/test/mocks/workerMock.ts"
 | 
			
		||||
      "^\\./IndexedDBWorker\\?worker$": "<rootDir>/test/mocks/workerMock.ts",
 | 
			
		||||
      "^\\./olm$": "<rootDir>/test/mocks/olmMock.ts"
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										22
									
								
								src/App.tsx
									
										
									
									
									
								
							
							
						
						
									
										22
									
								
								src/App.tsx
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -14,9 +14,7 @@ See the License for the specific language governing permissions and
 | 
			
		|||
limitations under the License.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
import Olm from "@matrix-org/olm";
 | 
			
		||||
import olmWasmPath from "@matrix-org/olm/olm.wasm?url";
 | 
			
		||||
import React, { Suspense, useEffect, useState } from "react";
 | 
			
		||||
import React, { Suspense } from "react";
 | 
			
		||||
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
 | 
			
		||||
import * as Sentry from "@sentry/react";
 | 
			
		||||
import { OverlayProvider } from "@react-aria/overlays";
 | 
			
		||||
| 
						 | 
				
			
			@ -30,7 +28,7 @@ import { ClientProvider } from "./ClientContext";
 | 
			
		|||
import { usePageFocusStyle } from "./usePageFocusStyle";
 | 
			
		||||
import { SequenceDiagramViewerPage } from "./SequenceDiagramViewerPage";
 | 
			
		||||
import { InspectorContextProvider } from "./room/GroupCallInspector";
 | 
			
		||||
import { CrashView, LoadingView } from "./FullScreenView";
 | 
			
		||||
import { CrashView } from "./FullScreenView";
 | 
			
		||||
 | 
			
		||||
const SentryRoute = Sentry.withSentryRouting(Route);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -39,25 +37,12 @@ interface AppProps {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
export default function App({ history }: AppProps) {
 | 
			
		||||
  const [olmLoaded, setOlmLoaded] = useState(false);
 | 
			
		||||
 | 
			
		||||
  usePageFocusStyle();
 | 
			
		||||
 | 
			
		||||
  useEffect(() => {
 | 
			
		||||
    if (!olmLoaded) {
 | 
			
		||||
      // TODO: https://gitlab.matrix.org/matrix-org/olm/-/issues/10
 | 
			
		||||
      window.OLM_OPTIONS = {};
 | 
			
		||||
      Olm.init({ locateFile: () => olmWasmPath }).then(() =>
 | 
			
		||||
        setOlmLoaded(true)
 | 
			
		||||
      );
 | 
			
		||||
    }
 | 
			
		||||
  }, [olmLoaded, setOlmLoaded]);
 | 
			
		||||
 | 
			
		||||
  const errorPage = <CrashView />;
 | 
			
		||||
 | 
			
		||||
  return (
 | 
			
		||||
    <Router history={history}>
 | 
			
		||||
      {olmLoaded ? (
 | 
			
		||||
      <Suspense fallback={null}>
 | 
			
		||||
        <ClientProvider>
 | 
			
		||||
          <InspectorContextProvider>
 | 
			
		||||
| 
						 | 
				
			
			@ -88,9 +73,6 @@ export default function App({ history }: AppProps) {
 | 
			
		|||
          </InspectorContextProvider>
 | 
			
		||||
        </ClientProvider>
 | 
			
		||||
      </Suspense>
 | 
			
		||||
      ) : (
 | 
			
		||||
        <LoadingView />
 | 
			
		||||
      )}
 | 
			
		||||
    </Router>
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,6 +18,7 @@ import type { MatrixClient } from "matrix-js-sdk/src/client";
 | 
			
		|||
import type { Room } from "matrix-js-sdk/src/models/room";
 | 
			
		||||
import IndexedDBWorker from "./IndexedDBWorker?worker";
 | 
			
		||||
import { getUrlParams } from "./UrlParams";
 | 
			
		||||
import { loadOlm } from "./olm";
 | 
			
		||||
 | 
			
		||||
export const defaultHomeserver =
 | 
			
		||||
  (import.meta.env.VITE_DEFAULT_HOMESERVER as string) ??
 | 
			
		||||
| 
						 | 
				
			
			@ -72,8 +73,9 @@ export async function initClient(
 | 
			
		|||
  clientOptions: ICreateClientOpts,
 | 
			
		||||
  restore: boolean
 | 
			
		||||
): Promise<MatrixClient> {
 | 
			
		||||
  let indexedDB: IDBFactory;
 | 
			
		||||
  await loadOlm();
 | 
			
		||||
 | 
			
		||||
  let indexedDB: IDBFactory;
 | 
			
		||||
  try {
 | 
			
		||||
    indexedDB = window.indexedDB;
 | 
			
		||||
  } catch (e) {}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										29
									
								
								src/olm.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/olm.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,29 @@
 | 
			
		|||
/*
 | 
			
		||||
Copyright 2022 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 Olm from "@matrix-org/olm";
 | 
			
		||||
import olmWasmPath from "@matrix-org/olm/olm.wasm?url";
 | 
			
		||||
 | 
			
		||||
// https://gitlab.matrix.org/matrix-org/olm/-/issues/10
 | 
			
		||||
window.OLM_OPTIONS = {};
 | 
			
		||||
 | 
			
		||||
let olmLoaded: Promise<void> | null = null;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Loads Olm, if not already loaded.
 | 
			
		||||
 */
 | 
			
		||||
export const loadOlm = (): Promise<void> =>
 | 
			
		||||
  (olmLoaded ??= Olm.init({ locateFile: () => olmWasmPath }));
 | 
			
		||||
							
								
								
									
										1
									
								
								test/mocks/olmMock.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								test/mocks/olmMock.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
module.exports = { loadOlm: jest.fn(async () => {}) }
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue