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
pub mod use_fetch_collection_hook;
pub mod use_fetch_hook;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;

pub use use_fetch_collection_hook::{
    use_fetch_collection, IntoList, UseFetchCollection, UseFetchCollectionStatusTrait,
};
pub use use_fetch_hook::{use_fetch, UseFetch, UseFetchStatusTrait};

use comp_state::{topo, update_state_with_topo_id};

use seed::*;
use serde::de::{DeserializeOwned, Deserializer};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::str::FromStr;

// Code + docs: https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen_futures/

#[derive(Clone, Debug)]
pub enum UseFetchStatus {
    Initialized,
    Loading,
    Failed,
    Complete,
}

impl Default for UseFetchStatus {
    fn default() -> Self {
        UseFetchStatus::Initialized
    }
}

// Needs to be implemented if you want Seed to be fully informed about fetching
pub trait UseFetchMsgTrait {
    fn fetch_message(id: topo::Id, url: String, method: Method) -> Self;
    fn fetched_message(id: topo::Id, response: String) -> Self;
}

pub(crate) fn fetch_json<
    T: DeserializeOwned + 'static + Clone,
    J: Serialize + Clone + 'static,
    Ms: Default + Clone + 'static,
    Mdl: 'static,
>(
    id: topo::Id,
    url: String,
    json: J,
    method: Method,
) -> impl Future<Output = Result<Ms, Ms>> {
    seed::fetch::Request::new(url)
        .method(method)
        .send_json(&json)
        .fetch_string(move |f| {
            let data = f.response().unwrap().data;
            update_state_with_topo_id::<UseFetchCollection<T, J>, _>(id, |u| {
                u.status = UseFetchStatus::Complete;
                u.string_response = Some(data.clone());
                crate::schedule_update::<Ms, Mdl>(Ms::default());
            });
            Ms::default()
        })
}

// Some of the NextTick code is copied from seed source, this is becuase it is needed
// to ensure the fetch future is properly executed.

/// A future that becomes ready after a tick of the micro task queue.
// A future that becomes ready after a tick of the micro task queue.
pub struct NextTick {
    inner: JsFuture,
}

impl NextTick {
    /// Construct a new `NextTick` future.
    pub fn new() -> NextTick {
        // Create a resolved promise that will run its callbacks on the next
        // tick of the micro task queue.
        let promise = js_sys::Promise::resolve(&JsValue::NULL);
        // Convert the promise into a `JsFuture`.
        let inner = JsFuture::from(promise);
        NextTick { inner }
    }
}

impl Default for NextTick {
    fn default() -> Self {
        Self::new()
    }
}
impl Future for NextTick {
    type Output = ();

    fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<()> {
        // Polling a `NextTick` just forwards to polling if the inner promise is
        // ready.
        match Pin::new(&mut self.get_mut().inner).poll(ctx) {
            Poll::Ready(_) => Poll::Ready(()),
            Poll::Pending => Poll::Pending,
        }
    }
}

pub fn from_str<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
    T: FromStr,
    T::Err: Display,
    D: Deserializer<'de>,
{
    let s = String::deserialize(deserializer)?;
    T::from_str(&s).map_err(serde::de::Error::custom)
}

// pub(crate) fn fetch_string_with_seed_msg<Ms: UseFetchMsgTrait + 'static>(
//     id: topo::Id,
//     url: String,
//     method: Method,
// ) -> impl Future<Item = Ms, Error = Ms> {
//     seed::fetch::Request::new(url)
//         .method(method)
//         .fetch_string(move |f| Ms::fetched_message(id, f.response().unwrap().data))
// }

// pub(crate) fn update_fetch<Ms: UseFetchMsgTrait + 'static>(
//     orders: &mut impl Orders<Ms>,
//     id: topo::Id,
//     url: String,
//     method: Method,
// ) {
//     orders.perform_cmd(fetch_string_with_seed_msg::<Ms>(id, url, method));
// }

// pub(crate) fn update_fetched(id: topo::Id, string_response: String) {
//     update_state_with_topo_id::<UseFetch, _>(id, |u| {
//         u.status = UseFetchStatus::Complete;
//         u.string_response = Some(string_response.clone());
//     })
// }