diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 27776d37..ab20a4eb 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -15,6 +15,7 @@ limitations under the License. */ import {createEnum} from "../utils/enum.js"; +import {AbortableOperation} from "../utils/AbortableOperation"; import {ObservableValue} from "../observable/ObservableValue.js"; import {HomeServerApi} from "./net/HomeServerApi.js"; import {Reconnector, ConnectionStatus} from "./net/Reconnector.js"; @@ -53,6 +54,7 @@ export const LoginFailure = createEnum( "Unknown", ); + export class SessionContainer { constructor({platform, olmPromise, workerPromise}) { this._platform = platform; @@ -121,11 +123,13 @@ export class SessionContainer { return result; } - async queryLogin(homeServer) { + queryLogin(homeServer) { const normalizedHS = normalizeHomeserver(homeServer); const hsApi = new HomeServerApi({homeServer: normalizedHS, request: this._platform.request}); - const response = await hsApi.getLoginFlows().response(); - return this._parseLoginOptions(response, normalizedHS); + return new AbortableOperation(async setAbortable => { + const response = await setAbortable(hsApi.getLoginFlows()).response(); + return this._parseLoginOptions(response, normalizedHS); + }); } async startWithLogin(loginMethod) { diff --git a/src/utils/AbortableOperation.ts b/src/utils/AbortableOperation.ts new file mode 100644 index 00000000..0cc49e10 --- /dev/null +++ b/src/utils/AbortableOperation.ts @@ -0,0 +1,40 @@ +/* +Copyright 2020 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. +*/ + +interface IAbortable { + abort(); +} + +type RunFn = (setAbortable: (a: IAbortable) => typeof a) => T; + +export class AbortableOperation { + public readonly result: T; + private _abortable: IAbortable | null; + + constructor(run: RunFn) { + this._abortable = null; + const setAbortable = abortable => { + this._abortable = abortable; + return abortable; + }; + this.result = run(setAbortable); + } + + abort() { + this._abortable?.abort(); + this._abortable = null; + } +}