Delay hero status fetch if already cached

The context call is more important
This commit is contained in:
Lim Chee Aun 2022-12-28 18:06:05 +08:00
parent 02c464a139
commit 7e3733d79e

View file

@ -51,6 +51,7 @@ function StatusPage({ id }) {
useEffect(() => { useEffect(() => {
setUIState('loading'); setUIState('loading');
let heroTimer;
const cachedStatuses = store.session.getJSON('statuses-' + id); const cachedStatuses = store.session.getJSON('statuses-' + id);
if (cachedStatuses) { if (cachedStatuses) {
@ -73,18 +74,28 @@ function StatusPage({ id }) {
} }
(async () => { (async () => {
const heroFetch = masto.v1.statuses.fetch(id); const heroFetch = () => masto.v1.statuses.fetch(id);
const contextFetch = masto.v1.statuses.fetchContext(id); const contextFetch = masto.v1.statuses.fetchContext(id);
const hasStatus = snapStates.statuses.has(id); const hasStatus = snapStates.statuses.has(id);
let heroStatus = snapStates.statuses.get(id); let heroStatus = snapStates.statuses.get(id);
if (hasStatus) {
console.log('Hero status is cached');
heroTimer = setTimeout(async () => {
try { try {
heroStatus = await heroFetch; heroStatus = await heroFetch();
states.statuses.set(id, heroStatus); states.statuses.set(id, heroStatus);
} catch (e) { } catch (e) {
// Silent fail if status is cached // Silent fail if status is cached
console.error(e); console.error(e);
if (!hasStatus) { }
}, 1000);
} else {
try {
heroStatus = await heroFetch();
states.statuses.set(id, heroStatus);
} catch (e) {
console.error(e);
setUIState('error'); setUIState('error');
alert('Error fetching status'); alert('Error fetching status');
return; return;
@ -153,6 +164,10 @@ function StatusPage({ id }) {
setUIState('error'); setUIState('error');
} }
})(); })();
return () => {
clearTimeout(heroTimer);
};
}, [id, snapStates.reloadStatusPage]); }, [id, snapStates.reloadStatusPage]);
const firstLoad = useRef(true); const firstLoad = useRef(true);