mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-14 04:13:52 +02:00
feat: mutual friend graph (#1491)
This commit is contained in:
@@ -17,3 +17,5 @@ export * from './location';
|
||||
export * from './invite';
|
||||
export * from './world';
|
||||
export * from './memos';
|
||||
export * from './throttle';
|
||||
export * from './retry';
|
||||
|
||||
24
src/shared/utils/retry.js
Normal file
24
src/shared/utils/retry.js
Normal file
@@ -0,0 +1,24 @@
|
||||
export async function executeWithBackoff(fn, options = {}) {
|
||||
const {
|
||||
maxRetries = 5,
|
||||
baseDelay = 1000,
|
||||
shouldRetry = () => true
|
||||
} = options;
|
||||
|
||||
async function attempt(remaining) {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (err) {
|
||||
if (remaining <= 0 || !shouldRetry(err)) {
|
||||
throw err;
|
||||
}
|
||||
const delay =
|
||||
baseDelay *
|
||||
Math.pow(2, (options.maxRetries || maxRetries) - remaining);
|
||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||
return attempt(remaining - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return attempt(maxRetries);
|
||||
}
|
||||
28
src/shared/utils/throttle.js
Normal file
28
src/shared/utils/throttle.js
Normal file
@@ -0,0 +1,28 @@
|
||||
export function createRateLimiter({ limitPerInterval, intervalMs }) {
|
||||
const stamps = [];
|
||||
|
||||
async function throttle() {
|
||||
const now = Date.now();
|
||||
while (stamps.length && now - stamps[0] > intervalMs) {
|
||||
stamps.shift();
|
||||
}
|
||||
if (stamps.length >= limitPerInterval) {
|
||||
const wait = intervalMs - (now - stamps[0]);
|
||||
await new Promise((resolve) => setTimeout(resolve, wait));
|
||||
}
|
||||
stamps.push(Date.now());
|
||||
}
|
||||
|
||||
return {
|
||||
async schedule(fn) {
|
||||
await throttle();
|
||||
return fn();
|
||||
},
|
||||
async wait() {
|
||||
await throttle();
|
||||
},
|
||||
clear() {
|
||||
stamps.length = 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user