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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! Capabilities provide a user-friendly API to request side-effects from the shell.
//!
//! Typically, capabilities provide I/O and host API access. Capabilities are external to the
//! core Crux library. Some are part of the Crux core distribution, others are expected to be built by the
//! community. Apps can also build single-use capabilities where necessary.
//!
//! # Example use
//!
//! A typical use of a capability would look like the following:
//!
//! ```rust
//!# use url::Url;
//!# const API_URL: &str = "";
//!# pub enum Event { Increment, Set(crux_http::Result<crux_http::Response<usize>>) }
//!# #[derive(crux_core::macros::Effect)]
//!# pub struct Capabilities {
//!#     pub render: crux_core::render::Render<Event>,
//!#     pub http: crux_http::Http<Event>,
//!# }
//!# #[derive(Default)] pub struct Model { count: usize }
//!# #[derive(Default)] pub struct App;
//!#
//!# impl crux_core::App for App {
//!#     type Event = Event;
//!#     type Model = Model;
//!#     type ViewModel = ();
//!#     type Capabilities = Capabilities;
//! fn update(&self, event: Self::Event, model: &mut Self::Model, caps: &Self::Capabilities) {
//!     match event {
//!         //...
//!         Event::Increment => {
//!             model.count += 1;
//!             caps.render.render(); // Render capability
//!
//!             let base = Url::parse(API_URL).unwrap();
//!             let url = base.join("/inc").unwrap();
//!             caps.http.post(url).expect_json().send(Event::Set); // HTTP client capability
//!         }
//!         Event::Set(_) => todo!(),
//!     }
//! }
//!# fn view(&self, model: &Self::Model) {
//!#     unimplemented!()
//!# }
//!# }

//! ```
//!
//! Capabilities don't _perform_ side-effects themselves, they request them from the Shell. As a consequence
//! the capability calls within the `update` function **only queue up the requests**. The side-effects themselves
//! are performed concurrently and don't block the update function.
//!
//! In order to use a capability, the app needs to include it in its `Capabilities` associated type and `WithContext`
//! trait implementation (which can be provided by the `crux_core::macros::Effect` macro). For example:
//!
//! ```rust
//! mod root {
//!
//! // An app module which can be reused in different apps
//! mod my_app {
//!     use crux_core::{capability::CapabilityContext, App, render::Render};
//!     use crux_core::macros::Effect;
//!     use serde::{Serialize, Deserialize};
//!
//!     #[derive(Default)]
//!     pub struct MyApp;
//!     #[derive(Serialize, Deserialize)]
//!     pub struct Event;
//!
//!     // The `Effect` derive macro generates an `Effect` type that is used by the
//!     // Shell to dispatch side-effect requests to the right capability implementation
//!     // (and, in some languages, checking that all necessary capabilities are implemented)
//!     #[derive(Effect)]
//!     pub struct Capabilities {
//!         pub render: Render<Event>
//!     }
//!
//!     impl App for MyApp {
//!         type Model = ();
//!         type Event = Event;
//!         type ViewModel = ();
//!         type Capabilities = Capabilities;
//!
//!         fn update(&self, event: Event, model: &mut (), caps: &Capabilities) {
//!             caps.render.render();
//!         }
//!
//!         fn view(&self, model: &()) {
//!             ()
//!         }
//!     }
//! }
//! }
//! ```
//!
//! # Implementing a capability
//!
//! Capabilities provide an interface to request side-effects. The interface has asynchronous semantics
//! with a form of callback. A typical capability call can look like this:
//!
//! ```rust,ignore
//! caps.ducks.get_in_a_row(10, Event::RowOfDucks)
//! ```
//!
//! The call above translates into "Get 10 ducks in a row and return them to me using the `RowOfDucks` event".
//! The capability's job is to translate this request into a serializable message and instruct the Shell to
//! do the duck herding and when it receives the ducks back, wrap them in the requested event and return it
//! to the app.
//!
//! We will refer to `get_in_row` in the above call as an _operation_, the `10` is an _input_, and the
//! `Event::RowOfDucks` is an event constructor - a function, which eventually receives the row of ducks
//! and returns a variant of the `Event` enum. Conveniently, enum tuple variants can be used as functions,
//! and so that will be the typical use.
//!
//! This is what the capability implementation could look like:
//!
//! ```rust
//! use crux_core::{
//!     capability::{CapabilityContext, Operation},
//! };
//! use crux_core::macros::Capability;
//! use serde::{Serialize, Deserialize};
//!
//! // A duck
//! #[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
//! struct Duck;
//!
//! // Operations that can be requested from the Shell
//! #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
//! enum DuckOperation {
//!     GetInARow(usize)
//! }
//!
//! // Respective outputs for those operations
//! #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
//! enum DuckOutput {
//!     GetInRow(Vec<Duck>)
//! }
//!
//! // Link the input and output type
//! impl Operation for DuckOperation {
//!     type Output = DuckOutput;
//! }
//!
//! // The capability. Context will provide the interface to the rest of the system.
//! #[derive(Capability)]
//! struct Ducks<Event> {
//!     context: CapabilityContext<DuckOperation, Event>
//! };
//!
//! impl<Event> Ducks<Event> {
//!     pub fn new(context: CapabilityContext<DuckOperation, Event>) -> Self {
//!         Self { context }
//!     }
//!
//!     pub fn get_in_a_row<F>(&self, number_of_ducks: usize, event: F)
//!     where
//!         Event: 'static,
//!         F: FnOnce(Vec<Duck>) -> Event + Send + 'static,
//!     {
//!         let ctx = self.context.clone();
//!         // Start a shell interaction
//!         self.context.spawn(async move {
//!             // Instruct Shell to get ducks in a row and await the ducks
//!             let ducks = ctx.request_from_shell(DuckOperation::GetInARow(number_of_ducks)).await;
//!
//!             // Unwrap the ducks and wrap them in the requested event
//!             // This will always succeed, as long as the Shell implementation is correct
//!             // and doesn't send the wrong output type back
//!             if let DuckOutput::GetInRow(ducks) = ducks {
//!                 // Queue an app update with the ducks event
//!                 ctx.update_app(event(ducks));
//!             }
//!         })
//!    }
//! }
//! ```
//!
//! The `self.context.spawn` API allows a multi-step transaction with the Shell to be performed by a capability
//! without involving the app, until the exchange has completed. During the exchange, one or more events can
//! be emitted (allowing a subscription or streaming like capability to be built).
//!
//! For Shell requests that have no output, you can use [`CapabilityContext::notify_shell`].
//!
//! `DuckOperation` and `DuckOutput` show how the set of operations can be extended. In simple capabilities,
//! with a single operation, these can be structs, or simpler types. For example, the HTTP capability works directly with
//! `HttpRequest` and `HttpResponse`.

