Files
electric-cherry/src/lib/leaderboard-updates.ts
T

28 lines
1015 B
TypeScript

import type { LeaderboardResponse } from './leaderboard.js';
export const leaderboardPageSize = 25;
/** Keep a viewed ranking stable until the reader chooses to apply new results. */
export function createLeaderboardUpdates(scope: string, current: LeaderboardResponse) {
let pending: LeaderboardResponse | null = null;
return {
reset(nextScope: string, report: LeaderboardResponse) {
scope = nextScope;
current = report;
pending = null;
},
receive(requestScope: string, report: LeaderboardResponse, inspecting: boolean) {
if (requestScope !== scope) return null;
if (JSON.stringify(current) === JSON.stringify(report)) pending = null;
else if (inspecting || pending) pending = report;
else current = report;
return { current, pending };
},
apply() {
if (pending) current = pending;
pending = null;
return current;
}
};
}