mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-11-20 03:25:52 +01:00
first draft of fully typescriptified LoginViewModel.ts
This commit is contained in:
parent
204948db64
commit
d7657dcc4d
@ -19,6 +19,7 @@ module.exports = {
|
||||
],
|
||||
rules: {
|
||||
"@typescript-eslint/no-floating-promises": 2,
|
||||
"@typescript-eslint/no-misused-promises": 2
|
||||
"@typescript-eslint/no-misused-promises": 2,
|
||||
"semi": ["error", "always"]
|
||||
}
|
||||
};
|
||||
|
@ -14,18 +14,18 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import {Options, ViewModel} from "./ViewModel";
|
||||
import {Options as BaseOptions, ViewModel} from "./ViewModel";
|
||||
import {Client} from "../matrix/Client.js";
|
||||
|
||||
type LogoutOptions = { sessionId: string; } & Options;
|
||||
type Options = { sessionId: string; } & BaseOptions;
|
||||
|
||||
export class LogoutViewModel extends ViewModel<LogoutOptions> {
|
||||
export class LogoutViewModel extends ViewModel<Options> {
|
||||
private _sessionId: string;
|
||||
private _busy: boolean;
|
||||
private _showConfirm: boolean;
|
||||
private _error?: Error;
|
||||
|
||||
constructor(options: LogoutOptions) {
|
||||
constructor(options: Options) {
|
||||
super(options);
|
||||
this._sessionId = options.sessionId;
|
||||
this._busy = false;
|
||||
|
@ -17,7 +17,7 @@ limitations under the License.
|
||||
import {Client} from "../matrix/Client.js";
|
||||
import {SessionViewModel} from "./session/SessionViewModel.js";
|
||||
import {SessionLoadViewModel} from "./SessionLoadViewModel.js";
|
||||
import {LoginViewModel} from "./login/LoginViewModel.js";
|
||||
import {LoginViewModel} from "./login/LoginViewModel.ts";
|
||||
import {LogoutViewModel} from "./LogoutViewModel";
|
||||
import {SessionPickerViewModel} from "./SessionPickerViewModel.js";
|
||||
import {ViewModel} from "./ViewModel";
|
||||
@ -118,7 +118,7 @@ export class RootViewModel extends ViewModel {
|
||||
// but we also want the change of screen to go through the navigation
|
||||
// so we store the session container in a temporary variable that will be
|
||||
// consumed by _applyNavigation, triggered by the navigation change
|
||||
//
|
||||
//
|
||||
// Also, we should not call _setSection before the navigation is in the correct state,
|
||||
// as url creation (e.g. in RoomTileViewModel)
|
||||
// won't be using the correct navigation base path.
|
||||
|
@ -62,7 +62,7 @@ export class ViewModel<O extends Options = Options> extends EventEmitter<{change
|
||||
const segmentObservable = this.navigation.observe(type);
|
||||
const unsubscribe = segmentObservable.subscribe((value: string | true | undefined) => {
|
||||
onChange(value, type);
|
||||
})
|
||||
});
|
||||
this.track(unsubscribe);
|
||||
}
|
||||
|
||||
@ -100,7 +100,7 @@ export class ViewModel<O extends Options = Options> extends EventEmitter<{change
|
||||
|
||||
// TODO: this will need to support binding
|
||||
// if any of the expr is a function, assume the function is a binding, and return a binding function ourselves
|
||||
//
|
||||
//
|
||||
// translated string should probably always be bindings, unless we're fine with a refresh when changing the language?
|
||||
// we probably are, if we're using routing with a url, we could just refresh.
|
||||
i18n(parts: TemplateStringsArray, ...expr: any[]) {
|
||||
@ -115,7 +115,7 @@ export class ViewModel<O extends Options = Options> extends EventEmitter<{change
|
||||
return result;
|
||||
}
|
||||
|
||||
emitChange(changedProps: any): void {
|
||||
emitChange(changedProps?: any): void {
|
||||
if (this._options.emitChange) {
|
||||
this._options.emitChange(changedProps);
|
||||
} else {
|
||||
|
@ -15,34 +15,51 @@ limitations under the License.
|
||||
*/
|
||||
|
||||
import {Client} from "../../matrix/Client.js";
|
||||
import {ViewModel} from "../ViewModel";
|
||||
import {Options as BaseOptions, ViewModel} from "../ViewModel";
|
||||
import {PasswordLoginViewModel} from "./PasswordLoginViewModel.js";
|
||||
import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel.js";
|
||||
import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel.js";
|
||||
import {LoadStatus} from "../../matrix/Client.js";
|
||||
import {SessionLoadViewModel} from "../SessionLoadViewModel.js";
|
||||
|
||||
export class LoginViewModel extends ViewModel {
|
||||
constructor(options) {
|
||||
// TODO(isaiah): make these default exported
|
||||
import {PasswordLoginMethod} from "../../matrix/login/PasswordLoginMethod";
|
||||
import {SSOLoginHelper} from "../../matrix/login/SSOLoginHelper";
|
||||
import {TokenLoginMethod} from "../../matrix/login/TokenLoginMethod";
|
||||
|
||||
type Options = {
|
||||
defaultHomeserver: string;
|
||||
ready: ReadyFn;
|
||||
loginToken?: string;
|
||||
} & BaseOptions;
|
||||
|
||||
export class LoginViewModel extends ViewModel<Options> {
|
||||
private _ready: ReadyFn;
|
||||
private _loginToken?: string;
|
||||
private _client: Client;
|
||||
private _loginOptions?: LoginOptions;
|
||||
private _passwordLoginViewModel?: PasswordLoginViewModel;
|
||||
private _startSSOLoginViewModel?: StartSSOLoginViewModel;
|
||||
private _completeSSOLoginViewModel?: CompleteSSOLoginViewModel;
|
||||
private _loadViewModel?: SessionLoadViewModel;
|
||||
private _loadViewModelSubscription?: () => void;
|
||||
private _homeserver: string;
|
||||
private _queriedHomeserver?: string;
|
||||
private _abortHomeserverQueryTimeout?: () => void;
|
||||
private _abortQueryOperation?: () => void;
|
||||
|
||||
private _hideHomeserver = false;
|
||||
private _isBusy = false;
|
||||
private _errorMessage = "";
|
||||
|
||||
constructor(options: Readonly<Options>) {
|
||||
super(options);
|
||||
const {ready, defaultHomeserver, loginToken} = options;
|
||||
this._ready = ready;
|
||||
this._loginToken = loginToken;
|
||||
this._client = new Client(this.platform);
|
||||
this._loginOptions = null;
|
||||
this._passwordLoginViewModel = null;
|
||||
this._startSSOLoginViewModel = null;
|
||||
this._completeSSOLoginViewModel = null;
|
||||
this._loadViewModel = null;
|
||||
this._loadViewModelSubscription = null;
|
||||
this._homeserver = defaultHomeserver;
|
||||
this._queriedHomeserver = null;
|
||||
this._errorMessage = "";
|
||||
this._hideHomeserver = false;
|
||||
this._isBusy = false;
|
||||
this._abortHomeserverQueryTimeout = null;
|
||||
this._abortQueryOperation = null;
|
||||
this._initViewModels();
|
||||
void this._initViewModels();
|
||||
}
|
||||
|
||||
get passwordLoginViewModel() { return this._passwordLoginViewModel; }
|
||||
@ -67,7 +84,7 @@ export class LoginViewModel extends ViewModel {
|
||||
this.childOptions(
|
||||
{
|
||||
client: this._client,
|
||||
attemptLogin: loginMethod => this.attemptLogin(loginMethod),
|
||||
attemptLogin: (loginMethod: PasswordLoginMethod) => this.attemptLogin(loginMethod),
|
||||
loginToken: this._loginToken
|
||||
})));
|
||||
this.emitChange("completeSSOLoginViewModel");
|
||||
@ -81,7 +98,7 @@ export class LoginViewModel extends ViewModel {
|
||||
this._passwordLoginViewModel = this.track(new PasswordLoginViewModel(
|
||||
this.childOptions({
|
||||
loginOptions: this._loginOptions,
|
||||
attemptLogin: loginMethod => this.attemptLogin(loginMethod)
|
||||
attemptLogin: (loginMethod: PasswordLoginMethod) => this.attemptLogin(loginMethod)
|
||||
})));
|
||||
this.emitChange("passwordLoginViewModel");
|
||||
}
|
||||
@ -93,23 +110,23 @@ export class LoginViewModel extends ViewModel {
|
||||
this.emitChange("startSSOLoginViewModel");
|
||||
}
|
||||
|
||||
_showError(message) {
|
||||
_showError(message: string) {
|
||||
this._errorMessage = message;
|
||||
this.emitChange("errorMessage");
|
||||
}
|
||||
|
||||
_setBusy(status) {
|
||||
_setBusy(status: boolean) {
|
||||
this._isBusy = status;
|
||||
this._passwordLoginViewModel?.setBusy(status);
|
||||
this._startSSOLoginViewModel?.setBusy(status);
|
||||
this.emitChange("isBusy");
|
||||
}
|
||||
|
||||
async attemptLogin(loginMethod) {
|
||||
async attemptLogin(loginMethod: PasswordLoginMethod) {
|
||||
this._setBusy(true);
|
||||
this._client.startWithLogin(loginMethod, {inspectAccountSetup: true});
|
||||
await this._client.startWithLogin(loginMethod, {inspectAccountSetup: true});
|
||||
const loadStatus = this._client.loadStatus;
|
||||
const handle = loadStatus.waitFor(status => status !== LoadStatus.Login);
|
||||
const handle = loadStatus.waitFor((status: LoadStatus) => status !== LoadStatus.Login);
|
||||
await handle.promise;
|
||||
this._setBusy(false);
|
||||
const status = loadStatus.get();
|
||||
@ -119,11 +136,11 @@ export class LoginViewModel extends ViewModel {
|
||||
this._hideHomeserver = true;
|
||||
this.emitChange("hideHomeserver");
|
||||
this._disposeViewModels();
|
||||
this._createLoadViewModel();
|
||||
await this._createLoadViewModel();
|
||||
return null;
|
||||
}
|
||||
|
||||
_createLoadViewModel() {
|
||||
async _createLoadViewModel() {
|
||||
this._loadViewModelSubscription = this.disposeTracked(this._loadViewModelSubscription);
|
||||
this._loadViewModel = this.disposeTracked(this._loadViewModel);
|
||||
this._loadViewModel = this.track(
|
||||
@ -139,7 +156,7 @@ export class LoginViewModel extends ViewModel {
|
||||
})
|
||||
)
|
||||
);
|
||||
this._loadViewModel.start();
|
||||
await this._loadViewModel.start();
|
||||
this.emitChange("loadViewModel");
|
||||
this._loadViewModelSubscription = this.track(
|
||||
this._loadViewModel.disposableOn("change", () => {
|
||||
@ -152,7 +169,7 @@ export class LoginViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
_disposeViewModels() {
|
||||
this._startSSOLoginViewModel = this.disposeTracked(this._ssoLoginViewModel);
|
||||
this._startSSOLoginViewModel = this.disposeTracked(this._startSSOLoginViewModel);
|
||||
this._passwordLoginViewModel = this.disposeTracked(this._passwordLoginViewModel);
|
||||
this._completeSSOLoginViewModel = this.disposeTracked(this._completeSSOLoginViewModel);
|
||||
this.emitChange("disposeViewModels");
|
||||
@ -161,8 +178,8 @@ export class LoginViewModel extends ViewModel {
|
||||
async setHomeserver(newHomeserver) {
|
||||
this._homeserver = newHomeserver;
|
||||
// clear everything set by queryHomeserver
|
||||
this._loginOptions = null;
|
||||
this._queriedHomeserver = null;
|
||||
this._loginOptions = undefined;
|
||||
this._queriedHomeserver = undefined;
|
||||
this._showError("");
|
||||
this._disposeViewModels();
|
||||
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
||||
@ -181,9 +198,9 @@ export class LoginViewModel extends ViewModel {
|
||||
}
|
||||
}
|
||||
this._abortHomeserverQueryTimeout = this.disposeTracked(this._abortHomeserverQueryTimeout);
|
||||
this.queryHomeserver();
|
||||
await this.queryHomeserver();
|
||||
}
|
||||
|
||||
|
||||
async queryHomeserver() {
|
||||
// don't repeat a query we've just done
|
||||
if (this._homeserver === this._queriedHomeserver || this._homeserver === "") {
|
||||
@ -210,7 +227,7 @@ export class LoginViewModel extends ViewModel {
|
||||
if (e.name === "AbortError") {
|
||||
return; //aborted, bail out
|
||||
} else {
|
||||
this._loginOptions = null;
|
||||
this._loginOptions = undefined;
|
||||
}
|
||||
} finally {
|
||||
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
||||
@ -221,19 +238,29 @@ export class LoginViewModel extends ViewModel {
|
||||
if (this._loginOptions.password) { this._showPasswordLogin(); }
|
||||
if (!this._loginOptions.sso && !this._loginOptions.password) {
|
||||
this._showError("This homeserver supports neither SSO nor password based login flows");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._showError(`Could not query login methods supported by ${this.homeserver}`);
|
||||
}
|
||||
}
|
||||
|
||||
dispose() {
|
||||
async dispose() {
|
||||
super.dispose();
|
||||
if (this._client) {
|
||||
// if we move away before we're done with initial sync
|
||||
// delete the session
|
||||
this._client.deleteSession();
|
||||
await this._client.deleteSession();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ReadyFn = (client: Client) => void;
|
||||
|
||||
// TODO: move to Client.js when its converted to typescript.
|
||||
type LoginOptions = {
|
||||
homeserver: string;
|
||||
password?: (username: string, password: string) => PasswordLoginMethod;
|
||||
sso?: SSOLoginHelper;
|
||||
token?: (loginToken: string) => TokenLoginMethod;
|
||||
};
|
@ -100,6 +100,8 @@ export class Client {
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: When converted to typescript this should return the same type
|
||||
// as this._loginOptions is in LoginViewModel.ts (LoginOptions).
|
||||
_parseLoginOptions(options, homeserver) {
|
||||
/*
|
||||
Take server response and return new object which has two props password and sso which
|
||||
@ -136,7 +138,7 @@ export class Client {
|
||||
const request = this._platform.request;
|
||||
const hsApi = new HomeServerApi({homeserver, request});
|
||||
const registration = new Registration(hsApi, {
|
||||
username,
|
||||
username,
|
||||
password,
|
||||
initialDeviceDisplayName,
|
||||
},
|
||||
@ -196,7 +198,7 @@ export class Client {
|
||||
sessionInfo.deviceId = dehydratedDevice.deviceId;
|
||||
}
|
||||
}
|
||||
await this._platform.sessionInfoStorage.add(sessionInfo);
|
||||
await this._platform.sessionInfoStorage.add(sessionInfo);
|
||||
// loading the session can only lead to
|
||||
// LoadStatus.Error in case of an error,
|
||||
// so separate try/catch
|
||||
@ -266,7 +268,7 @@ export class Client {
|
||||
this._status.set(LoadStatus.SessionSetup);
|
||||
await log.wrap("createIdentity", log => this._session.createIdentity(log));
|
||||
}
|
||||
|
||||
|
||||
this._sync = new Sync({hsApi: this._requestScheduler.hsApi, storage: this._storage, session: this._session, logger: this._platform.logger});
|
||||
// notify sync and session when back online
|
||||
this._reconnectSubscription = this._reconnector.connectionStatus.subscribe(state => {
|
||||
@ -311,7 +313,7 @@ export class Client {
|
||||
this._waitForFirstSyncHandle = this._sync.status.waitFor(s => {
|
||||
if (s === SyncStatus.Stopped) {
|
||||
// keep waiting if there is a ConnectionError
|
||||
// as the reconnector above will call
|
||||
// as the reconnector above will call
|
||||
// sync.start again to retry in this case
|
||||
return this._sync.error?.name !== "ConnectionError";
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user