pub(crate) mod channel;

mod executor;
mod shell_request;
mod shell_stream;

use futures::Future;
use std::sync::Arc;

pub(crate) use channel::channel;
pub(crate) use executor::{executor_and_spawner, QueuingExecutor};

use crate::Request;
use channel::Sender;

/// Operation trait links together input and output of a side-effect.
///
/// You implement `Operation` on the payload sent by the capability to the shell using [`CapabilityContext::request_from_shell`].
///
/// For example (from `crux_http`):
///
/// ```rust,ignore
/// impl Operation for HttpRequest {
///     type Output = HttpResponse;
/// }
/// ```
pub trait Operation: serde::Serialize + PartialEq + Send + 'static {
    /// `Output` assigns the type this request results in.
    type Output: serde::de::DeserializeOwned + Send + 'static;
}

/// A type that can be used as a capability operation, but which will never be sent to the shell.
/// This type is useful for capabilities that don't request effects.
/// For example, you can use this type as the Operation for a
/// capability that just composes other capabilities.
///
/// e.g.
/// ```rust
/// # use crux_core::capability::{CapabilityContext, Never};
/// # use crux_core::macros::Capability;
/// #[derive(Capability)]
/// pub struct Compose<E> {
///     context: CapabilityContext<Never, E>,
/// }
/// # impl<E> Compose<E> {
/// #     pub fn new(context: CapabilityContext<Never, E>) -> Self {
/// #         Self { context }
/// #     }
/// # }
///
/// ```

#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Never {}

/// Implement `Operation` for `Never` to allow using it as a capability operation.
impl Operation for Never {
    type Output = ();
}

