add a timeout function to promiseUtils for attemptConnection

pull/1306/head
Audric Ackermann 5 years ago
parent eb3ee8f6b0
commit cf3352d0af
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -103,7 +103,7 @@ export class SessionClosableOverlay extends React.Component<Props, State> {
}); });
} }
// tslint:disable-next-line max-func-body-length */ // tslint:disable-next-line max-func-body-length cyclomatic-complexity */
public render(): JSX.Element { public render(): JSX.Element {
const { const {
overlayMode, overlayMode,

@ -1,4 +1,5 @@
import { ConversationModel } from '../../../js/models/conversations'; import { ConversationModel } from '../../../js/models/conversations';
import { PromiseUtils } from '../utils';
interface OpenGroupParams { interface OpenGroupParams {
server: string; server: string;
@ -126,8 +127,14 @@ export class OpenGroup {
onLoading(); onLoading();
} }
conversation = await window.attemptConnection(prefixedServer, channel); conversation = await PromiseUtils.timeout(
conversationId = conversation?.cid; window.attemptConnection(prefixedServer, channel),
5000
);
if (!conversation) {
throw new Error(window.i18n('connectToServerFail'));
}
conversationId = (conversation as any)?.cid;
} catch (e) { } catch (e) {
throw new Error(e); throw new Error(e);
} }

@ -14,13 +14,13 @@ async function toPromise<T>(value: Return<T>): Promise<T> {
*/ */
export async function waitForTask<T>( export async function waitForTask<T>(
task: (done: SimpleFunction<T>) => Return<void>, task: (done: SimpleFunction<T>) => Return<void>,
timeout: number = 2000 timeoutMs: number = 2000
): Promise<T> { ): Promise<T> {
const timeoutPromise = new Promise<T>((_, rej) => { const timeoutPromise = new Promise<T>((_, rej) => {
const wait = setTimeout(() => { const wait = setTimeout(() => {
clearTimeout(wait); clearTimeout(wait);
rej(new Error('Task timed out.')); rej(new Error('Task timed out.'));
}, timeout); }, timeoutMs);
}); });
const taskPromise = new Promise(async (res, rej) => { const taskPromise = new Promise(async (res, rej) => {
@ -35,7 +35,7 @@ export async function waitForTask<T>(
} }
export interface PollOptions { export interface PollOptions {
timeout: number; timeoutMs: number;
interval: number; interval: number;
} }
@ -51,16 +51,16 @@ export async function poll(
options: Partial<PollOptions> = {} options: Partial<PollOptions> = {}
): Promise<void> { ): Promise<void> {
const defaults: PollOptions = { const defaults: PollOptions = {
timeout: 2000, timeoutMs: 2000,
interval: 100, interval: 100,
}; };
const { timeout, interval } = { const { timeoutMs, interval } = {
...defaults, ...defaults,
...options, ...options,
}; };
const endTime = Date.now() + timeout; const endTime = Date.now() + timeoutMs;
let stop = false; let stop = false;
const finish = () => { const finish = () => {
stop = true; stop = true;
@ -101,7 +101,7 @@ export async function poll(
*/ */
export async function waitUntil( export async function waitUntil(
check: () => Return<boolean>, check: () => Return<boolean>,
timeout: number = 2000 timeoutMs: number = 2000
) { ) {
// This is causing unhandled promise rejection somewhere in MessageQueue tests // This is causing unhandled promise rejection somewhere in MessageQueue tests
return poll( return poll(
@ -112,8 +112,22 @@ export async function waitUntil(
} }
}, },
{ {
timeout, timeoutMs,
interval: timeout / 20, interval: timeoutMs / 20,
} }
); );
} }
export async function timeout<T>(
promise: Promise<T>,
timeoutMs: number = 2000
): Promise<T> {
const timeoutPromise = new Promise<T>((_, rej) => {
const wait = setTimeout(() => {
clearTimeout(wait);
rej(new Error('Task timed out.'));
}, timeoutMs);
});
return Promise.race([timeoutPromise, promise]);
}

@ -60,7 +60,7 @@ describe('Promise Utils', () => {
const completionSpy = sandbox.spy(); const completionSpy = sandbox.spy();
const task = (_done: any) => undefined; const task = (_done: any) => undefined;
const promise = PromiseUtils.poll(task, { timeout: 1 }); const promise = PromiseUtils.poll(task, { timeoutMs: 1 });
await expect(promise).to.be.rejectedWith('Periodic check timeout'); await expect(promise).to.be.rejectedWith('Periodic check timeout');
expect(pollSpy.callCount).to.equal(1); expect(pollSpy.callCount).to.equal(1);
@ -82,7 +82,7 @@ describe('Promise Utils', () => {
} }
}; };
const promise = PromiseUtils.poll(task, { timeout, interval }); const promise = PromiseUtils.poll(task, { timeoutMs: timeout, interval });
await expect(promise).to.be.fulfilled; await expect(promise).to.be.fulfilled;
expect(pollSpy.callCount).to.equal(1); expect(pollSpy.callCount).to.equal(1);

Loading…
Cancel
Save