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
//! State handlers determine how state should be created, modified, and shared.
use std::any::type_name;
#[cfg(feature = "future")]
use std::pin::Pin;
use std::rc::Rc;

use serde::{Deserialize, Serialize};
#[cfg(feature = "future")]
use std::future::Future;
use yew::{
    agent::{AgentLink, Bridge, Bridged, HandlerId},
    format::Json,
    services::{storage::Area, StorageService},
    Callback,
};
#[cfg(feature = "future")]
use yewtil::future::LinkFuture;

use crate::service::{ServiceInput, ServiceOutput, StateService};

pub(crate) type Reduction<T> = Rc<dyn Fn(&mut T)>;
pub(crate) type ReductionOnce<T> = Box<dyn FnOnce(&mut T)>;
pub type Changed = bool;

pub(crate) trait AgentLinkWrapper {
    type Message;
    type Input;
    type Output;

    fn send_message(&self, msg: Self::Message);
    fn send_input(&self, input: Self::Input);
    fn respond(&self, who: HandlerId, output: Self::Output);
    #[cfg(feature = "future")]
    fn send_future(&self, future: Pin<Box<dyn Future<Output = Self::Message>>>);
}

impl<H, SCOPE> AgentLinkWrapper for AgentLink<StateService<H, SCOPE>>
where
    H: StateHandler,
{
    type Message = H::Message;
    type Input = H::Input;
    type Output = H::Output;

    fn send_message(&self, msg: Self::Message) {
        AgentLink::<StateService<H, SCOPE>>::send_message(self, msg)
    }

    fn send_input(&self, input: Self::Input) {
        AgentLink::<StateService<H, SCOPE>>::send_input(self, ServiceInput::Handler(input))
    }

    fn respond(&self, who: HandlerId, output: Self::Output) {
        AgentLink::<StateService<H, SCOPE>>::respond(self, who, ServiceOutput::Handler(output))
    }

    #[cfg(feature = "future")]
    fn send_future(&self, future: Pin<Box<dyn Future<Output = Self::Message>>>) {
        LinkFuture::send_future(self, future)
    }
}

pub struct HandlerLink<H>
where
    H: StateHandler,
{
    link: Rc<dyn AgentLinkWrapper<Message = H::Message, Input = H::Input, Output = H::Output>>,
}

impl<H> Clone for HandlerLink<H>
where
    H: StateHandler,
{
    fn clone(&self) -> Self {
        Self {
            link: self.link.clone(),
        }
    }
}

type HandlerMsg<H> = <H as StateHandler>::Message;
type HandlerInput<H> = <H as StateHandler>::Input;
type HandlerOutput<H> = <H as StateHandler>::Output;

impl<H: StateHandler> HandlerLink<H> {
    pub(crate) fn new(
        link: impl AgentLinkWrapper<
                Message = HandlerMsg<H>,
                Input = HandlerInput<H>,
                Output = HandlerOutput<H>,
            > + 'static,
    ) -> Self {
        Self {
            link: Rc::new(link),
        }
    }

    pub fn send_message<T>(&self, msg: T)
    where
        T: Into<HandlerMsg<H>>,
    {
        self.link.send_message(msg.into())
    }

    pub fn send_input<T>(&self, msg: T)
    where
        T: Into<HandlerInput<H>>,
    {
        self.link.send_input(msg.into())
    }

    pub fn respond<T>(&self, who: HandlerId, output: T)
    where
        T: Into<HandlerOutput<H>>,
    {
        self.link.respond(who, output.into())
    }

    pub fn callback<F, IN, M>(&self, function: F) -> Callback<IN>
    where
        HandlerInput<H>: 'static,
        HandlerOutput<H>: 'static,
        HandlerMsg<H>: 'static,
        M: Into<HandlerMsg<H>>,
        F: Fn(IN) -> M + 'static,
    {
        let link = self.link.clone();
        let cb = move |x| {
            let result = function(x);
            link.send_message(result.into());
        };

        cb.into()
    }

    pub fn callback_once<F, IN, M>(&self, function: F) -> Callback<IN>
    where
        HandlerInput<H>: 'static,
        HandlerOutput<H>: 'static,
        HandlerMsg<H>: 'static,
        M: Into<HandlerMsg<H>>,
        F: FnOnce(IN) -> M + 'static,
    {
        let link = self.link.clone();
        let cb = move |x| {
            let result = function(x);
            link.send_message(result.into());
        };

        Callback::once(cb)
    }

    #[cfg(feature = "future")]
    pub fn send_future<F, M>(&self, future: F)
    where
        M: Into<HandlerMsg<H>>,
        F: Future<Output = M> + 'static,
    {
        let future = async { future.await.into() };
        self.link.send_future(Box::pin(future))
    }

    #[cfg(feature = "future")]
    pub fn callback_future<FN, FU, IN, M>(&self, function: FN) -> yew::Callback<IN>
    where
        HandlerInput<H>: 'static,
        HandlerOutput<H>: 'static,
        HandlerMsg<H>: 'static,
        M: Into<HandlerMsg<H>>,
        FU: Future<Output = M> + 'static,
        FN: Fn(IN) -> FU + 'static,
    {
        let link = self.link.clone();
        let cb = move |x| {
            let future = function(x);
            let future = async { future.await.into() };
            link.send_future(Box::pin(future));
        };

        cb.into()
    }

