leptos_server/
multi_action.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use reactive_graph::{
    actions::{ArcMultiAction, MultiAction},
    traits::DefinedAt,
};
use server_fn::{ServerFn, ServerFnError};
use std::{ops::Deref, panic::Location};

/// An [`ArcMultiAction`] that can be used to call a server function.
pub struct ArcServerMultiAction<S>
where
    S: ServerFn + 'static,
    S::Output: 'static,
{
    inner: ArcMultiAction<S, Result<S::Output, ServerFnError<S::Error>>>,
    #[cfg(debug_assertions)]
    defined_at: &'static Location<'static>,
}

impl<S> ArcServerMultiAction<S>
where
    S: ServerFn + Clone + Send + Sync + 'static,
    S::Output: Send + Sync + 'static,
    S::Error: Send + Sync + 'static,
{
    /// Creates a new [`ArcMultiAction`] which, when dispatched, will call the server function `S`.
    #[track_caller]
    pub fn new() -> Self {
        Self {
            inner: ArcMultiAction::new(|input: &S| {
                S::run_on_client(input.clone())
            }),
            #[cfg(debug_assertions)]
            defined_at: Location::caller(),
        }
    }
}

impl<S> Deref for ArcServerMultiAction<S>
where
    S: ServerFn + 'static,
    S::Output: 'static,
{
    type Target = ArcMultiAction<S, Result<S::Output, ServerFnError<S::Error>>>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<S> Clone for ArcServerMultiAction<S>
where
    S: ServerFn + 'static,
    S::Output: 'static,
{
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            #[cfg(debug_assertions)]
            defined_at: self.defined_at,
        }
    }
}

impl<S> Default for ArcServerMultiAction<S>
where
    S: ServerFn + Clone + Send + Sync + 'static,
    S::Output: Send + Sync + 'static,
    S::Error: Send + Sync + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<S> DefinedAt for ArcServerMultiAction<S>
where
    S: ServerFn + 'static,
    S::Output: 'static,
{
    fn defined_at(&self) -> Option<&'static Location<'static>> {
        #[cfg(debug_assertions)]
        {
            Some(self.defined_at)
        }
        #[cfg(not(debug_assertions))]
        {
            None
        }
    }
}

/// A [`MultiAction`] that can be used to call a server function.
pub struct ServerMultiAction<S>
where
    S: ServerFn + 'static,
    S::Output: 'static,
{
    inner: MultiAction<S, Result<S::Output, ServerFnError<S::Error>>>,
    #[cfg(debug_assertions)]
    defined_at: &'static Location<'static>,
}

impl<S> From<ServerMultiAction<S>>
    for MultiAction<S, Result<S::Output, ServerFnError<S::Error>>>
where
    S: ServerFn + 'static,
    S::Output: 'static,
{
    fn from(value: ServerMultiAction<S>) -> Self {
        value.inner
    }
}

impl<S> ServerMultiAction<S>
where
    S: ServerFn + Send + Sync + Clone + 'static,
    S::Output: Send + Sync + 'static,
    S::Error: Send + Sync + 'static,
{
    /// Creates a new [`MultiAction`] which, when dispatched, will call the server function `S`.
    pub fn new() -> Self {
        Self {
            inner: MultiAction::new(|input: &S| {
                S::run_on_client(input.clone())
            }),
            #[cfg(debug_assertions)]
            defined_at: Location::caller(),
        }
    }
}

impl<S> Clone for ServerMultiAction<S>
where
    S: ServerFn + 'static,
    S::Output: 'static,
{
    fn clone(&self) -> Self {
        *self
    }
}

impl<S> Copy for ServerMultiAction<S>
where
    S: ServerFn + 'static,
    S::Output: 'static,
{
}

impl<S> Deref for ServerMultiAction<S>
where
    S: ServerFn + 'static,
    S::Output: 'static,
    S::Error: 'static,
{
    type Target = MultiAction<S, Result<S::Output, ServerFnError<S::Error>>>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<S> Default for ServerMultiAction<S>
where
    S: ServerFn + Clone + Send + Sync + 'static,
    S::Output: Send + Sync + 'static,
    S::Error: Send + Sync + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<S> DefinedAt for ServerMultiAction<S>
where
    S: ServerFn + 'static,
    S::Output: 'static,
{
    fn defined_at(&self) -> Option<&'static Location<'static>> {
        #[cfg(debug_assertions)]
        {
            Some(self.defined_at)
        }
        #[cfg(not(debug_assertions))]
        {
            None
        }
    }
}