mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-02-25 09:18:51 +01:00
Further rate limit this threadify calls
Every post calls threadify and clogs the RAF
This commit is contained in:
parent
5f48f92c11
commit
49fd8a5dc9
2 changed files with 30 additions and 2 deletions
26
src/utils/ratelimit.js
Normal file
26
src/utils/ratelimit.js
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
// Rate limit repeated function calls and queue them to set interval
|
||||||
|
export default function rateLimit(fn, interval) {
|
||||||
|
let queue = [];
|
||||||
|
let isRunning = false;
|
||||||
|
|
||||||
|
function executeNext() {
|
||||||
|
if (queue.length === 0) {
|
||||||
|
isRunning = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextFn = queue.shift();
|
||||||
|
nextFn();
|
||||||
|
setTimeout(executeNext, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
return function (...args) {
|
||||||
|
const callFn = () => fn.apply(this, args);
|
||||||
|
queue.push(callFn);
|
||||||
|
|
||||||
|
if (!isRunning) {
|
||||||
|
isRunning = true;
|
||||||
|
setTimeout(executeNext, interval);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ import { subscribeKey } from 'valtio/utils';
|
||||||
|
|
||||||
import { api } from './api';
|
import { api } from './api';
|
||||||
import pmem from './pmem';
|
import pmem from './pmem';
|
||||||
|
import rateLimit from './ratelimit';
|
||||||
import store from './store';
|
import store from './store';
|
||||||
|
|
||||||
const states = proxy({
|
const states = proxy({
|
||||||
|
@ -187,7 +188,7 @@ export function saveStatus(status, instance, opts) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function threadifyStatus(status, propInstance) {
|
function _threadifyStatus(status, propInstance) {
|
||||||
const { masto, instance } = api({ instance: propInstance });
|
const { masto, instance } = api({ instance: propInstance });
|
||||||
// Return all statuses in the thread, via inReplyToId, if inReplyToAccountId === account.id
|
// Return all statuses in the thread, via inReplyToId, if inReplyToAccountId === account.id
|
||||||
let fetchIndex = 0;
|
let fetchIndex = 0;
|
||||||
|
@ -204,7 +205,7 @@ export function threadifyStatus(status, propInstance) {
|
||||||
let prevStatus = states.statuses[key];
|
let prevStatus = states.statuses[key];
|
||||||
if (!prevStatus) {
|
if (!prevStatus) {
|
||||||
if (fetchIndex++ > 3) throw 'Too many fetches for thread'; // Some people revive old threads
|
if (fetchIndex++ > 3) throw 'Too many fetches for thread'; // Some people revive old threads
|
||||||
await new Promise((r) => setTimeout(r, 500 * fetchIndex)); // Be nice to rate limits
|
await new Promise((r) => setTimeout(r, 300 * fetchIndex)); // Be nice to rate limits
|
||||||
// prevStatus = await masto.v1.statuses.$.select(inReplyToId).fetch();
|
// prevStatus = await masto.v1.statuses.$.select(inReplyToId).fetch();
|
||||||
prevStatus = await fetchStatus(inReplyToId, masto);
|
prevStatus = await fetchStatus(inReplyToId, masto);
|
||||||
saveStatus(prevStatus, instance, { skipThreading: true });
|
saveStatus(prevStatus, instance, { skipThreading: true });
|
||||||
|
@ -226,6 +227,7 @@ export function threadifyStatus(status, propInstance) {
|
||||||
console.error(e, status);
|
console.error(e, status);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
export const threadifyStatus = rateLimit(_threadifyStatus, 300);
|
||||||
|
|
||||||
const fetchStatus = pmem((statusID, masto) => {
|
const fetchStatus = pmem((statusID, masto) => {
|
||||||
return masto.v1.statuses.$select(statusID).fetch();
|
return masto.v1.statuses.$select(statusID).fetch();
|
||||||
|
|
Loading…
Reference in a new issue