Skip to content

Commit b974576

Browse files
committed
feat: added .make_unrx()
1 parent c238df9 commit b974576

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

packages/perseus-macro/src/rx_state.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,33 @@ pub fn make_rx_impl(mut orig_struct: ItemStruct, name: Ident) -> TokenStream {
150150
// We filtered out the other types before
151151
_ => unreachable!(),
152152
};
153+
let make_unrx_fields = match orig_struct.fields {
154+
syn::Fields::Named(ref mut fields) => {
155+
let mut field_assignments = quote!();
156+
for field in fields.named.iter_mut() {
157+
// We know it has an identifier because it's a named field
158+
let field_name = field.ident.as_ref().unwrap();
159+
// Check if this field was registered as one to use nested reactivity
160+
if nested_fields_map.contains_key(field.ident.as_ref().unwrap()) {
161+
field_assignments.extend(quote! {
162+
#field_name: self.#field_name.make_unrx(),
163+
})
164+
} else {
165+
// We can `.clone()` the field because we implement `Clone` on both the new and the original `struct`s, meaning all fields must also be `Clone`
166+
field_assignments.extend(quote! {
167+
#field_name: (*self.#field_name.get_untracked()).clone(),
168+
});
169+
}
170+
}
171+
quote! {
172+
#ident {
173+
#field_assignments
174+
}
175+
}
176+
}
177+
// We filtered out the other types before
178+
_ => unreachable!(),
179+
};
153180

154181
quote! {
155182
// We add a Serde derivation because it will always be necessary for Perseus on the original `struct`, and it's really difficult and brittle to filter it out
@@ -166,8 +193,7 @@ pub fn make_rx_impl(mut orig_struct: ItemStruct, name: Ident) -> TokenStream {
166193
impl#generics ::perseus::state::MakeUnrx for #name#generics {
167194
type Unrx = #ident#generics;
168195
fn make_unrx(self) -> #ident#generics {
169-
todo!()
170-
// #make_rx_fields
196+
#make_unrx_fields
171197
}
172198
}
173199
}

0 commit comments

Comments
 (0)