Skip to content

Commit

Permalink
Clean up and add Composer::replaceable_group
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jul 3, 2023
1 parent ea50a30 commit 14023a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub enum Slot {
id: TypeId,
f: Option<Box<dyn FnMut(&mut Composer)>>,
},
ReplaceableGroup {
id: TypeId,
},
}

pub struct Composer {
Expand All @@ -32,7 +35,6 @@ impl Composer {

pub fn compose(&mut self, content: impl FnOnce(&mut Self)) {
content(self);

}

pub async fn recompose(&mut self) {
Expand All @@ -41,6 +43,7 @@ impl Composer {
let idx = *self.map.get(&id).unwrap();
let mut f = match &mut self.slots[idx] {
Slot::RestartGroup { id: _, f } => f.take().unwrap(),
_ => todo!(),
};
self.pos = idx;

Expand All @@ -52,7 +55,7 @@ impl Composer {
self.tracked_states = HashSet::new();
}

pub fn group(&mut self, id: TypeId, mut f: impl FnMut(&mut Self) + 'static) {
pub fn restart_group(&mut self, id: TypeId, mut f: impl FnMut(&mut Self) + 'static) {
let tracked = self.tracked_states.clone();

let scope = Scope::default().enter(|| f(self));
Expand All @@ -72,20 +75,24 @@ impl Composer {

self.tracked_states = tracked;
}

pub fn replaceable_group(&mut self, id: TypeId, mut f: impl FnMut(&mut Self)) {
self.slots.push(Slot::ReplaceableGroup { id });
f(self);
}
}

#[cfg(test)]
mod tests {
use std::any::Any;

use crate::{Composer, State};
use std::any::Any;

#[tokio::test]
async fn it_works() {
let mut composer = Composer::new();

composer.compose(|composer| {
composer.group(().type_id(), |composer| {
composer.restart_group(().type_id(), |composer| {
let state = State::new(0);
dbg!(*state.get());

Expand Down
4 changes: 3 additions & 1 deletion src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ mod tests {
state.update(|x| *x = 1);
assert_eq!(*state.get(), 0);

snapshot.apply_pending();
for id in snapshot.apply_pending() {

}
assert_eq!(*state.get(), 1);
});
}
Expand Down

0 comments on commit 14023a8

Please sign in to comment.