mirror of
https://github.com/vector-im/element-call.git
synced 2024-11-21 00:28:08 +08:00
Lazy load matrix-js-sdk when running as SPA (#2785)
This commit is contained in:
parent
137a53dbee
commit
50934a53cd
@ -16,19 +16,13 @@ import {
|
|||||||
useMemo,
|
useMemo,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { useHistory } from "react-router-dom";
|
import { useHistory } from "react-router-dom";
|
||||||
import {
|
|
||||||
ClientEvent,
|
|
||||||
ICreateClientOpts,
|
|
||||||
MatrixClient,
|
|
||||||
} from "matrix-js-sdk/src/client";
|
|
||||||
import { logger } from "matrix-js-sdk/src/logger";
|
import { logger } from "matrix-js-sdk/src/logger";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync";
|
import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync";
|
||||||
import { MatrixError } from "matrix-js-sdk/src/matrix";
|
import { ClientEvent, type MatrixClient } from "matrix-js-sdk/src/client";
|
||||||
import { WidgetApi } from "matrix-widget-api";
|
|
||||||
|
|
||||||
|
import type { WidgetApi } from "matrix-widget-api";
|
||||||
import { ErrorView } from "./FullScreenView";
|
import { ErrorView } from "./FullScreenView";
|
||||||
import { fallbackICEServerAllowed, initClient } from "./utils/matrix";
|
|
||||||
import { widget } from "./widget";
|
import { widget } from "./widget";
|
||||||
import {
|
import {
|
||||||
PosthogAnalytics,
|
PosthogAnalytics,
|
||||||
@ -36,7 +30,6 @@ import {
|
|||||||
} from "./analytics/PosthogAnalytics";
|
} from "./analytics/PosthogAnalytics";
|
||||||
import { translatedError } from "./TranslatedError";
|
import { translatedError } from "./TranslatedError";
|
||||||
import { useEventTarget } from "./useEvents";
|
import { useEventTarget } from "./useEvents";
|
||||||
import { Config } from "./config/Config";
|
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
@ -359,7 +352,7 @@ export const ClientProvider: FC<Props> = ({ children }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
type InitResult = {
|
export type InitResult = {
|
||||||
widgetApi: WidgetApi | null;
|
widgetApi: WidgetApi | null;
|
||||||
client: MatrixClient;
|
client: MatrixClient;
|
||||||
passwordlessUser: boolean;
|
passwordlessUser: boolean;
|
||||||
@ -376,50 +369,8 @@ async function loadClient(): Promise<InitResult | null> {
|
|||||||
passwordlessUser: false,
|
passwordlessUser: false,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
// We're running as a standalone application
|
const { initSPA } = await import("./utils/spa");
|
||||||
try {
|
return initSPA(loadSession, clearSession);
|
||||||
const session = loadSession();
|
|
||||||
if (!session) {
|
|
||||||
logger.log("No session stored; continuing without a client");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.log("Using a standalone client");
|
|
||||||
|
|
||||||
/* eslint-disable camelcase */
|
|
||||||
const { user_id, device_id, access_token, passwordlessUser } = session;
|
|
||||||
const initClientParams: ICreateClientOpts = {
|
|
||||||
baseUrl: Config.defaultHomeserverUrl()!,
|
|
||||||
accessToken: access_token,
|
|
||||||
userId: user_id,
|
|
||||||
deviceId: device_id,
|
|
||||||
fallbackICEServerAllowed: fallbackICEServerAllowed,
|
|
||||||
livekitServiceURL: Config.get().livekit?.livekit_service_url,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const client = await initClient(initClientParams, true);
|
|
||||||
return {
|
|
||||||
widgetApi: null,
|
|
||||||
client,
|
|
||||||
passwordlessUser,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof MatrixError && err.errcode === "M_UNKNOWN_TOKEN") {
|
|
||||||
// We can't use this session anymore, so let's log it out
|
|
||||||
logger.log(
|
|
||||||
"The session from local store is invalid; continuing without a client",
|
|
||||||
);
|
|
||||||
clearSession();
|
|
||||||
// returning null = "no client` pls register" (undefined = "loading" which is the current value when reaching this line)
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
clearSession();
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
64
src/utils/spa.ts
Normal file
64
src/utils/spa.ts
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2024 New Vector Ltd.
|
||||||
|
|
||||||
|
SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
Please see LICENSE in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { ICreateClientOpts } from "matrix-js-sdk/src/client";
|
||||||
|
import { MatrixError } from "matrix-js-sdk/src/http-api";
|
||||||
|
import { logger } from "matrix-js-sdk/src/logger";
|
||||||
|
|
||||||
|
import { Config } from "../config/Config";
|
||||||
|
import { fallbackICEServerAllowed, initClient } from "./matrix";
|
||||||
|
import type { InitResult, Session } from "../ClientContext";
|
||||||
|
|
||||||
|
export async function initSPA(
|
||||||
|
loadSession: () => Session | undefined,
|
||||||
|
clearSession: () => void,
|
||||||
|
): Promise<InitResult | null> {
|
||||||
|
// We're running as a standalone application
|
||||||
|
try {
|
||||||
|
const session = loadSession();
|
||||||
|
if (!session) {
|
||||||
|
logger.log("No session stored; continuing without a client");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.log("Using a standalone client");
|
||||||
|
|
||||||
|
/* eslint-disable camelcase */
|
||||||
|
const { user_id, device_id, access_token, passwordlessUser } = session;
|
||||||
|
const initClientParams: ICreateClientOpts = {
|
||||||
|
baseUrl: Config.defaultHomeserverUrl()!,
|
||||||
|
accessToken: access_token,
|
||||||
|
userId: user_id,
|
||||||
|
deviceId: device_id,
|
||||||
|
fallbackICEServerAllowed,
|
||||||
|
livekitServiceURL: Config.get().livekit?.livekit_service_url,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const client = await initClient(initClientParams, true);
|
||||||
|
return {
|
||||||
|
widgetApi: null,
|
||||||
|
client,
|
||||||
|
passwordlessUser,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof MatrixError && err.errcode === "M_UNKNOWN_TOKEN") {
|
||||||
|
// We can't use this session anymore, so let's log it out
|
||||||
|
logger.log(
|
||||||
|
"The session from local store is invalid; continuing without a client",
|
||||||
|
);
|
||||||
|
clearSession();
|
||||||
|
// returning null = "no client` pls register" (undefined = "loading" which is the current value when reaching this line)
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
clearSession();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user