Something went wrong. Try again.
data endpoint for entity 90008 (aka. a website)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839import { get, writable } from 'svelte/store';import { readFile, writeFile } from 'node:fs/promises';
/** * Creates a persistent counter that is stored in a file * @param fileName The name of the file to store the count in * @param initialValue The initial value if the file doesn't exist * @returns An object with methods to get, increment, and set the count */export const createFileCounter = async (filePath: string, initialValue: number = 0) => { let countRaw: string | null = null; try { countRaw = await readFile(filePath, 'utf8'); } catch { // we use initial value if not found }
const counter = writable(parseInt(countRaw ?? initialValue.toString()));
const saveToFile = async (value: number) => { await writeFile(filePath, value.toString(), 'utf8'); return value; };
return { get: () => get(counter), increment: (amount: number = 1) => { const currentValue = get(counter) + amount; counter.set(currentValue); return saveToFile(currentValue); }, set: (value: number) => { counter.set(value); return saveToFile(value); }, subscribe: counter.subscribe };};