Something went wrong. Try again.
a tiny pet with a GPT-2 brain, living in your browser
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import type { PetState } from "../types.ts";import { fresh, parseSave } from "./save-format.ts";import { store } from "./store.ts";import { errorMessage } from "./utils.ts";export { fresh, parseSave } from "./save-format.ts";
export function load(): PetState { let raw = null; try { raw = localStorage.getItem(KEY); } catch { return fresh(); } // storage unavailable if (raw == null) return fresh(); try { return parseSave(raw); } catch (e) { store.badEgg = { raw, why: e instanceof SyntaxError ? "it is damaged" : errorMessage(e), newer: e instanceof Error && "newer" in e && !!e.newer, }; return fresh(); }}let saveTimer = 0;export function save() { // Never write while a hard reset reloads the page, over a save this page // could not read, or from a tab that no longer holds the pet. if (store.resetting || store.badEgg || !store.tabOwned) return; clearTimeout(saveTimer); saveTimer = setTimeout(() => { if (store.resetting || store.badEgg || !store.tabOwned) return; try { localStorage.setItem(KEY, JSON.stringify(store.state)); } catch { /* quota or private mode */ } }, 200);}
export const KEY = "tokenling.v1";export const BAD_KEY = "tokenling.bad";export function cancelSave() { clearTimeout(saveTimer);}