/// Implement the `Capability` trait for your capability. This will allow
/// mapping events when composing apps from submodules.
///
/// Note that this implementation can be generated by the `crux_core::macros::Capability` derive macro.
///
/// Example:
///
/// ```rust
/// # use crux_core::{Capability, capability::{CapabilityContext, Operation}};
/// # pub struct Http<Ev> {
/// #     context: CapabilityContext<HttpOperation, Ev>,
/// # }
/// # #[derive(serde::Serialize, PartialEq, Eq)] pub struct HttpOperation;
/// # impl Operation for HttpOperation {
/// #     type Output = ();
/// # }
/// # impl<Ev> Http<Ev> where Ev: 'static, {
/// #     pub fn new(context: CapabilityContext<HttpOperation, Ev>) -> Self {
/// #         Self { context }
/// #     }
/// # }
/// impl<Ev> Capability<Ev> for Http<Ev> {
///     type Operation = HttpOperation;
///     type MappedSelf<MappedEv> = Http<MappedEv>;
///
///     fn map_event<F, NewEvent>(&self, f: F) -> Self::MappedSelf<NewEvent>
///     where
///         F: Fn(NewEvent) -> Ev + Send + Sync + 'static,
///         Ev: 'static,
///         NewEvent: 'static,
///     {
///         Http::new(self.context.map_event(f))
///     }
/// }
/// ```
pub trait Capability<Ev> {
    type Operation: Operation;

    type MappedSelf<MappedEv>;

    fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
    where
        F: Fn(NewEv) -> Ev + Send + Sync + 'static,
        Ev: 'static,
        NewEv: 'static + Send;
}

/// Allows Crux to construct app's set of required capabilities, providing context
/// they can then use to request effects and dispatch events.
///
/// `new_with_context` is called by Crux and should return an instance of the app's `Capabilities` type with
/// all capabilities constructed with context passed in. Use `Context::specialize` to
/// create an appropriate context instance with the effect constructor which should
/// wrap the requested operations.
///
/// Note that this implementation can be generated by the derive macro `crux_core::macros::Effect`.
///
/// ```rust
/// # #[derive(Default)]
/// # struct App;
/// # pub enum Event {}
/// # #[allow(dead_code)]
/// # pub struct Capabilities {
/// #     http: crux_http::Http<Event>,
/// #     render: crux_core::render::Render<Event>,
/// # }
/// # pub enum Effect {
/// #     Http(crux_core::Request<<crux_http::Http<Event> as crux_core::capability::Capability<Event>>::Operation>),
/// #     Render(crux_core::Request<<crux_core::render::Render<Event> as crux_core::capability::Capability<Event>>::Operation>),
/// # }
/// # #[derive(serde::Serialize)]
/// # pub enum EffectFfi {
/// #     Http(<crux_http::Http<Event> as crux_core::capability::Capability<Event>>::Operation),
/// #     Render(<crux_core::render::Render<Event> as crux_core::capability::Capability<Event>>::Operation),
/// # }
/// # impl crux_core::App for App {
/// #     type Event = Event;
/// #     type Model = ();
/// #     type ViewModel = ();
/// #     type Capabilities = Capabilities;
/// #     fn update(&self, _event: Self::Event, _model: &mut Self::Model, _caps: &Self::Capabilities) {
/// #         unimplemented!()
/// #     }
/// #     fn view(&self, _model: &Self::Model) -> Self::ViewModel {
/// #         unimplemented!()
/// #     }
/// # }
/// # impl crux_core::Effect for Effect {
/// #     type Ffi = EffectFfi;
/// #     fn serialize(self) -> (Self::Ffi, crux_core::bridge::ResolveSerialized) {
/// #         match self {
/// #             Effect::Http(request) => request.serialize(EffectFfi::Http),
/// #             Effect::Render(request) => request.serialize(EffectFfi::Render),
/// #         }
/// #     }
/// # }
/// impl crux_core::WithContext<Event, Effect> for Capabilities {
///     fn new_with_context(
///         context: crux_core::capability::ProtoContext<Effect, Event>,
///     ) -> Capabilities {
///         Capabilities {
///             http: crux_http::Http::new(context.specialize(Effect::Http)),
///             render: crux_core::render::Render::new(context.specialize(Effect::Render)),
///         }
///     }
/// }
/// ```
pub trait WithContext<Ev, Ef> {
    fn new_with_context(context: ProtoContext<Ef, Ev>) -> Self;
}

