SOURCE / PINNED RELEASE
Made of little things.
The Bathyal Post
- Release
- fa6ce530b9f3…
- Author-recorded commit
- b255b3c9fd49…
- License
- LICENSE
- Author’s source reference
- nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-55c50b9a042
Archive hash verified: 03cf62191e7476c5…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
import { storage } from '@napplet/sdk';
import { isVoyage, newVoyage, type Voyage } from './voyage.js';
import { runtimeHasDomain } from './domain-availability.js';
export interface Ledger {
voyage: Voyage;
postmarks: string[];
record: number;
}
export function freshLedger(seed: number): Ledger {
return { voyage: newVoyage(seed), postmarks: [], record: 0 };
}
export function newSeed() {
return Math.floor(Math.random() * 0x100000000);
}
const storageKey = 'bathyal-post.office.v1';
type SavedLedger = Omit<Ledger, 'voyage'> & {voyage: Voyage | null};
function readLedger(raw: string): SavedLedger {
const value: unknown = JSON.parse(raw);
if (!value || typeof value !== 'object') throw new Error('Unrecognized ledger');
const voyage = 'voyage' in value && isVoyage(value.voyage) ? value.voyage : null;
const marks = 'postmarks' in value && Array.isArray(value.postmarks) ? value.postmarks : [];
const postmarks = marks.filter((mark): mark is string => typeof mark === 'string' && /^(black|(?:medusa|whale|garden|trench)-[0-4])$/.test(mark));
const record = 'record' in value && typeof value.record === 'number' && Number.isFinite(value.record) ? Math.max(0, Math.floor(value.record)) : 0;
return {voyage, postmarks: [...new Set(postmarks)], record};
}
export class PostOffice {
status = runtimeHasDomain('storage') ? 'Opening the ledger…' : 'Session only · storage unavailable';
readonly available = runtimeHasDomain('storage');
private generation = 0;
private reading = false;
private writing = false;
private loaded = false;
private pending: (() => Ledger) | null = null;
private restore?: (entry: SavedLedger) => boolean;
private changed?: () => void;
retire() {
this.generation++;
this.pending = null;
this.restore = undefined;
this.changed = undefined;
this.reading = this.writing = this.loaded = false;
if (this.available) this.status = 'Opening the ledger…';
}
load(restore: (entry: SavedLedger) => boolean, changed: () => void) {
this.restore = restore;
this.changed = changed;
return this.read();
}
private async read() {
if (!this.available || this.reading || this.loaded || !this.restore) return;
const generation = this.generation;
this.reading = true;
let raw: string | null;
try {
raw = await storage.getItem(storageKey);
} catch {
if (generation === this.generation) {
this.reading = false;
this.status = 'Save unavailable · previous ledger could not be read · playing in this session';
this.changed?.();
}
return;
}
if (generation !== this.generation) return;
this.reading = false;
let saved: SavedLedger | undefined;
if (raw) {
try { saved = readLedger(raw); }
catch { /* An unrecognizable local file can be replaced by the next checkpoint. */ }
}
this.status = saved
? this.restore(saved) ? 'Saved voyage restored' : 'Album and best return restored'
: raw ? 'Previous checkpoint unreadable · autosave on' : 'Ledger ready · autosave on';
this.loaded = true;
this.changed?.();
await this.flush();
}
save(current: () => Ledger) {
if (!this.available) return;
// Read the live entry only after the old album and all earlier writes settle.
this.pending = current;
if (this.loaded) void this.flush();
else void this.read();
}
private async flush() {
if (this.writing || !this.loaded || !this.pending) return;
const generation = this.generation;
this.writing = true;
while (this.pending) {
const raw = JSON.stringify(this.pending());
this.pending = null;
try {
await storage.setItem(storageKey, raw);
if (generation !== this.generation) return;
this.status = 'Voyage saved to shell storage';
} catch {
if (generation !== this.generation) return;
this.status = 'Save unavailable · progress is in this session only';
}
this.changed?.();
}
this.writing = false;
}
}
