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
//! Logging [Middleware](crate::middleware::Middleware) for
//! applications running in the browser using `wasm-bindgen`.
//! Publishes actions/events that occur within the
//! [Store](crate::Store).

use super::{Middleware, ReduceMiddlewareResult};
use serde::Serialize;
use std::{fmt::Display, hash::Hash};
use wasm_bindgen::JsValue;
use web_sys::console;

pub enum LogLevel {
    Trace,
    Debug,
    Warn,
    Info,
    Log,
}

impl LogLevel {
    pub fn log_1(&self, message: &JsValue) {
        #[allow(unused_unsafe)]
        unsafe {
            match self {
                LogLevel::Trace => console::trace_1(message),
                LogLevel::Debug => console::debug_1(message),
                LogLevel::Warn => console::warn_1(message),
                LogLevel::Info => console::info_1(message),
                LogLevel::Log => console::log_1(message),
            }
        }
    }

    pub fn log(&self, messages: Vec<JsValue>) {
        let messages_array = js_sys::Array::new_with_length(messages.len() as u32);

        for (i, m) in messages.into_iter().enumerate() {
            messages_array.set(i as u32, m);
        }

        #[allow(unused_unsafe)]
        unsafe {
            match self {
                LogLevel::Trace => console::trace(&messages_array),
                LogLevel::Debug => console::debug(&messages_array),
                LogLevel::Warn => console::warn(&messages_array),
                LogLevel::Info => console::info(&messages_array),
                LogLevel::Log => console::log(&messages_array),
            }
        }
    }
}

pub enum DisplayType {
    /// Print using the browser's log groups. Unfortunately this isn't
    /// always very consistent, especially with
    /// asynchronous/concurrent events.
    Groups,
    /// Print the data in a single javascript object tree.
    SingleObject,
}

impl Default for DisplayType {
    fn default() -> Self {
        Self::Groups
    }
}

#[derive(Serialize)]
struct OnReduceLog<'a, State, Action, Effect> {
    action: &'a Option<Action>,
    prev_state: &'a State,
    next_state: &'a State,
    effects: &'a [Effect],
}

#[derive(Serialize)]
struct OnNotifyLog<'a, State, Event> {
    state: &'a State,
    events: &'a [Event],
}

impl Default for LogLevel {
    fn default() -> Self {
        LogLevel::Log
    }
}

/// Logging middleware for applications running in the browser.
///
/// See [web_logger](super::web_logger) for more details.
pub struct WebLoggerMiddleware {
    log_level: LogLevel,
    display_type: DisplayType,
}

impl WebLoggerMiddleware {
    pub fn new() -> Self {
        Self {
            log_level: LogLevel::default(),
            display_type: DisplayType::default(),
        }
    }

    /// Set the level at which the data from this middleware will be
    /// logged to.
    pub fn log_level(mut self, log_level: LogLevel) -> Self {
        self.log_level = log_level;
        self
    }

    /// What type of display to use when printing the data from this
    /// middleware.
    pub fn display_type(mut self, display_type: DisplayType) -> Self {
        self.display_type = display_type;
        self
    }

    fn on_reduce_groups<State, Action, Event, Effect>(
        &self,
        store: &crate::Store<State, Action, Event, Effect>,
        action: Option<&Action>,
        reduce: super::ReduceFn<State, Action, Event, Effect>,
    ) -> ReduceMiddlewareResult<Event, Effect>
    where
        State: Serialize,
        Action: Serialize + Display,
        Event: Clone + Hash + Eq + Serialize,
        Effect: Serialize,
    {
        let prev_state_js = JsValue::from_serde(&(*store.state())).unwrap();

        let action_js = JsValue::from_serde(&action).unwrap();
        let action_display = match &action {
            Some(action) => format!("{}", action),
            None => "None".to_string(),
        };

        let result = reduce(store, action);
        let next_state_js = JsValue::from_serde(&(*store.state())).unwrap();

        let effects_js = JsValue::from_serde(&result.effects).unwrap();
        let effects_display = match &result.effects.len() {
            0 => "None".to_string(),
            _ => format!("({})", result.effects.len()),
        };

        #[allow(unused_unsafe)]
        unsafe {
            console::group_collapsed_3(
                &JsValue::from_serde(&format!("%caction %c{}", action_display)).unwrap(),
                &JsValue::from_str("color: gray; font-weight: lighter;"),
                &JsValue::from_str("inherit"),
            );
            console::group_collapsed_2(
                &JsValue::from_str("%cprev state"),
                &JsValue::from_str("color: #9E9E9E; font-weight: bold;"),
            );
        }

        self.log_level.log_1(&prev_state_js);

        #[allow(unused_unsafe)]
        unsafe {
            console::group_end();

            console::group_collapsed_3(
                &JsValue::from_str(&format!("%caction: %c{}", action_display)),
                &JsValue::from_str("color: #03A9F4; font-weight: bold;"),
                &JsValue::from_str("color: gray; font-weight: lighter;"),
            );
        }

        self.log_level.log_1(&action_js);

        #[allow(unused_unsafe)]
        unsafe {
            console::group_end();

            console::group_collapsed_2(
                &JsValue::from_str("%cnext state"),
                &JsValue::from_str("color: #4CAF50; font-weight: bold;"),
            );
        }

        self.log_level.log_1(&next_state_js);

        #[allow(unused_unsafe)]
        unsafe {
            console::group_end();

            console::group_collapsed_3(
                &JsValue::from_str(&format!("%ceffects: %c{}", effects_display)),
                &JsValue::from_str("color: #C210C2; font-weight: bold;"),
                &JsValue::from_str("color: gray; font-weight: lighter;"),
            );
        }
        self.log_level.log_1(&effects_js);

        #[allow(unused_unsafe)]
        unsafe {
            console::group_end();
        }

        result
    }