/// An interface for capabilities to interact with the app and the shell.
///
/// To use [`update_app`](CapabilityContext::update_app), [`notify_shell`](CapabilityContext::notify_shell)
/// or [`request_from_shell`](CapabilityContext::request_from_shell), spawn a task first.
///
/// For example (from `crux_time`)
///
/// ```rust
/// # #[derive(PartialEq,serde::Serialize)]pub struct TimeRequest;
/// # #[derive(serde::Deserialize)]pub struct TimeResponse(pub String);
/// # impl crux_core::capability::Operation for TimeRequest {
/// #     type Output = TimeResponse;
/// # }
/// # pub struct Time<Ev> {
/// #     context: crux_core::capability::CapabilityContext<TimeRequest, Ev>,
/// # }
/// # impl<Ev> Time<Ev> where Ev: 'static, {
/// #     pub fn new(context: crux_core::capability::CapabilityContext<TimeRequest, Ev>) -> Self {
/// #         Self { context }
/// #     }
///
/// pub fn get<F>(&self, callback: F)
/// where
///     F: FnOnce(TimeResponse) -> Ev + Send + Sync + 'static,
/// {
///     let ctx = self.context.clone();
///     self.context.spawn(async move {
///         let response = ctx.request_from_shell(TimeRequest).await;
///
///         ctx.update_app(callback(response));
///     });
/// }
/// # }
/// ```
///
// used in docs/internals/runtime.md
// ANCHOR: capability_context
pub struct CapabilityContext<Op, Event>
where
    Op: Operation,
{
    inner: std::sync::Arc<ContextInner<Op, Event>>,
}

struct ContextInner<Op, Event>
where
    Op: Operation,
{
    shell_channel: Sender<Request<Op>>,
    app_channel: Sender<Event>,
    spawner: executor::Spawner,
}
// ANCHOR_END: capability_context

/// Initial version of capability Context which has not yet been specialized to a chosen capability
pub struct ProtoContext<Eff, Event> {
    shell_channel: Sender<Eff>,
    app_channel: Sender<Event>,
    spawner: executor::Spawner,
}

