From 22c5a4ad4cce55cf3fbae4f215a5216edc24aa1b Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 21 Oct 2022 11:32:27 -0300 Subject: [PATCH] fix(core): update metadata before window-created listeners, closes #5191 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. --- .changes/fix-metadata-update.md | 5 +++++ core/tauri/src/event.rs | 31 ++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 .changes/fix-metadata-update.md diff --git a/.changes/fix-metadata-update.md b/.changes/fix-metadata-update.md new file mode 100644 index 00000000000..d21507a03a7 --- /dev/null +++ b/.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. diff --git a/core/tauri/src/event.rs b/core/tauri/src/event.rs index 50f4a3df221..d19fed996eb 100644 --- a/core/tauri/src/event.rs +++ b/core/tauri/src/event.rs @@ -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,