Files
museion/Museion/PostsModelRail.swift
T
AlexKucera 06283b6287
CI / ci (push) Canceled after 0s
refactor(posts): route every library write through the write rail (#179)
One internal seam (PostsModelRail.swift) now pairs every library
verb store write with exactly one library rescan and owns the error
strip: saveDraft, newPostFromSession, rename, trash, createProject,
moveToProject, archivePost, restorePost, unarchiveProject,
importFiles, convertToPost, and runPass pass-state advance (12 sites
on this base; the pass snapshot/stats writes, the publish stamp, and
the editor flush stay outside by design).

The model owns the Home: boundHome is set by bind() alone (app
construction + Settings repoint); every public verb lost its home:
parameter (create post/outline/session-born, rename, move, archive,
restore, trash, convert, project creation, import, select, cards,
passes, blind read, publish).

The three rail entries are railWrite/railWriteReturningReason/
railWriteKeepingStrip — deliberately NOT overloads: a shared base
name let unlabeled trailing-closure calls resolve into the labeled
non-generic entry (Swift binds trailing closures to labeled first
parameters), silently bypassing the strip and the pairing.

Views + 11 test files swept to the homeless signatures (160 sites).
RED suite green: 3/3. Full unit suite: 654/654. Fixes #179
2026-11-10 03:05:48 +01:00

86 lines
3.0 KiB
Swift

//
// PostsModelRail.swift
// Museion
//
// #179: the library write rail — ONE internal seam every library verb's
// store write rides. The rail pairs the write with exactly ONE library
// rescan and routes the failure; a verb that bypasses it is a silent
// stale-library bug (the hand-copied write-then-rescan choreography this
// file replaces). The editor's flush keeps its own posture outside the
// rail (different error semantics, editor-owned — EditorSession), as do
// the pass substrate's snapshot/stats writes (they never regroup the
// library) and the publish stamp (wire-owned, deliberately quiet).
//
import Foundation
/// #179: rail observability — what the write-rail suite reads off the
/// seam. `.write` = a verb's body rode the rail; `.pairedRescan` = the
/// rail scheduled the verb's ONE rescan; `.writeFailed` = the verb's
/// write threw (the strip owns the reason; nothing paired).
enum PostsModelRailEvent: Equatable {
case write
case pairedRescan
case writeFailed
}
// MARK: Write rail (#179)
/// #179: ONE rail, three error routings — deliberately NOT overloads: a
/// shared base name let unlabeled trailing-closure calls resolve into the
/// wrong entry (Swift binds a trailing closure to a labeled first
/// parameter, and the non-generic overload wins) — a verb meant for the
/// strip silently took the reason-returning path and never touched
/// `lastError`. Distinct names make the routing choice unambiguous at
/// every call site.
extension PostsModel {
/// #179: the rail's default entry — strip-routed. The op runs the
/// verb's store write FIRST, then its state updates (a throw leaves
/// nothing half-updated); success clears the strip and pairs exactly
/// one rescan, a throw routes the failure into the strip.
@discardableResult
func railWrite<T>(_ operation: () throws -> T) -> T? {
onRailEvent?(.write)
do {
let result = try operation()
lastError = nil
pairRailRescan()
return result
} catch {
onRailEvent?(.writeFailed)
lastError = error.localizedDescription
return nil
}
}
/// #179: the reason-returning entry (project creation) — the sheet
/// owns its inline reason and the strip behind the sheet must not
/// double-message one error. Same ONE rescan on success.
func railWriteReturningReason(_ operation: () throws -> Void) -> String? {
onRailEvent?(.write)
do {
try operation()
pairRailRescan()
return nil
} catch {
return error.localizedDescription
}
}
/// #179: the strip-keeping entry (the import batch) — per-file
/// failures continue inside the op and the strip keeps the verb's own
/// message; the rail only pairs the ONE rescan.
func railWriteKeepingStrip(_ operation: () -> Void) {
onRailEvent?(.write)
operation()
pairRailRescan()
}
/// #179: the ONE pairing point — a write through any verb is followed
/// by exactly one rescan, pinned here and nowhere else.
private func pairRailRescan() {
onRailEvent?(.pairedRescan)
Task { await rescan() }
}
}