    fn on_reduce_no_groups<State, Action, Event, Effect>(
        &self,
        store: &crate::Store<State, Action, Event, Effect>,
        action: Option<&Action>,
        reduce: super::ReduceFn<State, Action, Event, Effect>,
    ) -> ReduceMiddlewareResult<Event, Effect>
    where
        State: Serialize,
        Action: Serialize + Display,
        Event: Clone + Hash + Eq + Serialize,
        Effect: Serialize,
    {
        let action_display = format!(
            "on_reduce(), action: {}",
            match &action {
                Some(action) => format!("{}", action),
                None => "None".to_string(),
            }
        );

        let action_display_js = JsValue::from_str(&action_display);

        let prev_state = store.state();

        let result = reduce(store, action);
        let next_state = store.state();

        let log_object = OnReduceLog {
            action: &action,
            prev_state: &*prev_state,
            next_state: &*next_state,
            effects: &result.effects,
        };

        let log_object_js = JsValue::from_serde(&log_object).unwrap();
        self.log_level.log(vec![action_display_js, log_object_js]);

        result
    }

    fn on_notify_groups<State, Action, Event, Effect>(
        &self,
        store: &crate::Store<State, Action, Event, Effect>,
        events: Vec<Event>,
        notify: super::NotifyFn<State, Action, Event, Effect>,
    ) -> Vec<Event>
    where
        Event: Serialize,
    {
        let events_js = JsValue::from_serde(&events).unwrap();
        let events_display = match events.len() {
            0 => "None".to_string(),
            _ => format!("({})", events.len()),
        };

        #[allow(unused_unsafe)]
        unsafe {
            console::group_collapsed_3(
                &JsValue::from_str(&format!("%cevents: %c{}", events_display)),
                &JsValue::from_str("color: #FCBA03; font-weight: bold;"),
                &JsValue::from_str("color: gray; font-weight: lighter;"),
            );
        }

        self.log_level.log_1(&events_js);

        #[allow(unused_unsafe)]
        unsafe {
            console::group_end();
            console::group_end();
        }

        notify(store, events)
    }

    fn on_notify_no_groups<State, Action, Event, Effect>(
        &self,
        store: &crate::Store<State, Action, Event, Effect>,
        events: Vec<Event>,
        notify: super::NotifyFn<State, Action, Event, Effect>,
    ) -> Vec<Event>
    where
        Event: Serialize + Clone + Hash + Eq,
        State: Serialize,
    {
        let log_object = OnNotifyLog {
            state: &*store.state(),
            events: &events,
        };

        let log_object_js = JsValue::from_serde(&log_object).unwrap();

        let display = JsValue::from_str("on_notify(): ");

        self.log_level.log(vec![display, log_object_js]);

        notify(store, events)
    }
}

impl Default for WebLoggerMiddleware {
    fn default() -> Self {
        WebLoggerMiddleware::new()
    }
}

impl<State, Action, Event, Effect> Middleware<State, Action, Event, Effect> for WebLoggerMiddleware
where
    State: Serialize,
    Action: Serialize + Display,
    Event: Clone + Hash + Eq + Serialize,
    Effect: Serialize,
{
    fn on_reduce(
        &self,
        store: &crate::Store<State, Action, Event, Effect>,
        action: Option<&Action>,
        reduce: super::ReduceFn<State, Action, Event, Effect>,
    ) -> ReduceMiddlewareResult<Event, Effect> {
        match self.display_type {
            DisplayType::Groups => self.on_reduce_groups(store, action, reduce),
            DisplayType::SingleObject => self.on_reduce_no_groups(store, action, reduce),
        }
    }

    fn process_effect(
        &self,
        _store: &crate::Store<State, Action, Event, Effect>,
        effect: Effect,
    ) -> Option<Effect> {
        Some(effect)
    }

    fn on_notify(
        &self,
        store: &crate::Store<State, Action, Event, Effect>,
        events: Vec<Event>,
        notify: super::NotifyFn<State, Action, Event, Effect>,
    ) -> Vec<Event> {
        match self.display_type {
            DisplayType::Groups => self.on_notify_groups(store, events, notify),
            DisplayType::SingleObject => self.on_notify_no_groups(store, events, notify),
        }
    }
}