use crate::action::{ActionId, Doc};
use crate::context::ActionContext;
use crate::entry::{Did, VerbCall};
pub const TYPING_PAUSE_MS: i64 = 30_000;
#[must_use]
pub fn within_pause(previous: i64, next: i64) -> bool {
previous <= 0 || next <= 0 || next.saturating_sub(previous) <= TYPING_PAUSE_MS
}
#[must_use]
pub fn by_a_person(context: &ActionContext) -> bool {
context.gesture.is_some()
&& context.compensation.is_none()
&& context
.invoker
.as_ref()
.is_none_or(|invoker| invoker.acting.is_person())
}
#[must_use]
pub fn setting(context: &ActionContext) -> Option<(&VerbCall, &str)> {
if !by_a_person(context) {
return None;
}
match (context.what.as_ref()?, context.item.as_ref()) {
(Did::Verb(call), Some(item)) => Some((call, item.id.as_str())),
_ => None,
}
}
pub const EDITED_FIELD: &str = "field";
fn names_value(call: &VerbCall) -> bool {
call.args.keys().any(|name| name != EDITED_FIELD)
}
#[must_use]
pub fn continues_setting(
previous: &ActionContext,
previous_ms: i64,
next: &ActionContext,
next_ms: i64,
) -> bool {
let (Some((before, item)), Some((after, on))) = (setting(previous), setting(next)) else {
return false;
};
names_value(before)
&& names_value(after)
&& before.action == after.action
&& item == on
&& before.args != after.args
&& within_pause(previous_ms, next_ms)
}
pub(crate) fn continued_gesture(
doc: &Doc,
actor: &[u8],
context: &ActionContext,
time_secs: i64,
) -> Option<ActionId> {
setting(context)?;
let heads = doc.committed_heads();
let [head] = heads.as_slice() else {
return None;
};
let document = doc.committed_document();
let meta = document.get_change_meta_by_hash(head)?;
if meta.actor.to_bytes() != actor {
return None;
}
let previous = crate::executor::entry_of(document, *head)?;
continues_setting(
&previous,
meta.timestamp.saturating_mul(1000),
context,
time_secs.saturating_mul(1000),
)
.then_some(previous.gesture)
.flatten()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::entry::{ActionItem, Scalar};
use crate::key::ActionKey;
fn rename(item: &str, title: &str) -> ActionContext {
ActionContext {
item: Some(ActionItem {
id: item.into(),
label: String::new(),
}),
what: Some(Did::verb(
ActionKey::new("notes", "notebook", "rename"),
[("title".to_owned(), Scalar::text(title))],
)),
gesture: Some(ActionId::new()),
..ActionContext::default()
}
}
#[test]
fn setting_one_value_again_runs_on_until_the_person_stops() {
let first = rename("note", "R");
assert!(continues_setting(
&first,
1_000,
&rename("note", "Ro"),
2_000
));
assert!(
!continues_setting(
&first,
1_000,
&rename("note", "Ro"),
2_000 + TYPING_PAUSE_MS
),
"a pause ends it"
);
assert!(
!continues_setting(&first, 1_000, &rename("other", "Ro"), 2_000),
"another item is another act"
);
assert!(
!continues_setting(&first, 1_000, &rename("note", "R"), 2_000),
"the same call again is a new act"
);
let mut agents = rename("note", "Ro");
agents.gesture = None;
assert!(
!continues_setting(&first, 1_000, &agents, 2_000),
"a step of its own runs on with nothing"
);
}
fn typed(field: &str) -> ActionContext {
ActionContext {
item: Some(ActionItem {
id: "note".into(),
label: String::new(),
}),
what: Some(Did::verb(
ActionKey::new("notes", "notebook", "edit-text"),
[(EDITED_FIELD.to_owned(), Scalar::text(field))],
)),
gesture: Some(ActionId::new()),
..ActionContext::default()
}
}
#[test]
fn typing_another_field_does_not_continue_the_run_of_the_one_before() {
let keystroke = typed("title");
let (call, _) = setting(&keystroke).expect("a keystroke is a person's setting");
assert!(!names_value(call), "a keystroke names no value");
assert!(
!continues_setting(&typed("title"), 1_000, &typed("body"), 2_000),
"another field is another burst"
);
}
}