blitz_script/
runtime_driver.rs1use std::sync::Arc;
7use std::task::Context;
8
9use blitz_dom::{BaseDocument, DocGuard, DocGuardMut, Document, DocumentConfig, NodeId};
10use blitz_traits::events::{BlitzPointerEvent, DomEvent, UiEvent};
11
12use crate::ScriptDocumentBackend;
13use crate::diagnostics::DiagnosticBatch;
14use crate::native_document::{BoundScriptDocument, DocumentPollHook, NativeDocumentRuntime};
15use crate::native_host::NativeHost;
16
17pub struct RuntimeDriver<G, H>
19where
20 G: NativeDocumentRuntime,
21 H: NativeHost<Guest = G> + 'static,
22{
23 document: BoundScriptDocument<G, H>,
24}
25
26impl<G, H> RuntimeDriver<G, H>
27where
28 G: NativeDocumentRuntime,
29 H: NativeHost<Guest = G> + 'static,
30{
31 pub fn new_bound(html: Arc<str>, config: DocumentConfig, host: H) -> Result<Self, String> {
33 BoundScriptDocument::new_bound(html, config, host).and_then(Self::from_document)
34 }
35
36 pub fn from_document(document: BoundScriptDocument<G, H>) -> Result<Self, String> {
38 if document.guest().is_none() {
39 return Err(String::from("a runtime driver requires a live bound guest"));
40 }
41 Ok(Self { document })
42 }
43
44 pub fn document(&self) -> &BoundScriptDocument<G, H> {
46 &self.document
47 }
48
49 pub fn document_mut(&mut self) -> &mut BoundScriptDocument<G, H> {
51 &mut self.document
52 }
53
54 pub fn into_document(self) -> BoundScriptDocument<G, H> {
56 self.document
57 }
58
59 pub fn execute_scripts(&mut self) -> Result<(), String> {
61 self.document.execute_scripts()
62 }
63
64 pub fn eval(&mut self, source: &str) -> Result<(), String> {
66 self.document.eval(source)
67 }
68
69 pub fn eval_named(&mut self, source: &str, name: &str) -> Result<(), String> {
71 self.document.eval_named(source, name)
72 }
73
74 pub fn eval_json(&mut self, source: &str) -> Result<serde_json::Value, String> {
76 self.document.eval_json(source)
77 }
78
79 pub fn has_pending_jobs(&self) -> bool {
81 self.document.has_pending_jobs()
82 }
83
84 pub fn diagnostic_batch(
86 &self,
87 console_after: u64,
88 errors_after: u64,
89 limit: usize,
90 ) -> Result<DiagnosticBatch, String> {
91 self.document
92 .diagnostic_batch(console_after, errors_after, limit)
93 }
94
95 pub fn dispatch_node_event(&mut self, node: NodeId, name: &str) -> Result<bool, String> {
97 self.document.dispatch_node_event(node, name)
98 }
99
100 pub fn guest(&self) -> Option<&G> {
102 self.document.guest()
103 }
104
105 pub fn guest_mut(&mut self) -> Option<&mut G> {
107 self.document.guest_mut()
108 }
109
110 pub fn with_guest_mut<R>(
112 &mut self,
113 callback: impl FnOnce(&mut G, &mut BaseDocument) -> R,
114 ) -> Result<R, String> {
115 self.document.with_guest_mut(callback)
116 }
117
118 pub fn pending_task_count(&self) -> u64 {
119 self.document.pending_task_count()
120 }
121
122 pub fn dispatch_dom_event(&mut self, event: DomEvent) -> Result<(), String> {
123 self.document.dispatch_dom_event(event)
124 }
125
126 pub fn handle_pointer_move_to_node(
127 &mut self,
128 event: BlitzPointerEvent,
129 node: NodeId,
130 ) -> Result<(), String> {
131 self.document.handle_pointer_move_to_node(event, node)
132 }
133
134 pub fn handle_ui_event_to_node(&mut self, event: UiEvent, node: NodeId) -> Result<(), String> {
135 self.document.handle_ui_event_to_node(event, node)
136 }
137
138 pub fn set_ipc_handler(&mut self, handler: Box<dyn Fn(String)>) -> Result<(), String> {
139 self.document.set_ipc_handler(handler)
140 }
141
142 pub fn add_poll_hook(&mut self, hook: DocumentPollHook) -> Result<(), String> {
143 self.document.add_poll_hook(hook)
144 }
145}
146
147impl<G, H> Document for RuntimeDriver<G, H>
148where
149 G: NativeDocumentRuntime,
150 H: NativeHost<Guest = G> + 'static,
151{
152 fn inner(&self) -> DocGuard<'_> {
153 self.document.inner()
154 }
155
156 fn inner_mut(&mut self) -> DocGuardMut<'_> {
157 self.document.inner_mut()
158 }
159
160 fn handle_ui_event(&mut self, event: UiEvent) {
161 self.document.handle_ui_event(event);
162 }
163
164 fn poll(&mut self, context: Option<Context>) -> bool {
165 self.document.poll(context)
166 }
167
168 fn poll_for_settle(&mut self, context: Option<Context>, run_timers: bool) -> bool {
169 self.document.poll_for_settle(context, run_timers)
170 }
171
172 fn prepare_teardown(&mut self) {
173 self.document.prepare_teardown();
174 }
175
176 fn dispose(self: Box<Self>) -> Arc<blitz_dom::release::Teardown> {
177 let Self { document } = *self;
178 Box::new(document).dispose()
179 }
180
181 fn set_page_visible(&mut self, visible: bool) {
182 self.document.set_page_visible(visible);
183 }
184
185 fn freeze(&mut self) {
186 self.document.freeze();
187 }
188
189 fn finish_restore(&mut self) {
190 self.document.finish_restore();
191 }
192
193 fn resume(&mut self) {
194 self.document.resume();
195 }
196}