Skip to content

Commit

Permalink
refactor: improve initialization sequence on routing
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriansaliou committed Oct 18, 2023
1 parent 8a45f0d commit 91bf55b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 58 deletions.
30 changes: 5 additions & 25 deletions src/broker/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -45,7 +46,6 @@ class BrokerClient {
private __delegate: BrokerDelegate;
private __credentials?: { jid: JID; password: string };
private __reconnectTimeout?: ReturnType<typeof setTimeout>;
private __isConnected = false;

constructor() {
// Initialize delegate
Expand Down Expand Up @@ -81,26 +81,6 @@ class BrokerClient {
await this.__connect(jid, password);
}

async awaitConnection(): Promise<void> {
// 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;

Expand Down Expand Up @@ -128,6 +108,10 @@ class BrokerClient {
}, afterDelay);
}

async observe(): Promise<void> {
await Broker.$muc.startObservingRooms();
}

async logout(): Promise<void> {
// Unassign JID
this.jid = undefined;
Expand All @@ -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);

Expand Down
80 changes: 47 additions & 33 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import Store from "@/store";
// PROJECT: BROKER
import Broker from "@/broker";

// PROJECT: UTILITIES
import logger from "@/utilities/logger";

/**************************************************************************
* ENUMERATIONS
* ************************************************************************* */
Expand Down Expand Up @@ -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);
}
});
}
},

Expand All @@ -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: [
Expand All @@ -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
}
]
},
Expand Down Expand Up @@ -141,34 +139,50 @@ class Router {
}
}

private __guardAuthenticated() {
private __guardAuthenticated(): Promise<void> {
// 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<void> {
// 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<void> {
// 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);
}
}
}

Expand Down

0 comments on commit 91bf55b

Please sign in to comment.