Skip to content

Commit

Permalink
fix(core): update metadata before window-created listeners, closes #5191
Browse files Browse the repository at this point in the history


Tauri fires event listeners in reverse order, so our own tauri://window-created listener is the last to execute. This commit changes the listen JS code to keep our own listener as the last one in the array, so the metadata is updated before user code is executed.
  • Loading branch information
lucasfernog committed Oct 21, 2022
1 parent 161d5be commit 22c5a4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-metadata-update.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Fixes access to the `WebviewWindow.getByLabel` function in a `tauri://window-created` event listener.
31 changes: 20 additions & 11 deletions core/tauri/src/event.rs
Expand Up @@ -327,17 +327,26 @@ pub fn listen_js(
handler: String,
) -> String {
format!(
"if (window['{listeners}'] === void 0) {{
Object.defineProperty(window, '{listeners}', {{ value: Object.create(null) }});
}}
if (window['{listeners}'][{event}] === void 0) {{
Object.defineProperty(window['{listeners}'], {event}, {{ value: [] }});
}}
window['{listeners}'][{event}].push({{
id: {event_id},
windowLabel: {window_label},
handler: {handler}
}});
"
(function () {{
if (window['{listeners}'] === void 0) {{
Object.defineProperty(window, '{listeners}', {{ value: Object.create(null) }});
}}
if (window['{listeners}'][{event}] === void 0) {{
Object.defineProperty(window['{listeners}'], {event}, {{ value: [] }});
}}
const eventListeners = window['{listeners}'][{event}]
const listener = {{
id: {event_id},
windowLabel: {window_label},
handler: {handler}
}};
if ({event} == 'tauri://window-created') {{
eventListeners.splice(eventListeners.length - 1, 0, listener)
}} else {{
eventListeners.push(listener);
}}
}})()
",
listeners = listeners_object_name,
event = event,
Expand Down

0 comments on commit 22c5a4a

Please sign in to comment.