use std::sync::Arc;
use std::task::Context;
use blitz_dom::{BaseDocument, DocGuard, DocGuardMut, Document, DocumentConfig, NodeId};
use blitz_traits::events::{BlitzPointerEvent, DomEvent, UiEvent};
use crate::ScriptDocumentBackend;
use crate::diagnostics::DiagnosticBatch;
use crate::native_document::{BoundScriptDocument, DocumentPollHook, NativeDocumentRuntime};
use crate::native_host::NativeHost;
pub struct RuntimeDriver<G, H>
where
G: NativeDocumentRuntime,
H: NativeHost<Guest = G> + 'static,
{
document: BoundScriptDocument<G, H>,
}
impl<G, H> RuntimeDriver<G, H>
where
G: NativeDocumentRuntime,
H: NativeHost<Guest = G> + 'static,
{
pub fn new_bound(html: Arc<str>, config: DocumentConfig, host: H) -> Result<Self, String> {
BoundScriptDocument::new_bound(html, config, host).and_then(Self::from_document)
}
pub fn from_document(document: BoundScriptDocument<G, H>) -> Result<Self, String> {
if document.guest().is_none() {
return Err(String::from("a runtime driver requires a live bound guest"));
}
Ok(Self { document })
}
pub fn document(&self) -> &BoundScriptDocument<G, H> {
&self.document
}
pub fn document_mut(&mut self) -> &mut BoundScriptDocument<G, H> {
&mut self.document
}
pub fn into_document(self) -> BoundScriptDocument<G, H> {
self.document
}
pub fn execute_scripts(&mut self) -> Result<(), String> {
self.document.execute_scripts()
}
pub fn eval(&mut self, source: &str) -> Result<(), String> {
self.document.eval(source)
}
pub fn eval_named(&mut self, source: &str, name: &str) -> Result<(), String> {
self.document.eval_named(source, name)
}
pub fn eval_json(&mut self, source: &str) -> Result<serde_json::Value, String> {
self.document.eval_json(source)
}
pub fn has_pending_jobs(&self) -> bool {
self.document.has_pending_jobs()
}
pub fn diagnostic_batch(
&self,
console_after: u64,
errors_after: u64,
limit: usize,
) -> Result<DiagnosticBatch, String> {
self.document
.diagnostic_batch(console_after, errors_after, limit)
}
pub fn dispatch_node_event(&mut self, node: NodeId, name: &str) -> Result<bool, String> {
self.document.dispatch_node_event(node, name)
}
pub fn guest(&self) -> Option<&G> {
self.document.guest()
}
pub fn guest_mut(&mut self) -> Option<&mut G> {
self.document.guest_mut()
}
pub fn with_guest_mut<R>(
&mut self,
callback: impl FnOnce(&mut G, &mut BaseDocument) -> R,
) -> Result<R, String> {
self.document.with_guest_mut(callback)
}
pub fn pending_task_count(&self) -> u64 {
self.document.pending_task_count()
}
pub fn dispatch_dom_event(&mut self, event: DomEvent) -> Result<(), String> {
self.document.dispatch_dom_event(event)
}
pub fn handle_pointer_move_to_node(
&mut self,
event: BlitzPointerEvent,
node: NodeId,
) -> Result<(), String> {
self.document.handle_pointer_move_to_node(event, node)
}
pub fn handle_ui_event_to_node(&mut self, event: UiEvent, node: NodeId) -> Result<(), String> {
self.document.handle_ui_event_to_node(event, node)
}
pub fn set_ipc_handler(&mut self, handler: Box<dyn Fn(String)>) -> Result<(), String> {
self.document.set_ipc_handler(handler)
}
pub fn add_poll_hook(&mut self, hook: DocumentPollHook) -> Result<(), String> {
self.document.add_poll_hook(hook)
}
}
impl<G, H> Document for RuntimeDriver<G, H>
where
G: NativeDocumentRuntime,
H: NativeHost<Guest = G> + 'static,
{
fn inner(&self) -> DocGuard<'_> {
self.document.inner()
}
fn inner_mut(&mut self) -> DocGuardMut<'_> {
self.document.inner_mut()
}
fn handle_ui_event(&mut self, event: UiEvent) {
self.document.handle_ui_event(event);
}
fn poll(&mut self, context: Option<Context>) -> bool {
self.document.poll(context)
}
fn poll_for_settle(&mut self, context: Option<Context>, run_timers: bool) -> bool {
self.document.poll_for_settle(context, run_timers)
}
fn prepare_teardown(&mut self) {
self.document.prepare_teardown();
}
fn dispose(self: Box<Self>) -> Arc<blitz_dom::release::Teardown> {
let Self { document } = *self;
Box::new(document).dispose()
}
fn set_page_visible(&mut self, visible: bool) {
self.document.set_page_visible(visible);
}
fn freeze(&mut self) {
self.document.freeze();
}
fn finish_restore(&mut self) {
self.document.finish_restore();
}
fn resume(&mut self) {
self.document.resume();
}
}