forked from mystiq/hydrogen-web
make queryLogin abortable
This commit is contained in:
parent
3b693c5b02
commit
577c3168e6
2 changed files with 47 additions and 3 deletions
|
@ -15,6 +15,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {createEnum} from "../utils/enum.js";
|
import {createEnum} from "../utils/enum.js";
|
||||||
|
import {AbortableOperation} from "../utils/AbortableOperation";
|
||||||
import {ObservableValue} from "../observable/ObservableValue.js";
|
import {ObservableValue} from "../observable/ObservableValue.js";
|
||||||
import {HomeServerApi} from "./net/HomeServerApi.js";
|
import {HomeServerApi} from "./net/HomeServerApi.js";
|
||||||
import {Reconnector, ConnectionStatus} from "./net/Reconnector.js";
|
import {Reconnector, ConnectionStatus} from "./net/Reconnector.js";
|
||||||
|
@ -53,6 +54,7 @@ export const LoginFailure = createEnum(
|
||||||
"Unknown",
|
"Unknown",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
export class SessionContainer {
|
export class SessionContainer {
|
||||||
constructor({platform, olmPromise, workerPromise}) {
|
constructor({platform, olmPromise, workerPromise}) {
|
||||||
this._platform = platform;
|
this._platform = platform;
|
||||||
|
@ -121,11 +123,13 @@ export class SessionContainer {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async queryLogin(homeServer) {
|
queryLogin(homeServer) {
|
||||||
const normalizedHS = normalizeHomeserver(homeServer);
|
const normalizedHS = normalizeHomeserver(homeServer);
|
||||||
const hsApi = new HomeServerApi({homeServer: normalizedHS, request: this._platform.request});
|
const hsApi = new HomeServerApi({homeServer: normalizedHS, request: this._platform.request});
|
||||||
const response = await hsApi.getLoginFlows().response();
|
return new AbortableOperation(async setAbortable => {
|
||||||
|
const response = await setAbortable(hsApi.getLoginFlows()).response();
|
||||||
return this._parseLoginOptions(response, normalizedHS);
|
return this._parseLoginOptions(response, normalizedHS);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async startWithLogin(loginMethod) {
|
async startWithLogin(loginMethod) {
|
||||||
|
|
40
src/utils/AbortableOperation.ts
Normal file
40
src/utils/AbortableOperation.ts
Normal file
|
@ -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<T> = (setAbortable: (a: IAbortable) => typeof a) => T;
|
||||||
|
|
||||||
|
export class AbortableOperation<T> {
|
||||||
|
public readonly result: T;
|
||||||
|
private _abortable: IAbortable | null;
|
||||||
|
|
||||||
|
constructor(run: RunFn<T>) {
|
||||||
|
this._abortable = null;
|
||||||
|
const setAbortable = abortable => {
|
||||||
|
this._abortable = abortable;
|
||||||
|
return abortable;
|
||||||
|
};
|
||||||
|
this.result = run(setAbortable);
|
||||||
|
}
|
||||||
|
|
||||||
|
abort() {
|
||||||
|
this._abortable?.abort();
|
||||||
|
this._abortable = null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue