leptos_forge_utils_leptos 0.6.3

Little utilities to use together with Leptos
Documentation
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Generic implementation of the [RwSignal] to make it more useful for writing applications
//!
//! Author of leptos thinks RwSignal can be confusing, I say it it's a developer responsibility to not make it
//! confusing by:
//!
//! 1. Naming stuff correctly
//! 2. Writing documentation that explains what the signal does and how to use it
//! 3. Believing  in the intelligence of the user (that might be somewhat questionable from time to time, I agree)
//!

use std::panic::Location;

use leptos::attr::Attribute;
use leptos::attr::AttributeValue;
use leptos::attr::any_attribute::AnyAttribute;
use leptos::prelude::Track;
use leptos::prelude::guards::ReadGuard;
use leptos::tachys::html::property::IntoProperty;
use leptos::tachys::hydration::Cursor;
use leptos::tachys::reactive_graph::RenderEffectState;
use leptos::tachys::reactive_graph::bind::IntoSplitSignal;
use leptos::tachys::renderer::types::Element;
use leptos::tachys::ssr::StreamBuilder;
use leptos::tachys::view::Mountable;
use leptos::tachys::view::Position;
use leptos::tachys::view::PositionState;
use leptos::tachys::view::Render;
use leptos::tachys::view::RenderHtml;
use leptos::tachys::view::add_attr::AddAnyAttr;

use leptos::server_fn::serde::Serialize;
use leptos::server_fn::serde::Serializer;
use reactive_stores::StoreField;
use reactive_stores::Subfield;

use reactive_graph::effect::RenderEffect;
use reactive_graph::owner::SyncStorage;
use reactive_graph::signal::signal;
use reactive_graph::traits::DefinedAt;
use reactive_graph::traits::Dispose;
use reactive_graph::traits::Get;
use reactive_graph::traits::ReadUntracked;
use reactive_graph::traits::Set;
use reactive_graph::traits::Update;
use reactive_graph::traits::With;
use reactive_graph::wrappers::read::Signal;
use reactive_graph::wrappers::read::SignalReadGuard;
use reactive_graph::wrappers::write::SignalSetter;
use utils::prelude::ThreadSafe;

/// Signal which allows reading and writing a value
///
/// You should never, ever create an RwSignal which reads from one value and writes to the other. I consider
/// you to be warned.
#[derive(Debug)]
pub struct URwSignal<T>
where
    T: ThreadSafe,
{
    /// Location where the URwSignal was created
    defined_at: &'static Location<'static>,
    /// signal from which we can read the value
    read_signal: Signal<T>,
    /// signal from where we can write the value to
    write_signal: SignalSetter<T>,
}

impl<T> Clone for URwSignal<T>
where
    T: ThreadSafe,
{
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for URwSignal<T> where T: ThreadSafe {}

impl<T> URwSignal<T>
where
    T: ThreadSafe,
{
    /// Creates a new RwSignal with initial value `initial`
    #[track_caller]
    pub fn new(initial: T) -> Self {
        let (read, write) = signal(initial);
        Self {
            defined_at: Location::caller(),
            read_signal: read.into(),
            write_signal: write.into(),
        }
    }
}

impl<T> URwSignal<T>
where
    T: ThreadSafe + Clone,
{
    /// Returns a read only part of the RwSignal
    pub fn read_only(&self) -> Signal<T> {
        self.read_signal
    }

    /// Returns a write only part of the RwSignal
    pub fn write_only(&self) -> SignalSetter<T> {
        self.write_signal
    }

    /// Transforms signal of T into signal of A
    ///
    /// Allows consistent read/write with the derived URwSignal
    #[track_caller]
    pub fn map<A>(
        &self,
        towards: impl Fn(&T) -> A + Send + Sync + 'static,
        from: impl Fn(&mut T, A) + Send + Sync + 'static,
    ) -> URwSignal<A>
    where
        A: ThreadSafe,
    {
        let read: Signal<T> = self.read_signal;
        let new_read: Signal<A> = Signal::derive(move || read.with(|t| towards(t)));

        let write: SignalSetter<T> = self.write_signal;
        let new_write = SignalSetter::map(move |a: A| {
            let mut t = read.get();
            from(&mut t, a);
            write.set(t);
        });

        URwSignal {
            defined_at: Location::caller(),
            read_signal: new_read,
            write_signal: new_write,
        }
    }
}

impl<T> Dispose for URwSignal<T>
where
    T: ThreadSafe,
{
    fn dispose(self) {
        self.read_signal.dispose();
    }
}

impl<T> DefinedAt for URwSignal<T>
where
    T: ThreadSafe,
{
    fn defined_at(&self) -> Option<&'static Location<'static>> {
        Some(self.defined_at)
    }
}

impl<T> PartialEq for URwSignal<T>
where
    T: ThreadSafe,
{
    fn eq(&self, other: &Self) -> bool {
        self.read_signal == other.read_signal
    }
}

impl<T> Eq for URwSignal<T> where T: ThreadSafe {}

impl<T> ReadUntracked for URwSignal<T>
where
    T: ThreadSafe + Clone,
{
    type Value = ReadGuard<T, SignalReadGuard<T, SyncStorage>>;

    fn try_read_untracked(&self) -> Option<Self::Value> {
        self.read_signal.try_read_untracked()
    }
}

impl<T> Set for URwSignal<T>
where
    T: ThreadSafe,
{
    type Value = T;

    fn set(&self, new_value: Self::Value) {
        self.write_signal.set(new_value);
    }

    fn try_set(&self, value: Self::Value) -> Option<Self::Value> {
        self.write_signal.try_set(value)
    }
}

impl<T> Get for URwSignal<T>
where
    T: ThreadSafe + Clone,
{
    type Value = T;

    fn try_get(&self) -> Option<Self::Value> {
        self.read_signal.try_get()
    }
}

impl<T> IntoProperty for URwSignal<T>
where
    T: 'static + IntoProperty + Clone + Send + Sync,
    <T as IntoProperty>::State: 'static,
    URwSignal<T>: Get<Value = T> + Clone,
{
    type State = RenderEffect<<T as IntoProperty>::State>;
    type Cloneable = Self;
    type CloneableOwned = Self;

    fn build(self, el: &Element, key: &str) -> Self::State {
        (move || self.get()).build(el, key)
    }

    fn hydrate<const FROM_SERVER: bool>(self, el: &Element, key: &str) -> Self::State {
        (move || self.get()).hydrate::<FROM_SERVER>(el, key)
    }

    fn rebuild(self, state: &mut Self::State, key: &str) {
        (move || self.get()).rebuild(state, key)
    }

    fn into_cloneable(self) -> Self::Cloneable {
        self
    }

    fn into_cloneable_owned(self) -> Self::CloneableOwned {
        self
    }
}

impl<T> From<T> for URwSignal<T>
where
    T: ThreadSafe,
{
    #[track_caller]
    fn from(value: T) -> Self {
        let (read, write) = signal(value);

        URwSignal {
            defined_at: Location::caller(),
            read_signal: read.into(),
            write_signal: write.into(),
        }
    }
}

impl<T> Render for URwSignal<T>
where
    T: ThreadSafe + Clone + Render,
    <T as Render>::State: 'static,
    URwSignal<T>: Get<Value = T>,
{
    type State = RenderEffectState<<T as Render>::State>;

    fn build(self) -> Self::State {
        (move || self.get()).build()
    }

    fn rebuild(self, state: &mut Self::State) {
        let new = self.build();
        let mut old = std::mem::replace(state, new);
        old.insert_before_this(state);
        old.unmount();
    }
}

impl<T> RenderHtml for URwSignal<T>
where
    T: ThreadSafe + Clone + RenderHtml,
    <T as Render>::State: 'static,
    URwSignal<T>: Get<Value = T>,
{
    type AsyncOutput = Self;
    type Owned = Self;

    const MIN_LENGTH: usize = 0;

    fn dry_resolve(&mut self) {}

    async fn resolve(self) -> Self::AsyncOutput {
        self
    }

    fn html_len(&self) -> usize {
        T::MIN_LENGTH
    }

    fn to_html_with_buf(
        self,
        buf: &mut String,
        position: &mut Position,
        escape: bool,
        mark_branches: bool,
        extra_attrs: Vec<AnyAttribute>,
    ) {
        let value = self.get();
        value.to_html_with_buf(buf, position, escape, mark_branches, extra_attrs)
    }

    fn to_html_async_with_buf<const OUT_OF_ORDER: bool>(
        self,
        buf: &mut StreamBuilder,
        position: &mut Position,
        escape: bool,
        mark_branches: bool,
        extra_attrs: Vec<AnyAttribute>,
    ) where
        Self: Sized,
    {
        let value = self.get();
        value.to_html_async_with_buf::<OUT_OF_ORDER>(
            buf,
            position,
            escape,
            mark_branches,
            extra_attrs,
        );
    }

    fn hydrate<const FROM_SERVER: bool>(
        self,
        cursor: &Cursor,
        position: &PositionState,
    ) -> Self::State {
        (move || self.get()).hydrate::<FROM_SERVER>(cursor, position)
    }

    fn into_owned(self) -> Self::Owned {
        self
    }
}

impl<T> AddAnyAttr for URwSignal<T>
where
    T: ThreadSafe + Clone + RenderHtml,
    <T as Render>::State: 'static,
    URwSignal<T>: Get<Value = T>,
{
    type Output<SomeNewAttr: Attribute> = Self;

    fn add_any_attr<NewAttr: Attribute>(self, _attr: NewAttr) -> Self::Output<NewAttr>
    where
        Self::Output<NewAttr>: RenderHtml,
    {
        todo!()
    }
}

impl<T> AttributeValue for URwSignal<T>
where
    T: ThreadSafe + Clone + AttributeValue,
    <T as AttributeValue>::State: 'static,
    URwSignal<T>: Get<Value = T>,
{
    type AsyncOutput = Self;
    type State = RenderEffect<<T as AttributeValue>::State>;
    type Cloneable = Self;
    type CloneableOwned = Self;

    fn html_len(&self) -> usize {
        0
    }

    fn to_html(self, key: &str, buf: &mut String) {
        let value = self.get();
        value.to_html(key, buf);
    }

    fn to_template(_key: &str, _buf: &mut String) {}

    fn hydrate<const FROM_SERVER: bool>(self, key: &str, el: &Element) -> Self::State {
        (move || self.get()).hydrate::<FROM_SERVER>(key, el)
    }

    fn build(self, el: &Element, key: &str) -> Self::State {
        (move || self.get()).build(el, key)
    }

    fn rebuild(self, key: &str, state: &mut Self::State) {
        (move || self.get()).rebuild(key, state)
    }

    fn into_cloneable(self) -> Self::Cloneable {
        self
    }

    fn into_cloneable_owned(self) -> Self::CloneableOwned {
        self
    }

    fn dry_resolve(&mut self) {}

    async fn resolve(self) -> Self::AsyncOutput {
        self
    }
}

impl<T> Serialize for URwSignal<T>
where
    T: ThreadSafe + Serialize,
{
    fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
    where
        Ser: Serializer,
    {
        self.read_signal.serialize(serializer)
    }
}

impl<T> From<URwSignal<T>> for Signal<T>
where
    T: ThreadSafe,
{
    #[track_caller]
    fn from(val: URwSignal<T>) -> Self {
        val.read_signal
    }
}

impl<T> IntoSplitSignal for URwSignal<T>
where
    T: ThreadSafe + Clone,
{
    type Value = T;
    type Read = Signal<T>;
    type Write = SignalSetter<T>;

    fn into_split_signal(self) -> (Self::Read, Self::Write) {
        (self.read_signal, self.write_signal)
    }
}

impl<Inner, Prev, T> From<Subfield<Inner, Prev, T>> for URwSignal<T>
where
    Inner: StoreField<Value = Prev> + Track + ThreadSafe + Clone,
    Prev: 'static,
    T: Send + Sync + Clone + 'static,
{
    #[track_caller]
    fn from(value: Subfield<Inner, Prev, T>) -> Self {
        let r: Signal<T> = value.clone().into();
        let w: SignalSetter<T> = SignalSetter::map(move |t| {
            value.update(|v| {
                *v = t;
            });
        });

        URwSignal {
            defined_at: Location::caller(),
            read_signal: r,
            write_signal: w,
        }
    }
}

impl<T> From<URwSignal<T>> for SignalSetter<T>
where
    T: Send + Sync + 'static,
{
    fn from(value: URwSignal<T>) -> Self {
        value.write_signal
    }
}

impl<T: ThreadSafe + Default> Default for URwSignal<T> {
    fn default() -> Self {
        URwSignal::new(T::default())
    }
}