You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
import { writable } from "svelte/store"; import stuffLogClient from "../clients/stufflog"; import type { GoalFilter, GoalResult } from "../models/goal";
interface GoalStoreData { loading: boolean stale: boolean goals: GoalResult[] filter: GoalFilter }
function createGoalStore() { const {update, subscribe} = writable<GoalStoreData>({ loading: false, stale: true, goals: [], filter: {}, });
let lastLoad = 0; let spamPoints = 3;
return { subscribe,
markStale() { update(v => ({...v, stale: true})); },
async load(filter: GoalFilter) { update(v => ({...v, loading: true, filter}));
// Prevent too much spammery when tweaking the date.
const timeSince = Date.now() - lastLoad; if (timeSince < 3000) { if (timeSince < 1000) { spamPoints -= 1; }
if (spamPoints <= 0) { await new Promise(resolve => setTimeout(resolve, 3000 - timeSince)); spamPoints = 3; } } else { spamPoints = 3; } lastLoad = Date.now();
const goals = await stuffLogClient.listGoals(filter);
const expireds = []; const upcomings = []; const actives = []; const now = Date.now(); for (const goal of goals) { if (Date.parse(goal.endTime) < now) { expireds.push(goal); } else if (Date.parse(goal.startTime) > now) { upcomings.push(goal); } else { actives.push(goal); } }
update(v => ({...v, loading: false, stale: false, goals: [...upcomings, ...actives, ...expireds] })); }, } }
const goalStore = createGoalStore();
export default goalStore; export const fpGoalStore = createGoalStore();
|