fix(tauri) run setup hook after init has been eval'd (#243)

* fix(tauri) run setup hook after init has been eval'd

* fix(endpoints) merge conflict
This commit is contained in:
Lucas Fernandes Nogueira
2019-12-29 09:21:11 -03:00
committed by GitHub
parent 548ab94810
commit 3fbb297064
2 changed files with 11 additions and 12 deletions

View File

@@ -73,8 +73,6 @@ pub(crate) fn run(application: &mut crate::App) {
});
}
let mut ran_setup = false;
let webview = web_view::builder()
.title(&config.tauri.window.title)
.size(config.tauri.window.width, config.tauri.window.height)
@@ -82,13 +80,15 @@ pub(crate) fn run(application: &mut crate::App) {
.debug(debug)
.user_data(())
.invoke_handler(|webview, arg| {
if !crate::endpoints::handle(webview, arg) {
application.run_invoke_handler(webview, arg);
}
// the first command is always the `init`, so we can safely run the setup hook here
if !ran_setup {
ran_setup = true;
if arg == r#"{"cmd":"__initialized"}"# {
application.run_setup(webview);
webview.eval("
if (window.onTauriInit !== void 0) {
window.onTauriInit()
}
").expect("failed to evaluate window.onTauriInit");
} else if !crate::endpoints::handle(webview, arg) {
application.run_invoke_handler(webview, arg);
}
Ok(())

View File

@@ -12,7 +12,7 @@ pub(crate) fn handle<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> boo
Init {} => {
webview
.eval(&format!(
"window['{queue}'] = [];
r#"window['{queue}'] = [];
window['{fn}'] = function (payload, salt, ignoreQueue) {{
const listeners = (window['{listeners}'] && window['{listeners}'][payload.type]) || []
if (!ignoreQueue && listeners.length === 0) {{
@@ -37,9 +37,8 @@ pub(crate) fn handle<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> boo
}}
}}
if (window.onTauriInit !== void 0) {{
window.onTauriInit()
}}",
window.external.invoke('{{"cmd":"__initialized"}}')
"#,
fn = crate::event::emit_function_name(),
listeners = crate::event::event_listeners_object_name(),
queue = crate::event::event_queue_object_name()