M1.3: engine workspace, file and buffer commands, event stream
State holds the open project (VFS project, watcher, watched directories)
and versioned buffers. New commands: project.open/close/children,
file.open/save/saveAll, buffer.get/edit/close. buffer.edit takes UTF-16
change sets based on a version and fails with the new stale_version code;
closing dirty buffers or projects without force fails with
unsaved_changes. Commands queue events (projectOpened, treeChanged,
bufferChanged, dirtyChanged, fileChangedOnDisk, ...) that the engine
returns in Outcome.events; Engine::pump_vfs maps changes on disk to
events and reloads clean buffers. project.children watches listed
directories, capped at 256 native / 32 polling, least recently listed
dropped first.
Control channel: events.subscribe streams {"event": ...} lines, with
bounded per-subscriber queues (lutra_control::Subscribers) and method docs
in methods.rs. testkit's EngineBackend and the desktop Host implement the
new Backend::subscribe. Verified with 14 engine integration tests, a TCP
events test and the running app over the control channel.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a63ab4ac45
commit
f1976c7d67
30 changed files with 2235 additions and 46 deletions
|
|
@ -44,7 +44,11 @@ impl Engine {
|
|||
self.registry.register(command)
|
||||
}
|
||||
|
||||
/// Runs a command inside the guard.
|
||||
/// Runs a command inside the guard. On success the outcome carries every
|
||||
/// event queued so far. When the command fails, events it queued before
|
||||
/// failing (real changes, such as files saved before another failed) stay
|
||||
/// queued and arrive with the next successful outcome or
|
||||
/// [`Engine::take_events`].
|
||||
///
|
||||
/// # Errors
|
||||
/// [`EngineError::UnknownCommand`] for an unknown id,
|
||||
|
|
@ -62,7 +66,31 @@ impl Engine {
|
|||
command.run(state, params)
|
||||
});
|
||||
state.commands_run = state.commands_run.saturating_add(1);
|
||||
result
|
||||
result.map(|mut outcome| {
|
||||
outcome.events = state.take_events();
|
||||
outcome
|
||||
})
|
||||
}
|
||||
|
||||
/// Applies pending changes on disk (reloads clean buffers, reports
|
||||
/// changed directories and conflicts) and returns every queued event.
|
||||
/// Hosts call it regularly, e.g. every 100 ms from a background thread.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`EngineError::Internal`] if handling the changes panicked; the queued
|
||||
/// events stay queued.
|
||||
pub fn pump_vfs(&mut self) -> Result<Vec<crate::Event>, EngineError> {
|
||||
let state = &mut self.state;
|
||||
guard::run(|| {
|
||||
crate::workspace::pump(state);
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(state.take_events())
|
||||
}
|
||||
|
||||
/// Takes the queued events, oldest first.
|
||||
pub fn take_events(&mut self) -> Vec<crate::Event> {
|
||||
self.state.take_events()
|
||||
}
|
||||
|
||||
/// Describes every command with its current enabled state. A command whose
|
||||
|
|
@ -191,11 +219,30 @@ mod tests {
|
|||
engine.register(Box::new(Disabled)).unwrap();
|
||||
let list = engine.list_commands();
|
||||
let ids: Vec<_> = list.iter().map(|c| c.id.as_str()).collect();
|
||||
assert_eq!(ids, vec!["app.about", "app.quit", "test.disabled"]);
|
||||
let quit = &list[1];
|
||||
assert_eq!(
|
||||
ids,
|
||||
vec![
|
||||
"app.about",
|
||||
"app.quit",
|
||||
"buffer.close",
|
||||
"buffer.edit",
|
||||
"buffer.get",
|
||||
"file.open",
|
||||
"file.save",
|
||||
"file.saveAll",
|
||||
"project.children",
|
||||
"project.close",
|
||||
"project.open",
|
||||
"test.disabled",
|
||||
]
|
||||
);
|
||||
let by_id = |id: &str| list.iter().find(|c| c.id == id).unwrap();
|
||||
let quit = by_id("app.quit");
|
||||
assert_eq!(quit.default_shortcut.as_deref(), Some("Mod+Q"));
|
||||
assert!(quit.enabled);
|
||||
assert!(!list[2].enabled);
|
||||
assert!(!by_id("test.disabled").enabled);
|
||||
assert!(by_id("project.open").enabled);
|
||||
assert!(!by_id("file.open").enabled, "needs a project");
|
||||
let json = serde_json::to_value(quit).unwrap();
|
||||
assert_eq!(json["menuPath"], "File/Quit");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue