Files
feuerwerk/src/editor/autosave.ts
T
rootandClaude Opus 5 d54a620ebd Add ESLint and Prettier, and close the node test gaps
Tooling:
- Prettier as the single formatting authority (110 columns, single quotes,
  measured against the existing style); Markdown excluded because table-cell
  padding turns the README glossary into 1000-character lines.
- Type-aware ESLint ending in eslint-config-prettier, so a rule always means
  "this is a bug", never "this looks different".
- .editorconfig and an `npm run check` that runs typecheck, lint, format and
  tests in one go.

Findings fixed on the first run: three more duplicate imports, four
String(unknown) calls (parameter types narrowed instead of casts), two dead
initialisers, two redundant casts, a console.log for a real error. Two
warnings were false here and are disabled with a reason in place.

Tests 132 -> 269:
- node-catalog.test.ts covers every node with a run(), with a guard that fails
  when a new node has no row.
- behavior-events.test.ts covers the ten event nodes through a running World,
  checking timing rather than existence.
- Input's state machine (press/release/releaseAll) is reachable without DOM
  events now; the DOM handlers are thin adapters.

Compare fell back to == without a configured operator although the schema
default is <; aligned.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-08 10:02:22 +02:00

72 lines
2.1 KiB
TypeScript

import { type ProjectStore, StaleRevisionError } from '../core/store/ProjectStore';
import type { EditorState } from './state';
/**
* Debounced autosave: every model change schedules a save ~1 s later. Saves never run
* during Play (`state.project` is the runtime copy then; a pending save resumes on Stop)
* and never overlap; a change that arrives while a save is in flight triggers another save
* afterwards.
*/
export function attachAutosave(
state: EditorState,
store: ProjectStore,
delayMs = 1000,
): { flush: () => Promise<void> } {
let timer = 0;
let saving: Promise<void> | null = null;
let pending = false;
const save = async (): Promise<void> => {
if (saving) {
pending = true;
return saving;
}
if (state.saveStatus !== 'dirty' || !state.project.id || state.mode === 'play') return;
state.setSaveStatus('saving');
saving = (async () => {
try {
state.revision = await store.save(state.project, state.revision);
state.setSaveStatus(pending ? 'dirty' : 'saved');
} catch (error) {
if (error instanceof StaleRevisionError) {
state.setSaveStatus('conflict');
state.setNotice('This project was changed elsewhere. Reload to continue.');
} else {
state.setSaveStatus('error');
state.setNotice(`Saving failed: ${error instanceof Error ? error.message : String(error)}`);
}
} finally {
saving = null;
if (pending) {
pending = false;
if (state.saveStatus === 'dirty') schedule();
}
}
})();
return saving;
};
const schedule = (): void => {
window.clearTimeout(timer);
timer = window.setTimeout(() => void save(), delayMs);
};
state.on('save', () => {
if (state.saveStatus === 'dirty') schedule();
});
const flush = async (): Promise<void> => {
window.clearTimeout(timer);
await save();
};
window.addEventListener('beforeunload', (event) => {
if (state.saveStatus === 'dirty' || state.saveStatus === 'saving') {
void flush();
event.preventDefault();
}
});
return { flush };
}