onLoading join

pull/1225/head
Vincent 5 years ago
parent b05d2e6725
commit a005aa8a2e

@ -462,7 +462,6 @@ export class LeftPaneMessageSection extends React.Component<Props, State> {
return; return;
} }
// Already connected? // Already connected?
if (Boolean(await OpenGroup.getConversation(serverUrl))) { if (Boolean(await OpenGroup.getConversation(serverUrl))) {
window.pushToast({ window.pushToast({
@ -474,15 +473,12 @@ export class LeftPaneMessageSection extends React.Component<Props, State> {
return; return;
} }
const successPromise = OpenGroup.join(serverUrl); // Connect to server
const timeoutPromise = new Promise((_resolve, reject) => const successPromise = OpenGroup.join(serverUrl, () => {
// tslint:disable-next-line: no-unnecessary-callback-wrapper no-void-expression this.setState({ loading: true });
setTimeout(() => reject(), window.CONSTANTS.MAX_CONNECTION_DURATION) });
);
// Connect to server with timeout. successPromise.then(() => {
this.setState({ loading: true });
await Promise.race([successPromise, timeoutPromise]).then(() => {
this.handleToggleOverlay(undefined); this.handleToggleOverlay(undefined);
this.setState({ this.setState({
loading: false, loading: false,
@ -501,8 +497,8 @@ export class LeftPaneMessageSection extends React.Component<Props, State> {
}); });
window.pushToast({ window.pushToast({
title: window.i18n('attemptedConnectionTimeout'), title: window.i18n('connectToServerFail'),
id: 'attemptedConnectionTimeout', id: 'connectToServerFail',
type: 'error', type: 'error',
}); });
}); });

@ -70,7 +70,9 @@ export class OpenGroup {
return new OpenGroup(openGroupParams); return new OpenGroup(openGroupParams);
} }
public static async join(server: string): Promise<OpenGroup | undefined> { public static async join(server: string, onLoading?: any): Promise<OpenGroup | undefined> {
// onLoading called when the server begins connecting - after passing every guard
const prefixedServer = OpenGroup.prefixify(server); const prefixedServer = OpenGroup.prefixify(server);
if (!OpenGroup.validate(server)) { if (!OpenGroup.validate(server)) {
return; return;
@ -94,12 +96,16 @@ export class OpenGroup {
} }
} }
// Try to connect to server
try { try {
if (onLoading) {
onLoading();
}
conversation = await window.attemptConnection(prefixedServer, channel); conversation = await window.attemptConnection(prefixedServer, channel);
conversationId = conversation?.cid; conversationId = conversation?.cid;
} catch (e) { } catch (e) {
console.warn(e); throw new Error(e);
return;
} }
// Do we want to add conversation as a property of OpenGroup? // Do we want to add conversation as a property of OpenGroup?

Loading…
Cancel
Save