You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
move fn to http-api and add jsdoc
This commit is contained in:
@@ -35,7 +35,12 @@ import {StubStore} from "./store/stub";
|
|||||||
import {createNewMatrixCall} from "./webrtc/call";
|
import {createNewMatrixCall} from "./webrtc/call";
|
||||||
import * as utils from './utils';
|
import * as utils from './utils';
|
||||||
import {sleep} from './utils';
|
import {sleep} from './utils';
|
||||||
import {MatrixError, PREFIX_MEDIA_R0, PREFIX_UNSTABLE} from "./http-api";
|
import {
|
||||||
|
MatrixError,
|
||||||
|
PREFIX_MEDIA_R0,
|
||||||
|
PREFIX_UNSTABLE,
|
||||||
|
retryNetworkOperation,
|
||||||
|
} from "./http-api";
|
||||||
import {getHttpUriForMxc} from "./content-repo";
|
import {getHttpUriForMxc} from "./content-repo";
|
||||||
import * as ContentHelpers from "./content-helpers";
|
import * as ContentHelpers from "./content-helpers";
|
||||||
import * as olmlib from "./crypto/olmlib";
|
import * as olmlib from "./crypto/olmlib";
|
||||||
@@ -49,7 +54,6 @@ import {randomString} from './randomstring';
|
|||||||
import {PushProcessor} from "./pushprocessor";
|
import {PushProcessor} from "./pushprocessor";
|
||||||
import {encodeBase64, decodeBase64} from "./crypto/olmlib";
|
import {encodeBase64, decodeBase64} from "./crypto/olmlib";
|
||||||
import { User } from "./models/user";
|
import { User } from "./models/user";
|
||||||
import {retryNetworkOperation} from "./utils";
|
|
||||||
|
|
||||||
const SCROLLBACK_DELAY_MS = 3000;
|
const SCROLLBACK_DELAY_MS = 3000;
|
||||||
export const CRYPTO_ENABLED = isCryptoAvailable();
|
export const CRYPTO_ENABLED = isCryptoAvailable();
|
||||||
|
|||||||
@@ -948,3 +948,34 @@ export class AbortError extends Error {
|
|||||||
return "AbortError";
|
return "AbortError";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retries a network operation run in a callback.
|
||||||
|
* @param {number} maxAttempts maximum attempts to try
|
||||||
|
* @param {Function} callback callback that returns a promise of the network operation. If rejected with ConnectionError, it will be retried by calling the callback again.
|
||||||
|
* @return {any} the result of the network operation
|
||||||
|
* @throws {ConnectionError} If after maxAttempts the callback still throws ConnectionError
|
||||||
|
*/
|
||||||
|
export async function retryNetworkOperation(maxAttempts, callback) {
|
||||||
|
let attempts = 0;
|
||||||
|
let lastConnectionError = null;
|
||||||
|
while (attempts < maxAttempts) {
|
||||||
|
try {
|
||||||
|
if (attempts > 0) {
|
||||||
|
const timeout = 1000 * Math.pow(2, attempts);
|
||||||
|
console.log(`network operation failed ${attempts} times,` +
|
||||||
|
` retrying in ${timeout}ms...`);
|
||||||
|
await new Promise(r => setTimeout(r, timeout));
|
||||||
|
}
|
||||||
|
return await callback();
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof ConnectionError) {
|
||||||
|
attempts += 1;
|
||||||
|
lastConnectionError = err;
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw lastConnectionError;
|
||||||
|
}
|
||||||
|
|||||||
23
src/utils.ts
23
src/utils.ts
@@ -749,26 +749,3 @@ export function setCrypto(c: Object) {
|
|||||||
export function getCrypto(): Object {
|
export function getCrypto(): Object {
|
||||||
return crypto;
|
return crypto;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function retryNetworkOperation(maxAttempts, callback) {
|
|
||||||
let attempts = 0;
|
|
||||||
let lastConnectionError = null;
|
|
||||||
while (attempts < maxAttempts) {
|
|
||||||
try {
|
|
||||||
if (attempts > 0) {
|
|
||||||
const timeout = 1000 * Math.pow(2, attempts);
|
|
||||||
console.log(`network operation failed ${attempts} times, retrying in ${timeout}ms...`);
|
|
||||||
await new Promise(r => setTimeout(r, timeout));
|
|
||||||
}
|
|
||||||
return await callback();
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof ConnectionError) {
|
|
||||||
attempts += 1;
|
|
||||||
lastConnectionError = err;
|
|
||||||
} else {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw lastConnectionError;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user