Skip to content

Commit

Permalink
Merge branch 'main' into scoped-futures
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj authored Sep 21, 2023
2 parents d1cc911 + 2c8f464 commit 31cd000
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
34 changes: 34 additions & 0 deletions leptos_reactive/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,40 @@ pub fn try_with_owner<T>(owner: Owner, f: impl FnOnce() -> T) -> Option<T> {
.flatten()
}

/// Runs the given function as a child of the current Owner, once.
pub fn run_as_child<T>(f: impl FnOnce() -> T + 'static) -> T {
let owner = with_runtime(|runtime| runtime.owner.get())
.expect("runtime should be alive when created");
let (value, disposer) = with_runtime(|runtime| {
let prev_observer = runtime.observer.take();
let prev_owner = runtime.owner.take();

runtime.owner.set(owner);
runtime.observer.set(owner);

let id = runtime.nodes.borrow_mut().insert(ReactiveNode {
value: None,
state: ReactiveNodeState::Clean,
node_type: ReactiveNodeType::Trigger,
});
runtime.push_scope_property(ScopeProperty::Trigger(id));
let disposer = Disposer(id);

runtime.owner.set(Some(id));
runtime.observer.set(Some(id));

let v = f();

runtime.observer.set(prev_observer);
runtime.owner.set(prev_owner);

(v, disposer)
})
.expect("runtime should be alive when run");
on_cleanup(move || drop(disposer));
value
}

impl RuntimeId {
/// Removes the runtime, disposing of everything created in it.
///
Expand Down
33 changes: 29 additions & 4 deletions server_fn_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ pub fn server_macro_impl(
let fn_path = fn_path.unwrap_or_else(|| Literal::string(""));
let encoding = quote!(#server_fn_path::#encoding);

let body = syn::parse::<ServerFnBody>(body.into())?;
let mut body = syn::parse::<ServerFnBody>(body.into())?;
let fn_name = &body.ident;
let fn_name_as_str = body.ident.to_string();
let vis = body.vis;
let block = body.block;

let fields = body
.inputs
.iter()
.iter_mut()
.filter(|f| {
if let Some(ctx) = &server_context {
!fn_arg_is_cx(f, ctx)
Expand All @@ -110,8 +110,33 @@ pub fn server_macro_impl(
}
FnArg::Typed(t) => t,
};
quote! { pub #typed_arg }
});
let mut default = false;
let mut other_attrs = Vec::new();
for attr in typed_arg.attrs.iter() {
if !attr.path().is_ident("server") {
other_attrs.push(attr.clone());
continue;
}
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("default") && meta.input.is_empty() {
default = true;
Ok(())
} else {
Err(meta.error(
"Unrecognized #[server] attribute, expected \
#[server(default)]",
))
}
})?;
}
typed_arg.attrs = other_attrs;
if default {
Ok(quote! { #[serde(default)] pub #typed_arg })
} else {
Ok(quote! { pub #typed_arg })
}
})
.collect::<Result<Vec<_>>>()?;

let cx_arg = body.inputs.iter().next().and_then(|f| {
server_context
Expand Down

0 comments on commit 31cd000

Please sign in to comment.