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
//! Embedding APIs offering finer-grained control over execution of the runtime.

use crate::{cached_node::CachedNode, interfaces::node::Child};
use futures::{
    future::LocalFutureObj,
    task::{LocalSpawn, SpawnError},
};
use moxie::runtime::RunLoop;

/// Wrapper around `moxie::runtime::RunLoop` and a root function which returns a
/// DOM node. After each call to `run_once` the node returned from the root
/// function is re-attached to the provided parent, removing other children.
#[must_use]
pub struct DomLoop {
    inner: RunLoop<Box<dyn FnMut()>>,
}

impl DomLoop {
    /// Construct a new `DomLoop` which will maintain the children of the
    /// provided `parent`.
    ///
    /// On its own, a `WebRuntime` is inert and must either have its `run_once`
    /// method called when a re-render is needed, or be scheduled with
    /// [`DomLoop::animation_frame_scheduler`].
    pub fn new<Root: Child + 'static>(
        parent: impl Into<augdom::Node>,
        mut root: impl (FnMut() -> Root) + 'static,
    ) -> Self {
        let parent = parent.into();

        let mut inner = RunLoop::new(Box::new(move || {
            let parent = CachedNode::new(parent.clone());
            let new_root = topo::call(|| root());

            parent.ensure_child_attached(new_root.to_bind());
            parent.remove_trailing_children();
        }) as Box<dyn FnMut()>);

        #[cfg(feature = "webdom")]
        {
            inner.set_task_executor(WebSpawner);
        }

        Self { inner }
    }

    /// Run the root function in a fresh `moxie::Revision` and bind the returned
    /// node as the child of the loop's parent.
    pub fn run_once(&mut self) {
        self.inner.run_once();
    }
}

#[cfg(feature = "rsdom")]
impl DomLoop {
    /// Construct a new `DomLoop` which will maintain the children of a virtual
    /// `<div>`.
    ///
    /// Internally calls [`DomLoop::new`].
    #[cfg(feature = "rsdom")]
    pub fn new_virtual<Root: Child + 'static>(
        parent: impl Into<augdom::Node>,
        root: impl (FnMut() -> Root) + 'static,
    ) -> Self {
        Self::new(parent, augdom::in_virtual_document(root))
    }
}

#[cfg(feature = "webdom")]
impl DomLoop {
    /// Pass ownership of this runtime to a "loop" which runs with
    /// `requestAnimationFrame`.
    pub fn animation_frame_scheduler(self) -> raf::AnimationFrameScheduler<Self> {
        raf::AnimationFrameScheduler::new(self)
    }
}

#[cfg(feature = "webdom")]
impl raf::Tick for DomLoop {
    fn tick(&mut self) {
        self.inner.run_once();
    }
}

#[cfg(feature = "webdom")]
impl raf::Waking for DomLoop {
    fn set_waker(&mut self, wk: std::task::Waker) {
        self.inner.set_state_change_waker(wk);
    }
}

#[cfg(feature = "webdom")]
struct WebSpawner;

#[cfg(feature = "webdom")]
impl LocalSpawn for WebSpawner {
    fn spawn_local_obj(&self, fut: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
        wasm_bindgen_futures::spawn_local(fut);
        Ok(())
    }
}