forked from mystiq/hydrogen-web
first draft of fully typescriptified LoginViewModel.ts
This commit is contained in:
parent
204948db64
commit
d7657dcc4d
6 changed files with 79 additions and 49 deletions
|
@ -19,6 +19,7 @@ module.exports = {
|
||||||
],
|
],
|
||||||
rules: {
|
rules: {
|
||||||
"@typescript-eslint/no-floating-promises": 2,
|
"@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.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Options, ViewModel} from "./ViewModel";
|
import {Options as BaseOptions, ViewModel} from "./ViewModel";
|
||||||
import {Client} from "../matrix/Client.js";
|
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 _sessionId: string;
|
||||||
private _busy: boolean;
|
private _busy: boolean;
|
||||||
private _showConfirm: boolean;
|
private _showConfirm: boolean;
|
||||||
private _error?: Error;
|
private _error?: Error;
|
||||||
|
|
||||||
constructor(options: LogoutOptions) {
|
constructor(options: Options) {
|
||||||
super(options);
|
super(options);
|
||||||
this._sessionId = options.sessionId;
|
this._sessionId = options.sessionId;
|
||||||
this._busy = false;
|
this._busy = false;
|
||||||
|
|
|
@ -17,7 +17,7 @@ limitations under the License.
|
||||||
import {Client} from "../matrix/Client.js";
|
import {Client} from "../matrix/Client.js";
|
||||||
import {SessionViewModel} from "./session/SessionViewModel.js";
|
import {SessionViewModel} from "./session/SessionViewModel.js";
|
||||||
import {SessionLoadViewModel} from "./SessionLoadViewModel.js";
|
import {SessionLoadViewModel} from "./SessionLoadViewModel.js";
|
||||||
import {LoginViewModel} from "./login/LoginViewModel.js";
|
import {LoginViewModel} from "./login/LoginViewModel.ts";
|
||||||
import {LogoutViewModel} from "./LogoutViewModel";
|
import {LogoutViewModel} from "./LogoutViewModel";
|
||||||
import {SessionPickerViewModel} from "./SessionPickerViewModel.js";
|
import {SessionPickerViewModel} from "./SessionPickerViewModel.js";
|
||||||
import {ViewModel} from "./ViewModel";
|
import {ViewModel} from "./ViewModel";
|
||||||
|
|
|
@ -62,7 +62,7 @@ export class ViewModel<O extends Options = Options> extends EventEmitter<{change
|
||||||
const segmentObservable = this.navigation.observe(type);
|
const segmentObservable = this.navigation.observe(type);
|
||||||
const unsubscribe = segmentObservable.subscribe((value: string | true | undefined) => {
|
const unsubscribe = segmentObservable.subscribe((value: string | true | undefined) => {
|
||||||
onChange(value, type);
|
onChange(value, type);
|
||||||
})
|
});
|
||||||
this.track(unsubscribe);
|
this.track(unsubscribe);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ export class ViewModel<O extends Options = Options> extends EventEmitter<{change
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
emitChange(changedProps: any): void {
|
emitChange(changedProps?: any): void {
|
||||||
if (this._options.emitChange) {
|
if (this._options.emitChange) {
|
||||||
this._options.emitChange(changedProps);
|
this._options.emitChange(changedProps);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -15,34 +15,51 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Client} from "../../matrix/Client.js";
|
import {Client} from "../../matrix/Client.js";
|
||||||
import {ViewModel} from "../ViewModel";
|
import {Options as BaseOptions, ViewModel} from "../ViewModel";
|
||||||
import {PasswordLoginViewModel} from "./PasswordLoginViewModel.js";
|
import {PasswordLoginViewModel} from "./PasswordLoginViewModel.js";
|
||||||
import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel.js";
|
import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel.js";
|
||||||
import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel.js";
|
import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel.js";
|
||||||
import {LoadStatus} from "../../matrix/Client.js";
|
import {LoadStatus} from "../../matrix/Client.js";
|
||||||
import {SessionLoadViewModel} from "../SessionLoadViewModel.js";
|
import {SessionLoadViewModel} from "../SessionLoadViewModel.js";
|
||||||
|
|
||||||
export class LoginViewModel extends ViewModel {
|
// TODO(isaiah): make these default exported
|
||||||
constructor(options) {
|
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);
|
super(options);
|
||||||
const {ready, defaultHomeserver, loginToken} = options;
|
const {ready, defaultHomeserver, loginToken} = options;
|
||||||
this._ready = ready;
|
this._ready = ready;
|
||||||
this._loginToken = loginToken;
|
this._loginToken = loginToken;
|
||||||
this._client = new Client(this.platform);
|
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._homeserver = defaultHomeserver;
|
||||||
this._queriedHomeserver = null;
|
void this._initViewModels();
|
||||||
this._errorMessage = "";
|
|
||||||
this._hideHomeserver = false;
|
|
||||||
this._isBusy = false;
|
|
||||||
this._abortHomeserverQueryTimeout = null;
|
|
||||||
this._abortQueryOperation = null;
|
|
||||||
this._initViewModels();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get passwordLoginViewModel() { return this._passwordLoginViewModel; }
|
get passwordLoginViewModel() { return this._passwordLoginViewModel; }
|
||||||
|
@ -67,7 +84,7 @@ export class LoginViewModel extends ViewModel {
|
||||||
this.childOptions(
|
this.childOptions(
|
||||||
{
|
{
|
||||||
client: this._client,
|
client: this._client,
|
||||||
attemptLogin: loginMethod => this.attemptLogin(loginMethod),
|
attemptLogin: (loginMethod: PasswordLoginMethod) => this.attemptLogin(loginMethod),
|
||||||
loginToken: this._loginToken
|
loginToken: this._loginToken
|
||||||
})));
|
})));
|
||||||
this.emitChange("completeSSOLoginViewModel");
|
this.emitChange("completeSSOLoginViewModel");
|
||||||
|
@ -81,7 +98,7 @@ export class LoginViewModel extends ViewModel {
|
||||||
this._passwordLoginViewModel = this.track(new PasswordLoginViewModel(
|
this._passwordLoginViewModel = this.track(new PasswordLoginViewModel(
|
||||||
this.childOptions({
|
this.childOptions({
|
||||||
loginOptions: this._loginOptions,
|
loginOptions: this._loginOptions,
|
||||||
attemptLogin: loginMethod => this.attemptLogin(loginMethod)
|
attemptLogin: (loginMethod: PasswordLoginMethod) => this.attemptLogin(loginMethod)
|
||||||
})));
|
})));
|
||||||
this.emitChange("passwordLoginViewModel");
|
this.emitChange("passwordLoginViewModel");
|
||||||
}
|
}
|
||||||
|
@ -93,23 +110,23 @@ export class LoginViewModel extends ViewModel {
|
||||||
this.emitChange("startSSOLoginViewModel");
|
this.emitChange("startSSOLoginViewModel");
|
||||||
}
|
}
|
||||||
|
|
||||||
_showError(message) {
|
_showError(message: string) {
|
||||||
this._errorMessage = message;
|
this._errorMessage = message;
|
||||||
this.emitChange("errorMessage");
|
this.emitChange("errorMessage");
|
||||||
}
|
}
|
||||||
|
|
||||||
_setBusy(status) {
|
_setBusy(status: boolean) {
|
||||||
this._isBusy = status;
|
this._isBusy = status;
|
||||||
this._passwordLoginViewModel?.setBusy(status);
|
this._passwordLoginViewModel?.setBusy(status);
|
||||||
this._startSSOLoginViewModel?.setBusy(status);
|
this._startSSOLoginViewModel?.setBusy(status);
|
||||||
this.emitChange("isBusy");
|
this.emitChange("isBusy");
|
||||||
}
|
}
|
||||||
|
|
||||||
async attemptLogin(loginMethod) {
|
async attemptLogin(loginMethod: PasswordLoginMethod) {
|
||||||
this._setBusy(true);
|
this._setBusy(true);
|
||||||
this._client.startWithLogin(loginMethod, {inspectAccountSetup: true});
|
await this._client.startWithLogin(loginMethod, {inspectAccountSetup: true});
|
||||||
const loadStatus = this._client.loadStatus;
|
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;
|
await handle.promise;
|
||||||
this._setBusy(false);
|
this._setBusy(false);
|
||||||
const status = loadStatus.get();
|
const status = loadStatus.get();
|
||||||
|
@ -119,11 +136,11 @@ export class LoginViewModel extends ViewModel {
|
||||||
this._hideHomeserver = true;
|
this._hideHomeserver = true;
|
||||||
this.emitChange("hideHomeserver");
|
this.emitChange("hideHomeserver");
|
||||||
this._disposeViewModels();
|
this._disposeViewModels();
|
||||||
this._createLoadViewModel();
|
await this._createLoadViewModel();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_createLoadViewModel() {
|
async _createLoadViewModel() {
|
||||||
this._loadViewModelSubscription = this.disposeTracked(this._loadViewModelSubscription);
|
this._loadViewModelSubscription = this.disposeTracked(this._loadViewModelSubscription);
|
||||||
this._loadViewModel = this.disposeTracked(this._loadViewModel);
|
this._loadViewModel = this.disposeTracked(this._loadViewModel);
|
||||||
this._loadViewModel = this.track(
|
this._loadViewModel = this.track(
|
||||||
|
@ -139,7 +156,7 @@ export class LoginViewModel extends ViewModel {
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
this._loadViewModel.start();
|
await this._loadViewModel.start();
|
||||||
this.emitChange("loadViewModel");
|
this.emitChange("loadViewModel");
|
||||||
this._loadViewModelSubscription = this.track(
|
this._loadViewModelSubscription = this.track(
|
||||||
this._loadViewModel.disposableOn("change", () => {
|
this._loadViewModel.disposableOn("change", () => {
|
||||||
|
@ -152,7 +169,7 @@ export class LoginViewModel extends ViewModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
_disposeViewModels() {
|
_disposeViewModels() {
|
||||||
this._startSSOLoginViewModel = this.disposeTracked(this._ssoLoginViewModel);
|
this._startSSOLoginViewModel = this.disposeTracked(this._startSSOLoginViewModel);
|
||||||
this._passwordLoginViewModel = this.disposeTracked(this._passwordLoginViewModel);
|
this._passwordLoginViewModel = this.disposeTracked(this._passwordLoginViewModel);
|
||||||
this._completeSSOLoginViewModel = this.disposeTracked(this._completeSSOLoginViewModel);
|
this._completeSSOLoginViewModel = this.disposeTracked(this._completeSSOLoginViewModel);
|
||||||
this.emitChange("disposeViewModels");
|
this.emitChange("disposeViewModels");
|
||||||
|
@ -161,8 +178,8 @@ export class LoginViewModel extends ViewModel {
|
||||||
async setHomeserver(newHomeserver) {
|
async setHomeserver(newHomeserver) {
|
||||||
this._homeserver = newHomeserver;
|
this._homeserver = newHomeserver;
|
||||||
// clear everything set by queryHomeserver
|
// clear everything set by queryHomeserver
|
||||||
this._loginOptions = null;
|
this._loginOptions = undefined;
|
||||||
this._queriedHomeserver = null;
|
this._queriedHomeserver = undefined;
|
||||||
this._showError("");
|
this._showError("");
|
||||||
this._disposeViewModels();
|
this._disposeViewModels();
|
||||||
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
||||||
|
@ -181,7 +198,7 @@ export class LoginViewModel extends ViewModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._abortHomeserverQueryTimeout = this.disposeTracked(this._abortHomeserverQueryTimeout);
|
this._abortHomeserverQueryTimeout = this.disposeTracked(this._abortHomeserverQueryTimeout);
|
||||||
this.queryHomeserver();
|
await this.queryHomeserver();
|
||||||
}
|
}
|
||||||
|
|
||||||
async queryHomeserver() {
|
async queryHomeserver() {
|
||||||
|
@ -210,7 +227,7 @@ export class LoginViewModel extends ViewModel {
|
||||||
if (e.name === "AbortError") {
|
if (e.name === "AbortError") {
|
||||||
return; //aborted, bail out
|
return; //aborted, bail out
|
||||||
} else {
|
} else {
|
||||||
this._loginOptions = null;
|
this._loginOptions = undefined;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
||||||
|
@ -228,12 +245,22 @@ export class LoginViewModel extends ViewModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose() {
|
async dispose() {
|
||||||
super.dispose();
|
super.dispose();
|
||||||
if (this._client) {
|
if (this._client) {
|
||||||
// if we move away before we're done with initial sync
|
// if we move away before we're done with initial sync
|
||||||
// delete the session
|
// 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) {
|
_parseLoginOptions(options, homeserver) {
|
||||||
/*
|
/*
|
||||||
Take server response and return new object which has two props password and sso which
|
Take server response and return new object which has two props password and sso which
|
||||||
|
|
Loading…
Reference in a new issue