Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions Sources/Saga/Saga+Build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ extension Saga {
/// Signals saga-cli via SIGUSR1 if Swift source files change (so it can recompile and relaunch).
func watchAndRebuild() async throws {
let watchPaths = [inputPath, rootPath + "Sources"]
let coordinator = DevRebuildCoordinator(saga: self)
let monitor = FolderMonitor(paths: watchPaths, ignoredPatterns: ignoreChangesPatterns) { [weak self] changedPaths in
guard let self else { return }

Expand All @@ -115,17 +116,8 @@ extension Saga {
return
}

Task {
do {
try self.reset()
if let changedPath = changedPaths.first {
self.buildReason = .fileChange(changedPath)
}
try await self.build()
self.signalParent(SIGUSR2)
} catch {
self.fileIO.log("💥 Rebuild failed: \(error)")
}
Task { [coordinator] in
await coordinator.enqueue(changedPaths)
}
}

Expand All @@ -137,3 +129,39 @@ extension Saga {
}
}
}

private actor DevRebuildCoordinator {
private let saga: Saga
private var isBuilding = false
private var pendingPaths = Set<Path>()

init(saga: Saga) {
self.saga = saga
}

func enqueue(_ changedPaths: Set<Path>) async {
pendingPaths.formUnion(changedPaths)
guard !isBuilding else { return }

// Saga's mutable pipeline state is process-wide. Rebuild callbacks must
// run one at a time, otherwise file-save bursts race in reset/build.
isBuilding = true
defer { isBuilding = false }

while !pendingPaths.isEmpty {
let paths = pendingPaths
pendingPaths.removeAll()

do {
try saga.reset()
if let changedPath = paths.first {
saga.buildReason = .fileChange(changedPath)
}
try await saga.build()
saga.signalParent(SIGUSR2)
} catch {
saga.fileIO.log("💥 Rebuild failed: \(error)")
}
}
}
}
Loading