    #[cfg(feature = "future")]
    pub fn callback_once_future<FN, FU, IN, M>(&self, function: FN) -> yew::Callback<IN>
    where
        HandlerInput<H>: 'static,
        HandlerOutput<H>: 'static,
        HandlerMsg<H>: 'static,
        M: Into<HandlerMsg<H>>,
        FU: Future<Output = M> + 'static,
        FN: FnOnce(IN) -> FU + 'static,
    {
        let link = self.link.clone();
        let cb = move |x| {
            let future = function(x);
            let future = async { future.await.into() };
            link.send_future(Box::pin(future));
        };

        Callback::once(cb)
    }
}

/// Determines how state should be created, modified, and shared.
pub trait StateHandler: Sized {
    type Model: Clone;
    type Message;
    type Input;
    type Output;

    /// Create new state.
    fn new(_link: HandlerLink<Self>) -> Self;

    /// Return a reference to current state.
    fn state(&mut self) -> &mut Rc<Self::Model>;

    /// Called after state is changed.
    fn changed(&mut self) {}

    /// Receive messages from components.
    fn update(&mut self, _msg: Self::Message) -> Changed {
        false
    }

    #[allow(unused_variables)]
    fn handle_input(&mut self, msg: Self::Input, _who: HandlerId) -> Changed {
        false
    }
}

/// A direct bridge to given state handler. Unlike a [ServiceBridge](crate::service::ServiceBridge)
/// bridge, it can only send and receive [StateHandler](StateHandler) messages.
pub struct HandlerBridge<H, SCOPE = H>
where
    H: StateHandler + 'static,
    SCOPE: 'static,
{
    bridge: Box<dyn Bridge<StateService<H, SCOPE>>>,
}

impl<H, SCOPE> HandlerBridge<H, SCOPE>
where
    H: StateHandler + 'static,
{
    pub fn new(callback: Callback<H::Output>) -> Self {
        let callback = move |msg: ServiceOutput<H>| match msg {
            ServiceOutput::Handler(msg) => callback.emit(msg),
            // Service should only send messages to those who subscribe. We don't subscribe, so we
            // shouldn't receive any messages here.
            ServiceOutput::Service(_) => unreachable!(),
        };

        Self {
            bridge: StateService::<_, SCOPE>::bridge(callback.into()),
        }
    }

    /// Send a message to the state handler.
    pub fn send(&mut self, msg: H::Input) {
        self.bridge.send(ServiceInput::Handler(msg));
    }
}

/// Handler for basic shared state.
#[derive(Default, Clone)]
pub struct SharedHandler<T> {
    state: Rc<T>,
}

impl<T> StateHandler for SharedHandler<T>
where
    T: Clone + Default,
{
    type Model = T;
    type Message = ();
    type Input = ();
    type Output = ();

    fn new(_link: HandlerLink<Self>) -> Self {
        Default::default()
    }

    fn state(&mut self) -> &mut Rc<Self::Model> {
        &mut self.state
    }
}

/// Allows state to be stored persistently in local or session storage.
pub trait Storable: Serialize + for<'a> Deserialize<'a> {
    /// The key used to save and load state from storage.
    fn key() -> &'static str {
        type_name::<Self>()
    }
    /// The area to store state.
    fn area() -> Area {
        Area::Local
    }
}

/// Handler for shared state with persistent storage.
///
/// If persistent storage is disabled it just behaves like a `SharedHandler`.
#[derive(Default)]
pub struct StorageHandler<T> {
    state: Rc<T>,
    storage: Option<StorageService>,
}

impl<T> StorageHandler<T>
where
    T: Storable + Default,
{
    pub fn new() -> Self {
        let mut this: Self = Default::default();
        this.storage = StorageService::new(T::area()).ok();
        this.load_state();
        this
    }

    pub fn load_state(&mut self) {
        let result = self.storage.as_mut().map(|s| s.restore(T::key()));
        if let Some(Json(Ok(state))) = result {
            self.state = state;
        }
    }

    pub fn save_state(&mut self) {
        if let Some(storage) = &mut self.storage {
            storage.store(T::key(), Json(&self.state));
        }
    }
}

impl<T> StateHandler for StorageHandler<T>
where
    T: Default + Clone + Storable,
{
    type Model = T;
    type Message = ();
    type Input = ();
    type Output = ();

    fn new(_link: HandlerLink<Self>) -> Self {
        Self::new()
    }

    fn state(&mut self) -> &mut Rc<Self::Model> {
        &mut self.state
    }

    fn changed(&mut self) {
        self.save_state();
    }
}

impl<T> Clone for StorageHandler<T>
where
    T: Default + Clone + Storable,
{
    fn clone(&self) -> Self {
        let mut new = Self::new();
        new.state = self.state.clone();
        new
    }
}

impl<T: Storable> Storable for Option<T> {
    fn key() -> &'static str {
        T::key()
    }

    fn area() -> Area {
        T::area()
    }
}

impl<T: Storable> Storable for Rc<T> {
    fn key() -> &'static str {
        T::key()
    }

    fn area() -> Area {
        T::area()
    }
}