Skip to content

Commit e00b1e5

Browse files
authored
fix(core): update metadata before window-created listeners, closes #5191 (#5458)
1 parent 161d5be commit e00b1e5

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

.changes/fix-metadata-update.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Fixes access to the `WebviewWindow.getByLabel` function in a `tauri://window-created` event listener.

core/tauri/src/event.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,26 @@ pub fn listen_js(
327327
handler: String,
328328
) -> String {
329329
format!(
330-
"if (window['{listeners}'] === void 0) {{
331-
Object.defineProperty(window, '{listeners}', {{ value: Object.create(null) }});
332-
}}
333-
if (window['{listeners}'][{event}] === void 0) {{
334-
Object.defineProperty(window['{listeners}'], {event}, {{ value: [] }});
335-
}}
336-
window['{listeners}'][{event}].push({{
337-
id: {event_id},
338-
windowLabel: {window_label},
339-
handler: {handler}
340-
}});
330+
"
331+
(function () {{
332+
if (window['{listeners}'] === void 0) {{
333+
Object.defineProperty(window, '{listeners}', {{ value: Object.create(null) }});
334+
}}
335+
if (window['{listeners}'][{event}] === void 0) {{
336+
Object.defineProperty(window['{listeners}'], {event}, {{ value: [] }});
337+
}}
338+
const eventListeners = window['{listeners}'][{event}]
339+
const listener = {{
340+
id: {event_id},
341+
windowLabel: {window_label},
342+
handler: {handler}
343+
}};
344+
if ({event} == 'tauri://window-created') {{
345+
eventListeners.splice(eventListeners.length - 1, 0, listener)
346+
}} else {{
347+
eventListeners.push(listener);
348+
}}
349+
}})()
341350
",
342351
listeners = listeners_object_name,
343352
event = event,

0 commit comments

Comments
 (0)