mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
Plug-ins: drop layer events of a rolled-back batch and serve waiting recipients first
A layer change seen when a command.batch step settled its interaction was queued before the batch finished, so a failed batch still reported it. The check now waits until the outermost command ends. Delivery under the frame budget now serves recipients in the order they were added to the event, so recipients re-added by a coalesced change no longer take the budget ahead of ones still waiting. The frame-budget tests use a session-local subscriber list instead of the shared plug-in registry.
This commit is contained in:
1 parent
c85d16f3c5
commit
ebd59b794d
2 files changed
+95
-15
No files matched your search
@@ -125,15 +125,22 @@ pub struct PluginEvents {
|
||||
queued: u64,
|
||||
dispatching: bool,
|
||||
frame_handlers: usize,
|
||||
interaction_ended: bool,
|
||||
failures: BTreeMap<String, Failures>,
|
||||
notices: VecDeque<Notice>,
|
||||
/// The installed plug-ins that react to events, as of a registry revision.
|
||||
subscribers: Option<(u64, Vec<Arc<Plugin>>)>,
|
||||
#[cfg(test)]
|
||||
pub(crate) test_subscribers: Option<Vec<Arc<Plugin>>>,
|
||||
}
|
||||
|
||||
impl PluginEvents {
|
||||
/// The installed plug-ins that react to events (none in Safe Mode).
|
||||
fn subscribers(&mut self) -> Vec<Arc<Plugin>> {
|
||||
#[cfg(test)]
|
||||
if let Some(subs) = &self.test_subscribers {
|
||||
return subs.clone();
|
||||
}
|
||||
if vectorcraft_plugins::safe_mode::active() {
|
||||
return Vec::new();
|
||||
}
|
||||
@@ -425,11 +432,9 @@ impl Session {
|
||||
if !p.event.outlives_document() && !self.docs.iter().any(|d| d.uid == p.document) {
|
||||
continue;
|
||||
}
|
||||
for plugin in subs.iter().filter(|s| s.manifest().reacts_to(p.event)) {
|
||||
let r = recipient(plugin);
|
||||
if !p.is_for(&r) {
|
||||
continue;
|
||||
}
|
||||
let recipients = p.to.clone();
|
||||
for (r, _) in recipients {
|
||||
let Some(plugin) = subs.iter().find(|s| recipient(s) == r && s.manifest().reacts_to(p.event)) else { continue };
|
||||
if self.plugin_events.failures_of(plugin).disabled {
|
||||
p.to.retain(|(to, _)| *to != r);
|
||||
continue;
|
||||
@@ -549,6 +554,9 @@ impl Session {
|
||||
/// them once a frame ([`Session::plugin_event_frames`]) or a script is running.
|
||||
pub(crate) fn plugin_events_after_command(&mut self) {
|
||||
self.plugin_events_see_new_documents();
|
||||
if std::mem::take(&mut self.plugin_events.interaction_ended) {
|
||||
self.plugin_events_interaction_ended();
|
||||
}
|
||||
if !self.plugin_event_frames && !self.script_in_run {
|
||||
self.deliver_plugin_events();
|
||||
}
|
||||
@@ -568,7 +576,11 @@ impl Session {
|
||||
/// installed during its previews. Tool actions may still be finishing; handlers wait for them.
|
||||
pub(crate) fn plugin_events_interaction_ended(&mut self) {
|
||||
if !self.plugin_events.dispatching {
|
||||
self.check_plugin_events(true);
|
||||
if self.depth == 0 {
|
||||
self.check_plugin_events(true);
|
||||
} else {
|
||||
self.plugin_events.interaction_ended = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,21 @@ fn install(s: &mut Session, id: &str, events: &str, more: &[&str], imports: &str
|
||||
run(s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&wasm), "grant": grant, "replace": true}));
|
||||
}
|
||||
|
||||
fn local_session() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.plugin_events.test_subscribers = Some(Vec::new());
|
||||
run(&mut s, "file.new", json!({"width": 400, "height": 200}));
|
||||
s
|
||||
}
|
||||
|
||||
fn subscribe(s: &mut Session, id: &str, events: &str, handler: &str) {
|
||||
let manifest = format!(r#"{{"id":"{id}","name":"Test {id}","kind":"filter","api":2,"permissions":["progress","events"],"events":{events}}}"#);
|
||||
let wasm = wat::parse_str(tpl::reacting(&manifest, tpl::HOST_IMPORTS, handler)).unwrap();
|
||||
let plugin = vectorcraft_plugins::Plugin::load(&wasm, vectorcraft_plugins::Limits::default()).unwrap();
|
||||
s.plugin_events.test_subscribers.as_mut().unwrap().push(std::sync::Arc::new(plugin));
|
||||
s.plugins_changed_for_events();
|
||||
}
|
||||
|
||||
/// The events plug-in `id`'s handler was given since the last call, as it logged them.
|
||||
fn given(s: &mut Session, id: &str) -> Vec<Value> {
|
||||
let notices = s.take_plugin_event_notices();
|
||||
@@ -197,8 +212,8 @@ fn background_installs_during_a_drag_record_the_settled_baseline() {
|
||||
fn silent_handlers_report_when_another_frame_is_needed() {
|
||||
use vectorcraft_plugins::events::Event;
|
||||
let id = "test.events.silent_pending";
|
||||
let mut s = session();
|
||||
install(&mut s, id, r#"["document.saved"]"#, &[], "", "(i32.const 0)", &[]);
|
||||
let mut s = local_session();
|
||||
subscribe(&mut s, id, r#"["document.saved"]"#, "(i32.const 0)");
|
||||
s.plugin_event_frames = true;
|
||||
let doc = s.doc().unwrap().uid;
|
||||
for _ in 0..=crate::plugin_events::MAX_FRAME_HANDLERS {
|
||||
@@ -210,15 +225,14 @@ fn silent_handlers_report_when_another_frame_is_needed() {
|
||||
s.begin_plugin_event_frame();
|
||||
assert!(!s.deliver_plugin_events(), "the next frame drains the remaining handler");
|
||||
assert!(s.take_plugin_event_notices().is_empty());
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deliveries_in_one_frame_share_the_handler_budget() {
|
||||
use vectorcraft_plugins::events::Event;
|
||||
let id = "test.events.shared_frame_budget";
|
||||
let mut s = session();
|
||||
install(&mut s, id, r#"["document.saved"]"#, &[], "", tpl::LOG_EVENT, &[]);
|
||||
let mut s = local_session();
|
||||
subscribe(&mut s, id, r#"["document.saved"]"#, tpl::LOG_EVENT);
|
||||
s.plugin_event_frames = true;
|
||||
let doc = s.doc().unwrap().uid;
|
||||
for _ in 0..(crate::plugin_events::MAX_FRAME_HANDLERS * 2) {
|
||||
@@ -232,17 +246,16 @@ fn deliveries_in_one_frame_share_the_handler_budget() {
|
||||
s.begin_plugin_event_frame();
|
||||
assert!(!s.deliver_plugin_events());
|
||||
assert_eq!(given(&mut s, id).len(), crate::plugin_events::MAX_FRAME_HANDLERS);
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frame_delivery_bounds_handlers_and_resumes_remaining_recipients() {
|
||||
use vectorcraft_plugins::events::Event;
|
||||
let mut s = session();
|
||||
let mut s = local_session();
|
||||
s.plugin_event_frames = true;
|
||||
let ids: Vec<String> = (0..=crate::plugin_events::MAX_FRAME_HANDLERS).map(|i| format!("test.events.frame_budget_{i}")).collect();
|
||||
for id in &ids {
|
||||
install(&mut s, id, r#"["document.saved"]"#, &[], "", tpl::LOG_EVENT, &[]);
|
||||
subscribe(&mut s, id, r#"["document.saved"]"#, tpl::LOG_EVENT);
|
||||
}
|
||||
let doc = s.doc().unwrap().uid;
|
||||
s.note_plugin_event(Event::DocumentSaved, doc);
|
||||
@@ -258,7 +271,62 @@ fn frame_delivery_bounds_handlers_and_resumes_remaining_recipients() {
|
||||
assert_eq!(delivered.len(), ids.len() * 2);
|
||||
for id in &ids {
|
||||
assert_eq!(delivered.iter().filter(|got| *got == id).count(), 2, "{id}");
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coalesced_changes_serve_waiting_recipients_before_readded_recipients() {
|
||||
let mut s = local_session();
|
||||
let r = rect(&mut s, 10.0);
|
||||
run(&mut s, "select.none", json!({}));
|
||||
let ids: Vec<String> = (0..9).map(|i| format!("test.events.fair_selection_{i}")).collect();
|
||||
for id in &ids {
|
||||
subscribe(&mut s, id, r#"["selection.changed"]"#, tpl::LOG_EVENT);
|
||||
}
|
||||
s.plugin_event_frames = true;
|
||||
let mut delivered = Vec::new();
|
||||
for frame in 0..3 {
|
||||
s.begin_plugin_event_frame();
|
||||
let selection = if frame % 2 == 0 { vec![r.0] } else { Vec::new() };
|
||||
run(&mut s, "select.set", json!({"ids": selection}));
|
||||
assert!(s.deliver_plugin_events());
|
||||
let notices = s.take_plugin_event_notices();
|
||||
assert_eq!(notices.len(), crate::plugin_events::MAX_FRAME_HANDLERS);
|
||||
delivered.extend(notices.into_iter().map(|n| n.plugin));
|
||||
}
|
||||
for id in &ids {
|
||||
assert!(delivered.contains(id), "{id} was not served while selection kept changing");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_batch_drops_layer_changes_from_a_settled_interaction() {
|
||||
let id = "test.events.rolled_back_layer";
|
||||
for frames in [false, true] {
|
||||
let mut s = local_session();
|
||||
subscribe(&mut s, id, r#"["layer.changed","document.opened"]"#, tpl::LOG_EVENT);
|
||||
s.plugin_event_frames = frames;
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let error = s
|
||||
.execute(
|
||||
"command.batch",
|
||||
&json!({"commands": [
|
||||
{"command": "layer.new", "params": {"name": "Notes"}},
|
||||
{"command": "edit.newDocumentFromState", "params": {}},
|
||||
{"command": "no.such.command", "params": {}}
|
||||
]}),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(error.to_string().contains("batch step 2"), "{error}");
|
||||
assert_eq!(s.documents().len(), 1);
|
||||
assert!(std::sync::Arc::ptr_eq(&before, &s.doc().unwrap().doc));
|
||||
s.deliver_plugin_events();
|
||||
assert!(given(&mut s, id).is_empty(), "the batch's layer was rolled back");
|
||||
run(&mut s, "layer.new", json!({"name": "Committed"}));
|
||||
s.deliver_plugin_events();
|
||||
let got = given(&mut s, id);
|
||||
assert_eq!(types(&got), ["layer.changed"]);
|
||||
assert!(got[0]["reasons"].as_array().unwrap().contains(&json!("added")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user