Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ekr-cfa committed Oct 16, 2024
1 parent 42f62ba commit fce1920
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/people.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn add_derived_property<T: 'static>(register: impl Fn(&mut Context) + 'stati
}
}

pub fn get_dependencies<T: 'static>() -> Vec<Rc<RegisterFn>> {
fn get_derived_properties() -> Vec<Rc<RegisterFn>> {
unsafe {
let properties = DERIVED_PROPERTIES.lock().unwrap();
properties.clone()
Expand Down Expand Up @@ -150,6 +150,7 @@ macro_rules! define_derived_person_property {
#[ctor::ctor]
fn [<$derived_property:snake _add_dep>]() {
$crate::people::add_derived_property::<$derived_property>(|context| {
println!("Registering derived property {:?}", std::any::type_name::<$derived_property>());
context.register_derived_property($derived_property);
});
}
Expand Down Expand Up @@ -262,6 +263,8 @@ trait PrivateContextPeopleExt {
property: T,
value: T::Value,
);

fn ensure_data_container_mut(&mut self) -> &mut PeopleData;
}

impl PrivateContextPeopleExt for Context {
Expand Down Expand Up @@ -312,6 +315,19 @@ impl PrivateContextPeopleExt for Context {
}
}
}

fn ensure_data_container_mut(&mut self) -> &mut PeopleData {
let data_container = self.get_data_container(PeoplePlugin);
let init = data_container.is_none();
let _ = self.get_data_container_mut(PeoplePlugin);
if init {
for property in get_derived_properties() {
println!("Adding derived property");
property(self);
}
}
self.get_data_container_mut(PeoplePlugin)
}
}

pub trait ContextPeopleExt {
Expand Down Expand Up @@ -356,7 +372,7 @@ impl ContextPeopleExt for Context {
}

fn add_person(&mut self) -> PersonId {
let person_id = self.get_data_container_mut(PeoplePlugin).add_person();
let person_id = self.ensure_data_container_mut().add_person();
self.emit_event(PersonCreatedEvent { person_id });
person_id
}
Expand Down Expand Up @@ -418,9 +434,6 @@ impl ContextPeopleExt for Context {
property: T,
handler: impl Fn(&mut Context, PersonPropertyChangeData<T>) + 'static,
) {
if T::is_derived() {
self.register_derived_property(property);
}
self.subscribe_to_event(
move |context, event: PersonPropertyChangeEventInternal<T>| {
handler(
Expand Down

0 comments on commit fce1920

Please sign in to comment.