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
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2018, the Perspective Authors.
//
// This file is part of the Perspective library, distributed under the terms
// of the Apache License 2.0.  The full license can be found in the LICENSE
// file.

use std::cell::RefCell;
use std::collections::VecDeque;
use std::future::Future;
use std::rc::Rc;

use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::*;

use crate::utils::*;

#[derive(Default, Clone)]
pub struct MovingWindowRenderTimer(Rc<RefCell<RenderTimerType>>);

enum RenderTimerType {
    Moving(Closure<dyn Fn(JsValue)>, Rc<RefCell<Option<VecDeque<f64>>>>),
    Constant(f64),
}

impl Drop for RenderTimerType {
    fn drop(&mut self) {
        if let RenderTimerType::Moving(closure, _) = self {
            let document = window().unwrap().document().unwrap();
            document
                .remove_event_listener_with_callback(
                    "visibilitychange",
                    closure.as_ref().unchecked_ref(),
                )
                .unwrap();
        }
    }
}

impl Default for RenderTimerType {
    fn default() -> Self {
        let deque: Rc<RefCell<Option<VecDeque<f64>>>> = Default::default();
        Self::Moving(register_on_visibility_change(deque.clone()), deque)
    }
}

/// We need to clear the throttle queue when the browser tab is hidden, else
/// the next frame timing will be the time the tab was hidden + render time.
fn register_on_visibility_change(
    deque: Rc<RefCell<Option<VecDeque<f64>>>>,
) -> Closure<dyn Fn(JsValue)> {
    let fun = move |_| {
        *deque.borrow_mut() = None;
    };

    let closure = fun.into_closure();
    let document = window().unwrap().document().unwrap();
    document
        .add_event_listener_with_callback("visibilitychange", closure.as_ref().unchecked_ref())
        .unwrap();

    closure
}

impl MovingWindowRenderTimer {
    pub async fn capture_time<T>(&self, f: impl Future<Output = T>) -> T {
        let perf = window().unwrap().performance().unwrap();
        let start = match *self.0.borrow() {
            RenderTimerType::Constant(_) => 0_f64,
            RenderTimerType::Moving(..) => perf.now(),
        };

        let result = f.await;
        match &mut *self.0.borrow_mut() {
            RenderTimerType::Moving(_, timings) => {
                let mut timings = timings.borrow_mut();
                if let Some(timings) = &mut *timings {
                    timings.push_back(perf.now() - start);
                    if timings.len() > 5 {
                        timings.pop_front();
                    }
                } else {
                    *timings = Some(Default::default());
                }
            }
            RenderTimerType::Constant(_) => (),
        };

        result
    }

    pub fn set_throttle(&mut self, val: Option<f64>) {
        match val {
            None => {
                *self.0.borrow_mut() = RenderTimerType::default();
            }
            Some(val) => {
                *self.0.borrow_mut() = RenderTimerType::Constant(val);
            }
        }
    }

    pub fn get_avg(&self) -> i32 {
        match &*self.0.borrow() {
            RenderTimerType::Constant(constant) => *constant as i32,
            RenderTimerType::Moving(_, timings) => {
                if let Some(timings) = &*timings.borrow() {
                    let len = timings.len();
                    if len < 5 {
                        0_i32
                    } else {
                        let sum = timings.iter().sum::<f64>();
                        let avg: f64 = sum / len as f64;
                        f64::min(5000_f64, avg.floor()) as i32
                    }
                } else {
                    0_i32
                }
            }
        }
    }
}