Identify state by key only

This commit is contained in:
Laurenz 2023-07-19 13:11:54 +02:00
parent fa9e2c6237
commit fc90b72355
4 changed files with 24 additions and 10 deletions

View File

@ -385,7 +385,7 @@ impl Counter {
/// Produce content that performs a state update. /// Produce content that performs a state update.
pub fn update(self, update: CounterUpdate) -> Content { pub fn update(self, update: CounterUpdate) -> Content {
UpdateElem::new(self, update).pack() UpdateElem::new(self.0, update).pack()
} }
/// Produce the whole sequence of counter states. /// Produce the whole sequence of counter states.
@ -461,7 +461,7 @@ impl Counter {
/// The selector relevant for this counter's updates. /// The selector relevant for this counter's updates.
fn selector(&self) -> Selector { fn selector(&self) -> Selector {
let mut selector = let mut selector =
Selector::Elem(UpdateElem::func(), Some(dict! { "counter" => self.clone() })); Selector::Elem(UpdateElem::func(), Some(dict! { "key" => self.0.clone() }));
if let CounterKey::Selector(key) = &self.0 { if let CounterKey::Selector(key) = &self.0 {
selector = Selector::Or(eco_vec![selector, key.clone()]); selector = Selector::Or(eco_vec![selector, key.clone()]);
@ -502,8 +502,13 @@ pub enum CounterKey {
cast! { cast! {
CounterKey, CounterKey,
self => match self {
Self::Page => PageElem::func().into_value(),
Self::Selector(v) => v.into_value(),
Self::Str(v) => v.into_value(),
},
v: Str => Self::Str(v), v: Str => Self::Str(v),
label: Label => Self::Selector(Selector::Label(label)), v: Label => Self::Selector(Selector::Label(v)),
v: ElemFunc => { v: ElemFunc => {
if v == PageElem::func() { if v == PageElem::func() {
Self::Page Self::Page
@ -511,7 +516,7 @@ cast! {
Self::Selector(LocatableSelector::from_value(v.into_value())?.0) Self::Selector(LocatableSelector::from_value(v.into_value())?.0)
} }
}, },
selector: LocatableSelector => Self::Selector(selector.0), v: LocatableSelector => Self::Selector(v.0),
} }
impl Debug for CounterKey { impl Debug for CounterKey {
@ -666,9 +671,9 @@ impl Show for DisplayElem {
/// Category: special /// Category: special
#[element(Locatable, Show)] #[element(Locatable, Show)]
struct UpdateElem { struct UpdateElem {
/// The counter. /// The key that identifies the counter.
#[required] #[required]
counter: Counter, key: CounterKey,
/// The update to perform on the counter. /// The update to perform on the counter.
#[required] #[required]

View File

@ -297,7 +297,7 @@ impl State {
/// Produce content that performs a state update. /// Produce content that performs a state update.
pub fn update(self, update: StateUpdate) -> Content { pub fn update(self, update: StateUpdate) -> Content {
UpdateElem::new(self, update).pack() UpdateElem::new(self.key, update).pack()
} }
/// Produce the whole sequence of states. /// Produce the whole sequence of states.
@ -349,7 +349,7 @@ impl State {
/// The selector for this state's updates. /// The selector for this state's updates.
fn selector(&self) -> Selector { fn selector(&self) -> Selector {
Selector::Elem(UpdateElem::func(), Some(dict! { "state" => self.clone() })) Selector::Elem(UpdateElem::func(), Some(dict! { "key" => self.key.clone() }))
} }
} }
@ -423,9 +423,9 @@ impl Show for DisplayElem {
/// Category: special /// Category: special
#[element(Locatable, Show)] #[element(Locatable, Show)]
struct UpdateElem { struct UpdateElem {
/// The state. /// The key that identifies the state.
#[required] #[required]
state: State, key: Str,
/// The update to perform on the state. /// The update to perform on the state.
#[required] #[required]

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 52 KiB

View File

@ -15,6 +15,15 @@ Was: #locate(location => {
s.at(it.location()) s.at(it.location())
}). }).
---
// Try same key with different initial value.
#state("key", 2).display()
#state("key").update(x => x + 1)
#state("key", 2).display()
#state("key", 3).display()
#state("key").update(x => x + 1)
#state("key", 2).display()
--- ---
#set page(width: 200pt) #set page(width: 200pt)
#set text(8pt) #set text(8pt)