diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2026-11-19 08:53:51 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2026-11-19 08:53:51 +0100 |
| commit | b884441ba0d10bdd3559748507a0991888f66194 (patch) | |
| tree | 074321d353c1153e8d95191eecf5a61b2eec1096 /server/lib/core/settings_store.dart | |
| parent | d64cf3514e5ff1ddb1c7f3ddac9a769db3e7dde0 (diff) | |
Phase 6: settings, saves, localization parity (M6)
Diffstat (limited to 'server/lib/core/settings_store.dart')
| -rw-r--r-- | server/lib/core/settings_store.dart | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/server/lib/core/settings_store.dart b/server/lib/core/settings_store.dart new file mode 100644 index 0000000..520ed97 --- /dev/null +++ b/server/lib/core/settings_store.dart @@ -0,0 +1,56 @@ +import 'dart:io'; + +import 'package:path/path.dart' as p; + +/// Persistent store for the server settings JSON (Phase 6: the settings +/// split). Same contract as [SaveStore]: the server changes a setting, and +/// "written through on every change" must be on disk when [write] returns. +abstract class SettingsStore { + /// Returns the stored settings JSON, or null if nothing has been saved yet. + String? read(); + + /// Writes the settings JSON. + void write(String data); + + /// Removes the stored settings. + void clear(); +} + +/// In-memory [SettingsStore] — used by tests. +class InMemorySettingsStore implements SettingsStore { + String? _data; + + @override + String? read() => _data; + + @override + void write(String data) => _data = data; + + @override + void clear() => _data = null; +} + +/// File-backed [SettingsStore] writing one JSON document into [directory]. +class FileSettingsStore implements SettingsStore { + final File _file; + + FileSettingsStore(String directory) + : _file = File(p.join(directory, 'settings.json')); + + @override + String? read() => _file.existsSync() ? _file.readAsStringSync() : null; + + @override + void write(String data) { + final dir = _file.parent; + if (!dir.existsSync()) dir.createSync(recursive: true); + // Synchronous write: the same kill −9 race the save store had — the + // settings must be on disk when [write] returns. + _file.writeAsStringSync(data, flush: true); + } + + @override + void clear() { + if (_file.existsSync()) _file.deleteSync(); + } +} |
