Skip to content

Commit 640a16e

Browse files
Return connection status on reconnect (#2072)
* Update LostConnectionHandler.ts Amending the reconnect function to return the result from the AsyncGenerator function * Update Sample app to handle connection failure on reconnect Updating the sample app to handle the status on reconnect
1 parent cfab9ee commit 640a16e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

vuu-ui/packages/vuu-data-remote/src/LostConnectionHandler.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ export class RetryGenerator {
4747
console.log("[RetryGenerator] constructor");
4848
}
4949

50-
async *[Symbol.asyncIterator](): AsyncGenerator {
50+
async *[Symbol.asyncIterator](): AsyncGenerator<
51+
"connected" | "connection-failed"
52+
> {
5153
let connected = false;
5254
do {
5355
await new Promise((resolve) =>
54-
setTimeout(resolve, this.options.interval),
56+
setTimeout(resolve, this.options.interval)
5557
);
5658
try {
5759
await this.vuuAuth.login();
@@ -62,11 +64,12 @@ export class RetryGenerator {
6264
}
6365
this.options.next();
6466
} while (!connected && this.options.interval !== -1);
65-
if (connected) {
66-
return;
67-
} else {
67+
68+
if (!connected) {
6869
yield "connection-failed";
6970
}
71+
72+
return;
7073
}
7174
}
7275

@@ -75,12 +78,15 @@ export class LostConnectionHandler {
7578
private vuuAuth: VuuAuthenticator,
7679
private retryIntervals = defaultRetryIntervals,
7780
) {}
78-
async reconnect() {
81+
async reconnect(): Promise<"connected" | "connection-failed"> {
7982
for await (const result of new RetryGenerator(
8083
this.vuuAuth,
81-
RetryOptions(this.retryIntervals),
84+
RetryOptions(this.retryIntervals)
8285
)) {
8386
console.log(` ... async iterator result = ${result}`);
87+
return result;
8488
}
89+
90+
return "connection-failed";
8591
}
8692
}

vuu-ui/sample-apps/app-vuu-example/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import { App } from "./src/App";
1313
import "@vuu-ui/vuu-icons/index.css";
1414
import "@vuu-ui/vuu-theme/index.css";
1515

16+
const CONNECTION_FAILED = 'connection-failed';
17+
const isConnectionFailedMessage = (err) => typeof err === "string" && err.includes(CONNECTION_FAILED);
18+
1619
const { websocketUrl } = await vuuConfig;
1720

1821
const vuuAuth = new VuuAuthenticator({
@@ -26,7 +29,11 @@ const lostConnectionHandler = new LostConnectionHandler(vuuAuth);
2629
const onConnectionStatusChange = (connectionStatus: ConnectionStatus) => {
2730
if (connectionStatus === "disconnected") {
2831
// do we care about the reason ?
29-
lostConnectionHandler.reconnect();
32+
lostConnectionHandler.reconnect().then(status => {
33+
if (status === CONNECTION_FAILED) {
34+
throw new Error(status);
35+
}
36+
});
3037
}
3138
};
3239

@@ -40,7 +47,7 @@ try {
4047
const root = createRoot(container);
4148
root.render(<App logout={vuuAuth.logout} user={{ username: userName }} />);
4249
} catch (err: unknown) {
43-
if (isLoginErrorMessage(err)) {
50+
if (isLoginErrorMessage(err) || isConnectionFailedMessage(err)) {
4451
const root = createRoot(container);
4552
root.render(<div>{`${err}`}</div>);
4653
} else {

0 commit comments

Comments
 (0)