Merge pull request #702 from robintown/no-double-clients

Don't doubly initialize the client in strict mode
This commit is contained in:
Robin 2022-11-02 11:28:21 -04:00 committed by GitHub
commit 9b93d45ea0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,6 +22,7 @@ import React, {
createContext, createContext,
useMemo, useMemo,
useContext, useContext,
useRef,
} from "react"; } from "react";
import { useHistory } from "react-router-dom"; import { useHistory } from "react-router-dom";
import { MatrixClient, ClientEvent } from "matrix-js-sdk/src/client"; import { MatrixClient, ClientEvent } from "matrix-js-sdk/src/client";
@ -87,6 +88,7 @@ interface Props {
export const ClientProvider: FC<Props> = ({ children }) => { export const ClientProvider: FC<Props> = ({ children }) => {
const history = useHistory(); const history = useHistory();
const initializing = useRef(false);
const [ const [
{ loading, isAuthenticated, isPasswordlessUser, client, userName, error }, { loading, isAuthenticated, isPasswordlessUser, client, userName, error },
setState, setState,
@ -100,6 +102,12 @@ export const ClientProvider: FC<Props> = ({ children }) => {
}); });
useEffect(() => { useEffect(() => {
// In case the component is mounted, unmounted, and remounted quickly (as
// React does in strict mode), we need to make sure not to doubly initialize
// the client
if (initializing.current) return;
initializing.current = true;
const init = async (): Promise< const init = async (): Promise<
Pick<ClientProviderState, "client" | "isPasswordlessUser"> Pick<ClientProviderState, "client" | "isPasswordlessUser">
> => { > => {
@ -190,7 +198,8 @@ export const ClientProvider: FC<Props> = ({ children }) => {
userName: null, userName: null,
error: undefined, error: undefined,
}); });
}); })
.finally(() => (initializing.current = false));
}, []); }, []);
const changePassword = useCallback( const changePassword = useCallback(