Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(engine): a background job's step goes only to the recording it was started in (#7289)

Korg1ch07b96005615d
diff --git a/crates/engine/src/actions_cmds.rs b/crates/engine/src/actions_cmds.rs
index d881a6112..b8ecb248d 100644
--- a/crates/engine/src/actions_cmds.rs
+++ b/crates/engine/src/actions_cmds.rs
@@ -98,6 +98,10 @@ pub struct ActionState {
/// Bumped whenever `list` changes, so the preset store can skip unchanged writes.
#[serde(skip)]
pub rev: u64,
+ /// Bumped by each `actions.record`, so a background job knows whether the recording it was
+ /// started in is still the one running when its result lands.
+ #[serde(skip)]
+ pub take: u64,
}
impl ActionState {
@@ -272,6 +276,7 @@ fn record(s: &mut Session, p: &Value) -> Result<Value> {
s.actions.list.len() - 1
};
s.actions.recording = Some((s.journal.len(), idx));
+ s.actions.take = s.actions.take.wrapping_add(1);
let name = s.actions.list.get(idx).map(|a| a.name.clone()).unwrap_or_default();
Ok(json!({"action": name, "index": idx}))
}
diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs
index 0ee057317..7aa3fffd4 100644
--- a/crates/engine/src/jobs.rs
+++ b/crates/engine/src/jobs.rs
@@ -212,6 +212,10 @@ struct Running {
label: String,
params: Value,
journal: bool,
+ /// The recording (`ActionState::take`) running when the job started, if any (set by
+ /// `dispatch`, once recording resumes). Only that recording gets the job's step: one started
+ /// later must not pick up an edit made before it.
+ recorded_by: Option<u64>,
doc: Option<DocId>,
/// The document as the job saw it: applying fails if it was edited meanwhile.
snapshot: Option<Weak<Document>>,
@@ -400,6 +404,7 @@ pub fn run<T: Send + 'static>(
label: label.to_string(),
params: Value::Null,
journal: true,
+ recorded_by: None,
doc,
snapshot,
ctx,
@@ -780,6 +785,7 @@ impl Session {
job.command = id.to_string();
job.params = params;
job.journal = spec.journal;
+ job.recorded_by = self.actions.recording.map(|_| self.actions.take);
let jid = job.id;
self.jobs.running.push(job);
Ok(Started::Job(jid))
@@ -868,7 +874,13 @@ impl Session {
}
fn apply_job(&mut self, job: &Running, apply: Option<Apply>, payload: Payload) -> Result<Value> {
- let Running { label, doc, snapshot, coalesce, color_restrict, layers, command, params, journal, .. } = job;
+ let Running { label, doc, snapshot, coalesce, color_restrict, layers, command, params, journal, recorded_by, .. } = job;
+ // A recording started after the job (#7289) keeps what it has so far, and the job's step
+ // goes into the journal past its start, so that recording doesn't get it.
+ let foreign = self.actions.recording.is_some() && *recorded_by != Some(self.actions.take);
+ if foreign {
+ crate::actions_cmds::flush_recording(self)?;
+ }
let prev_active = self.active;
let target = match doc {
Some(d) => {
@@ -917,6 +929,9 @@ impl Session {
// to that document instead, when the user switched tabs mid-job.
if r.is_ok() {
self.after_command(command, params.clone(), *journal, Some(nested_from));
+ if foreign && let Some((start, _)) = self.actions.recording.as_mut() {
+ *start = self.journal.len();
+ }
}
// Give the user back the layers they selected while the job ran, unless the job chose a
// selection itself (a new layer, say) or one of their layers is gone.