fix(core): regression on the unlisten function (#3623)

This commit is contained in:
Lucas Fernandes Nogueira 2022-03-06 20:45:38 -03:00 committed by GitHub
parent 66fe5d81bc
commit 76c791bd2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 30 deletions

6
.changes/unlisten-fix.md Normal file
View File

@ -0,0 +1,6 @@
---
"tauri": patch
"api": patch
---
Fixes a regression on the `unlisten` command.

File diff suppressed because one or more lines are too long

View File

@ -63,7 +63,7 @@ pub enum Cmd {
},
/// Unlisten to an event.
#[serde(rename_all = "camelCase")]
Unlisten { event_id: u64 },
Unlisten { event: EventId, event_id: u64 },
/// Emit an event to the webview associated with the given window.
/// If the window_label is omitted, the event will be triggered on all listeners.
#[serde(rename_all = "camelCase")]
@ -103,11 +103,16 @@ impl Cmd {
Ok(event_id)
}
fn unlisten<R: Runtime>(context: InvokeContext<R>, event_id: u64) -> super::Result<()> {
fn unlisten<R: Runtime>(
context: InvokeContext<R>,
event: EventId,
event_id: u64,
) -> super::Result<()> {
context
.window
.eval(&unlisten_js(
context.window.manager().event_listeners_object_name(),
event.0,
event_id,
))
.map_err(crate::error::into_anyhow)?;

View File

@ -300,17 +300,21 @@ mod test {
}
}
pub fn unlisten_js(listeners_object_name: String, event_id: u64) -> String {
pub fn unlisten_js(listeners_object_name: String, event_name: String, event_id: u64) -> String {
format!(
"
for (var event in (window['{listeners}'] || {{}})) {{
var listeners = (window['{listeners}'] || {{}})[event]
(function () {{
const listeners = (window['{listeners}'] || {{}})['{event_name}']
if (listeners) {{
window['{listeners}'][event] = window['{listeners}'][event].filter(function (e) {{ return e.id !== {event_id} }})
const index = window['{listeners}']['{event_name}'].findIndex(e => e.id === {event_id})
if (index > -1) {{
window['{listeners}']['{event_name}'].splice(index, 1)
}}
}}
}}
}})()
",
listeners = listeners_object_name,
event_name = event_name,
event_id = event_id,
)
}

File diff suppressed because one or more lines are too long

View File

@ -41,17 +41,19 @@ export type EventCallback<T> = (event: Event<T>) => void
export type UnlistenFn = () => void
/**
* Unregister the event listener associated with the given id.
* Unregister the event listener associated with the given name and id.
*
* @ignore
* @param event The event name
* @param eventId Event identifier
* @returns
*/
async function _unlisten(eventId: number): Promise<void> {
async function _unlisten(event: string, eventId: number): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Event',
message: {
cmd: 'unlisten',
event,
eventId
}
})
@ -102,7 +104,7 @@ async function listen<T>(
handler: transformCallback(handler)
}
}).then((eventId) => {
return async () => _unlisten(eventId)
return async () => _unlisten(event, eventId)
})
}
@ -120,7 +122,7 @@ async function once<T>(
): Promise<UnlistenFn> {
return listen<T>(event, windowLabel, (eventData) => {
handler(eventData)
_unlisten(eventData.id).catch(() => {})
_unlisten(event, eventData.id).catch(() => {})
})
}