docs: runtime error if setting the same event listener 2x rather than silent failure (#2383)

Delegated event listeners do not support adding more than one event listener of the same type. This can cause confusion if two listeners are added, as one is silently dropped.
This commit is contained in:
Giovanni 2024-03-07 21:49:23 +00:00 committed by GitHub
parent 943a992570
commit 2b4f5e0f58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -70,6 +70,12 @@ pub fn add_event_listener<E>(
let cb = Closure::wrap(cb as Box<dyn FnMut(E)>).into_js_value();
let key = intern(&key);
debug_assert_eq!(
Ok(false),
js_sys::Reflect::has(target, &JsValue::from_str(&key)),
"Error while adding {key} event listener, a listener of type {key} \
already present."
);
_ = js_sys::Reflect::set(target, &JsValue::from_str(&key), &cb);
add_delegated_event_listener(&key, event_name, options);
}