impl<Op, Ev> Clone for CapabilityContext<Op, Ev>
where
    Op: Operation,
{
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<Eff, Ev> ProtoContext<Eff, Ev>
where
    Ev: 'static,
    Eff: 'static,
{
    pub(crate) fn new(
        shell_channel: Sender<Eff>,
        app_channel: Sender<Ev>,
        spawner: executor::Spawner,
    ) -> Self {
        Self {
            shell_channel,
            app_channel,
            spawner,
        }
    }

    /// Specialize the CapabilityContext to a specific capability, wrapping its operations into
    /// an Effect `Ef`. The `func` argument will typically be an Effect variant constructor, but
    /// can be any function taking the capability's operation type and returning
    /// the effect type.
    ///
    /// This will likely only be called from the implementation of [`WithContext`]
    /// for the app's `Capabilities` type. You should not need to call this function directly.
    pub fn specialize<Op, F>(&self, func: F) -> CapabilityContext<Op, Ev>
    where
        F: Fn(Request<Op>) -> Eff + Sync + Send + Copy + 'static,
        Op: Operation,
    {
        CapabilityContext::new(
            self.shell_channel.map_input(func),
            self.app_channel.clone(),
            self.spawner.clone(),
        )
    }
}

impl<Op, Ev> CapabilityContext<Op, Ev>
where
    Op: Operation,
    Ev: 'static,
{
    pub(crate) fn new(
        shell_channel: Sender<Request<Op>>,
        app_channel: Sender<Ev>,
        spawner: executor::Spawner,
    ) -> Self {
        let inner = Arc::new(ContextInner {
            shell_channel,
            app_channel,
            spawner,
        });

        CapabilityContext { inner }
    }

    /// Spawn a task to do the asynchronous work. Within the task, async code
    /// can be used to interact with the Shell and the App.
    pub fn spawn(&self, f: impl Future<Output = ()> + 'static + Send) {
        self.inner.spawner.spawn(f);
    }

    /// Send an effect request to the shell in a fire and forget fashion. The
    /// provided `operation` does not expect anything to be returned back.
    pub async fn notify_shell(&self, operation: Op) {
        // This function might look like it doesn't need to be async but
        // it's important that it is.  It forces all capabilities to
        // spawn onto the executor which keeps the ordering of effects
        // consistent with their function calls.
        self.inner
            .shell_channel
            .send(Request::resolves_never(operation));
    }

    /// Send an event to the app. The event will be processed on the next
    /// run of the update loop. You can call `update_app` several times,
    /// the events will be queued up and processed sequentially after your
    /// async task either `await`s or finishes.
    pub fn update_app(&self, event: Ev) {
        self.inner.app_channel.send(event);
    }

    /// Transform the CapabilityContext into one which uses the provided function to
    /// map each event dispatched with `update_app` to a different event type.
    ///
    /// This is useful when composing apps from modules to wrap a submodule's
    /// event type with a specific variant of the parent module's event, so it can
    /// be forwarded to the submodule when received.
    ///
    /// In a typical case you would implement `From` on the submodule's `Capabilities` type
    ///
    /// ```rust
    /// # use crux_core::Capability;
    /// # #[derive(Default)]
    /// # struct App;
    /// # pub enum Event {
    /// #     Submodule(child::Event),
    /// # }
    /// # #[derive(crux_core::macros::Effect)]
    /// # pub struct Capabilities {
    /// #     some_capability: crux_time::Time<Event>,
    /// #     render: crux_core::render::Render<Event>,
    /// # }
    /// # impl crux_core::App for App {
    /// #     type Event = Event;
    /// #     type Model = ();
    /// #     type ViewModel = ();
    /// #     type Capabilities = Capabilities;
    /// #     fn update(
    /// #         &self,
    /// #         _event: Self::Event,
    /// #         _model: &mut Self::Model,
    /// #         _caps: &Self::Capabilities,
    /// #     ) {
    /// #         unimplemented!()
    /// #     }
    /// #     fn view(&self, _model: &Self::Model) -> Self::ViewModel {
    /// #         unimplemented!()
    /// #     }
    /// # }
    ///impl From<&Capabilities> for child::Capabilities {
    ///    fn from(incoming: &Capabilities) -> Self {
    ///        child::Capabilities {
    ///            some_capability: incoming.some_capability.map_event(Event::Submodule),
    ///            render: incoming.render.map_event(Event::Submodule),
    ///        }
    ///    }
    ///}
    /// # mod child {
    /// #     #[derive(Default)]
    /// #     struct App;
    /// #     pub struct Event;
    /// #     #[derive(crux_core::macros::Effect)]
    /// #     pub struct Capabilities {
    /// #         pub some_capability: crux_time::Time<Event>,
    /// #         pub render: crux_core::render::Render<Event>,
    /// #     }
    /// #     impl crux_core::App for App {
    /// #         type Event = Event;
    /// #         type Model = ();
    /// #         type ViewModel = ();
    /// #         type Capabilities = Capabilities;
    /// #         fn update(
    /// #             &self,
    /// #             _event: Self::Event,
    /// #             _model: &mut Self::Model,
    /// #             _caps: &Self::Capabilities,
    /// #         ) {
    /// #             unimplemented!()
    /// #         }
    /// #         fn view(&self, _model: &Self::Model) -> Self::ViewModel {
    /// #             unimplemented!()
    /// #         }
    /// #     }
    /// # }
    /// ```
    ///
    /// in the parent module's `update` function, you can then call `.into()` on the
    /// capabilities, before passing them down to the submodule.
    pub fn map_event<NewEv, F>(&self, func: F) -> CapabilityContext<Op, NewEv>
    where
        F: Fn(NewEv) -> Ev + Sync + Send + 'static,
        NewEv: 'static,
    {
        CapabilityContext::new(
            self.inner.shell_channel.clone(),
            self.inner.app_channel.map_input(func),
            self.inner.spawner.clone(),
        )
    }

    pub(crate) fn send_request(&self, request: Request<Op>) {
        self.inner.shell_channel.send(request);
    }
}

#[cfg(test)]
mod tests {
    use serde::Serialize;
    use static_assertions::assert_impl_all;

    use super::*;

    #[allow(dead_code)]
    enum Effect {}

    #[allow(dead_code)]
    enum Event {}

    #[derive(PartialEq, Serialize)]
    struct Op {}

    impl Operation for Op {
        type Output = ();
    }

    assert_impl_all!(ProtoContext<Effect, Event>: Send, Sync);
    assert_impl_all!(CapabilityContext<Op, Event>: Send, Sync);
}