Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "Mutations" #29

Open
nicoburniske opened this issue Feb 24, 2024 · 0 comments
Open

Support "Mutations" #29

nicoburniske opened this issue Feb 24, 2024 · 0 comments

Comments

@nicoburniske
Copy link
Member

Add custom mutation helpers compatible with Server actions. Rough draft below

pub fn create_server_action_with_callbacks<S>(
    on_invoke: impl Fn(&S) + 'static,
    on_settled: impl Fn((S, &Result<S::Output, ServerFnError<S::Error>>)) + 'static,
) -> Action<S, Result<S::Output, ServerFnError<S::Error>>>
where
    S: Clone + server_fn::ServerFn,
    S::Error: Clone,
{
    let on_invoke = Rc::new(on_invoke);
    let on_settled = Rc::new(on_settled);

    // The server is able to call the function directly
    #[cfg(feature = "ssr")]
    let action_function = move |args: &S| {
        let args = args.clone();
        let on_invoke = on_invoke.clone();
        let on_settled = on_settled.clone();

        async move {
            on_invoke(&args);
            let result = S::run_body(args.clone()).await;
            on_settled((args, &result));
            result
        }
    };

    // When not on the server send a fetch to request the fn call.
    #[cfg(not(feature = "ssr"))]
    let action_function = move |args: &S| {
        let args = args.clone();
        let on_invoke = on_invoke.clone();
        let on_settled = on_settled.clone();

        async move {
            on_invoke(&args);
            let result = S::run_on_client(args.clone()).await;
            on_settled((args, &result));
            result
        }
    };

    // create the action
    Action::new(action_function).using_server_fn()
}

pub fn create_server_multi_action_with_callbacks<S>(
    on_invoke: impl Fn(&S) + 'static,
    on_settled: impl Fn((S, &Result<S::Output, ServerFnError<S::Error>>)) + 'static,
) -> MultiAction<S, Result<S::Output, ServerFnError<S::Error>>>
where
    S: Clone + server_fn::ServerFn,
{
    let on_invoke = Rc::new(on_invoke);
    let on_settled = Rc::new(on_settled);
    #[cfg(feature = "ssr")]
    let c = move |args: &S| {
        let args = args.clone();
        let on_invoke = on_invoke.clone();
        let on_settled = on_settled.clone();
        async move {
            on_invoke(&args);
            let result = S::run_body(args.clone()).await;
            on_settled((args, &result));
            result
        }
    };
    #[cfg(not(feature = "ssr"))]
    let c = move |args: &S| {
        let args = args.clone();
        let on_invoke = on_invoke.clone();
        let on_settled = on_settled.clone();
        async move {
            on_invoke(&args);
            let result = S::run_on_client(args.clone()).await;
            on_settled((args, &result));
            result
        }
    };

    create_multi_action(c).using_server_fn::<S>()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant