fix(tauri) event system init on windows fixed (#29)

This commit is contained in:
Lucas Fernandes Nogueira 2019-08-23 11:39:41 -03:00 committed by nothingismagick
parent 68565b899c
commit 157a3ac40e
4 changed files with 46 additions and 39 deletions

View File

@ -9,6 +9,46 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
Err(_) => false,
Ok(command) => {
match command {
Init {} => {
webview
.handle()
.dispatch(move |_webview| {
_webview
.eval(&format!(
"window['{queue}'] = [];
window['{fn}'] = function (payload, salt, ignoreQueue) {{
window.tauri.promisified({{
cmd: 'validateSalt',
salt: salt
}}).then(function () {{
const listeners = (window['{listeners}'] && window['{listeners}'][payload.type]) || []
if (!ignoreQueue && listeners.length === 0) {{
window['{queue}'].push({{
payload: payload,
salt: salt
}})
}}
for (let i = listeners.length - 1; i >= 0; i--) {{
const listener = listeners[i]
if (listener.once)
listeners.splice(i, 1)
listener.handler(payload)
}}
}})
}}",
fn = crate::event::emit_function_name(),
listeners = crate::event::event_listeners_object_name(),
queue = crate::event::event_queue_object_name()
))
.unwrap();
Ok(())
})
.unwrap();
},
#[cfg(any(feature = "all-api", feature = "readTextFile"))]
ReadTextFile {
path,

View File

@ -1,6 +1,7 @@
#[derive(Deserialize)]
#[serde(tag = "cmd", rename_all = "camelCase")]
pub enum Cmd {
Init {},
#[cfg(any(feature = "all-api", feature = "readTextFile"))]
ReadTextFile {
path: String,

View File

@ -106,44 +106,6 @@ pub(crate) fn run(application: &mut crate::App) {
.build()
.unwrap();
webview
.handle()
.dispatch(move |_webview| {
_webview
.eval(&format!(
"window['{queue}'] = [];
window['{fn}'] = function (payload, salt, ignoreQueue) {{
window.tauri.promisified({{
cmd: 'validateSalt',
salt
}}).then(function () {{
const listeners = (window['{listeners}'] && window['{listeners}'][payload.type]) || []
if (!ignoreQueue && listeners.length === 0) {{
window['{queue}'].push({{
payload: payload,
salt: salt
}})
}}
for (let i = listeners.length - 1; i >= 0; i--) {{
const listener = listeners[i]
if (listener.once)
listeners.splice(i, 1)
listener.handler(payload)
}}
}})
}}",
fn = crate::event::emit_function_name(),
listeners = crate::event::event_listeners_object_name(),
queue = crate::event::event_queue_object_name()
))
.unwrap();
Ok(())
})
.unwrap();
#[cfg(not(feature = "dev"))]
{
#[cfg(feature = "embedded-server")]

View File

@ -7,7 +7,7 @@
**/
// open <a href="..."> links with the Tauri API
document.querySelector('body').addEventListener('click', e => {
document.querySelector('body').addEventListener('click', function (e) {
let target = e.target
while (target != null) {
if (target.matches ? target.matches('a') : target.msMatchesSelector('a')) {
@ -18,6 +18,10 @@ document.querySelector('body').addEventListener('click', e => {
}
}, true)
document.addEventListener('DOMContentLoaded', function () {
tauri.invoke({ cmd: 'init' })
})
/**
* @module tauri
* @description This API interface makes powerful interactions available