diff --git a/src/broker/client.ts b/src/broker/client.ts index d1bf3394..b0bf4184 100644 --- a/src/broker/client.ts +++ b/src/broker/client.ts @@ -18,6 +18,7 @@ import Store from "@/store"; import logger from "@/utilities/logger"; // PROJECT: BROKER +import Broker from "@/broker"; import BrokerConnection from "@/broker/connection"; import { VERSION_NAME, @@ -45,7 +46,6 @@ class BrokerClient { private __delegate: BrokerDelegate; private __credentials?: { jid: JID; password: string }; private __reconnectTimeout?: ReturnType; - private __isConnected = false; constructor() { // Initialize delegate @@ -81,26 +81,6 @@ class BrokerClient { await this.__connect(jid, password); } - async awaitConnection(): Promise { - // Already connected? - if (this.__isConnected === true) { - return Promise.resolve(); - } - - // Not already connected - const delegate = this.__delegate; - - return new Promise(resolve => { - const handler = () => { - delegate.events().off("client:connected", handler); - - resolve(); - }; - - delegate.events().on("client:connected", handler); - }); - } - reconnect(afterDelay = 0): void { const credentials = this.__credentials; @@ -128,6 +108,10 @@ class BrokerClient { }, afterDelay); } + async observe(): Promise { + await Broker.$muc.startObservingRooms(); + } + async logout(): Promise { // Unassign JID this.jid = undefined; @@ -140,15 +124,11 @@ class BrokerClient { } private __onClientConnected(): void { - this.__isConnected = true; - Store.$session.setConnected(true); Store.$session.setConnecting(false); } private __onClientDisconnected(): void { - this.__isConnected = false; - Store.$session.setConnected(false); Store.$session.setConnecting(false); diff --git a/src/router/index.ts b/src/router/index.ts index 2ab71c4a..c1c00502 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -29,6 +29,9 @@ import Store from "@/store"; // PROJECT: BROKER import Broker from "@/broker"; +// PROJECT: UTILITIES +import logger from "@/utilities/logger"; + /************************************************************************** * ENUMERATIONS * ************************************************************************* */ @@ -66,13 +69,15 @@ class Router { name: "start.login", component: StartLogin, - beforeEnter: (to, from, next) => { - this.__initializeWasmModule().then(() => { - // Ensure that user is not already logged-in - this.__guardAnonymous(); - - next(); - }); + beforeEnter: (_to, _from, next) => { + this.__initializeWasmModule() + .then(this.__guardAnonymous.bind(this)) + .then(next) + .catch(error => { + if (error) { + logger.error("Failed routing to start login", error); + } + }); } }, @@ -83,16 +88,16 @@ class Router { name: "app", component: AppBase, - beforeEnter: (to, from, next) => { - this.__initializeWasmModule().then(() => { - // Ensure that user is logged-in - this.__guardAuthenticated(); - - // Setup broker client (resume session) - this.__setupBrokerClient(); - - next(); - }); + beforeEnter: (_to, _from, next) => { + this.__initializeWasmModule() + .then(this.__guardAuthenticated.bind(this)) + .then(this.__setupBrokerClient.bind(this)) + .then(next) + .catch(error => { + if (error) { + logger.error("Failed routing to app", error); + } + }); }, children: [ @@ -105,14 +110,7 @@ class Router { { path: "inbox/:roomId/", name: "app.inbox", - component: AppInboxBase, - - beforeEnter: (to, from, next) => { - Broker.client - .awaitConnection() - .then(() => Broker.$muc.startObservingRooms()) - .then(next); - } + component: AppInboxBase } ] }, @@ -141,34 +139,50 @@ class Router { } } - private __guardAuthenticated() { + private __guardAuthenticated(): Promise { // Ensure that user is logged in (redirect to base if not) if (!Store.$account.credentials.jid) { this.__router.push({ name: "start.login" }); + + return Promise.reject(); } + + return Promise.resolve(); } - private __guardAnonymous() { + private __guardAnonymous(): Promise { // Ensure that user is not logged-in (redirect to app if so) if (Store.$account.credentials.jid) { this.__router.push({ name: "app" }); + + return Promise.reject(); } + + return Promise.resolve(); } - private __setupBrokerClient() { + private async __setupBrokerClient(): Promise { // Authenticate to broker client const credentials = Store.$account.credentials; if (credentials.jid) { - Broker.client - .authenticate(new JID(credentials.jid), credentials.password) - .catch(() => { - // Ignore authentication errors here - }); + try { + // Authenticate client + await Broker.client.authenticate( + new JID(credentials.jid), + credentials.password + ); + + // Start observers + await Broker.client.observe(); + } catch (error) { + // Ignore authentication errors here + logger.error("Error setting up broker client", error); + } } }