Skip to content

Complex Logic Flows

For complex features, responsibilities must be explicitly split side-by-side between the frontend and backend.

Undo / Redo (The Split Model)

Because C# holds the active text buffer and Nim manages the persistent file system, Undo/Redo is strictly split into two layers:

  • Ephemeral Undo (C#): Keystroke-level corrections (deleting a word, fixing a typo, paragraph edits).
    • C# handles this entirely in its local memory buffer (undoStack and redoStack) with zero IPC traffic.
    • Standard Execution: Navigates backward or forward through the Batches of the currently active Scope.
    • Speed Bump: Enforced within C# when reaching a boundary, requiring a deliberate secondary press.
    • Scope Jumps: Pointer arithmetic in C#'s memory that skips entire nodes without touching the disk or sending payloads.
  • Structural Undo (Nim): Book-level actions (e.g., moving a chapter to Outtakes, splitting a chapter).
    • C# sends the structural command to Nim.
    • Nim commands libgit2 to restore the previous file state directly on disk. Because it relies on Git snapshots, Nim doesn't need to hold text in RAM.
    • Nim returns the restored JSON layout to C#.

The Shutdown Sequence

The Nim engine acts as the Shutdown Mastermind.

  • The entire closing sequence must route through Nim.
  • The C# UI simply notifies Nim of the close intent.
  • This allows Nim to orchestrate flushing saves, pausing Git, triggering exit reviews (like the Utterly Unrelated review), and cleanly terminating the process without risking data loss or dangling file handlers.