Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Break import cycles by not directly depending on Lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Jun 27, 2022
1 parent e644b2b commit b607da5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import VideoChannelStore from "./stores/VideoChannelStore";
import { fixStuckDevices } from "./utils/VideoChannelUtils";
import { Action } from "./dispatcher/actions";
import AbstractLocalStorageSettingsHandler from "./settings/handlers/AbstractLocalStorageSettingsHandler";
import { OverwriteLoginPayload } from "./dispatcher/payloads/OverwriteLoginPayload";

const HOMESERVER_URL_KEY = "mx_hs_url";
const ID_SERVER_URL_KEY = "mx_is_url";
Expand All @@ -71,6 +72,10 @@ dis.register((payload) => {
if (payload.action === Action.TriggerLogout) {
// noinspection JSIgnoredPromiseFromCall - we don't care if it fails
onLoggedOut();
} else if (payload.action === Action.OverwriteLogin) {
const typed = <OverwriteLoginPayload>payload;
// noinspection JSIgnoredPromiseFromCall - we don't care if it fails
doSetLoggedIn(typed.credentials, true);
}
});

Expand Down Expand Up @@ -558,7 +563,7 @@ export async function hydrateSession(credentials: IMatrixClientCreds): Promise<M
*
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started
*/
export async function doSetLoggedIn(
async function doSetLoggedIn(
credentials: IMatrixClientCreds,
clearStorageEnabled: boolean,
): Promise<MatrixClient> {
Expand Down
5 changes: 5 additions & 0 deletions src/dispatcher/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,9 @@ export enum Action {
* Fired when the client was logged in. No additional payload information required.
*/
OnLoggedIn = "on_logged_in",

/**
* Overwrites the existing login with fresh session credentials. Use with a OverwriteLoginPayload.
*/
OverwriteLogin = "overwrite_login",
}
25 changes: 25 additions & 0 deletions src/dispatcher/payloads/OverwriteLoginPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
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 { ActionPayload } from "../payloads";
import { Action } from "../actions";
import { IMatrixClientCreds } from "../../MatrixClientPeg";

export interface OverwriteLoginPayload extends ActionPayload {
action: Action.OverwriteLogin;

credentials: IMatrixClientCreds;
}
13 changes: 8 additions & 5 deletions src/modules/ProxiedModuleApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import { _t } from "../languageHandler";
import { ModuleUiDialog } from "../components/views/dialogs/ModuleUiDialog";
import SdkConfig from "../SdkConfig";
import PlatformPeg from "../PlatformPeg";
import { doSetLoggedIn } from "../Lifecycle";
import dispatcher from "../dispatcher/dispatcher";
import { navigateToPermalink } from "../utils/permalinks/navigator";
import { parsePermalink } from "../utils/permalinks/Permalinks";
import { MatrixClientPeg } from "../MatrixClientPeg";
import { getCachedRoomIDForAlias } from "../RoomAliasCache";
import { Action } from "../dispatcher/actions";
import { OverwriteLoginPayload } from "../dispatcher/payloads/OverwriteLoginPayload";

/**
* Glue between the `ModuleApi` interface and the react-sdk. Anticipates one instance
Expand Down Expand Up @@ -136,10 +136,13 @@ export class ProxiedModuleApi implements ModuleApi {
* @override
*/
public async overwriteAccountAuth(accountInfo: AccountAuthInfo): Promise<void> {
await doSetLoggedIn({
...accountInfo,
guest: false,
}, true);
dispatcher.dispatch<OverwriteLoginPayload>({
action: Action.OverwriteLogin,
credentials: {
...accountInfo,
guest: false,
},
}, true); // require to be sync to match inherited interface behaviour
}

/**
Expand Down

0 comments on commit b607da5

Please sign in to comment.