pub struct CallbackRef<S, C>where
S: ThreadSafety,
C: CallbackSignature,{ /* private fields */ }
Expand description
Vdom
A callback reference linked to a CallbackRegistration
.
Implementations§
Source§impl<S: ThreadSafety, C> CallbackRef<S, C>where
C: CallbackSignature,
impl<S: ThreadSafety, C> CallbackRef<S, C>where
C: CallbackSignature,
Sourcepub fn deanonymize(self) -> Self
👎Deprecated: Call of .deanonymize()
on named type.
pub fn deanonymize(self) -> Self
.deanonymize()
on named type.When called on an opaque type, deanonymizes it into the underlying named type.
Both AutoSafe
and Deanonymize
must be in scope and the method must be called without qualification for this to work.
Calling this method on a named type returns the value and type unchanged and produces a deprecation warning.
Source§impl<C> CallbackRef<ThreadSafe, C>where
C: CallbackSignature,
impl<C> CallbackRef<ThreadSafe, C>where
C: CallbackSignature,
Sourcepub fn prefer_thread_safe(self) -> Self
pub fn prefer_thread_safe(self) -> Self
Gently nudges the compiler to choose the ThreadSafe
version of a value if both are possible.
This method is by value, so it will resolve with higher priority than the by-reference method on the ThreadBound
type.
Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
Sourcepub fn prefer_thread_safe_ref(&self) -> &Self
pub fn prefer_thread_safe_ref(&self) -> &Self
Gently nudges the compiler to choose the ThreadSafe
version of a reference if both are possible.
This method is once by single reference, so it will resolve with higher priority than the twice-by-reference method on the ThreadBound
type.
Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
Source§impl<C> CallbackRef<ThreadBound, C>where
C: CallbackSignature,
impl<C> CallbackRef<ThreadBound, C>where
C: CallbackSignature,
Sourcepub fn prefer_thread_safe(&self) -> Self
pub fn prefer_thread_safe(&self) -> Self
Gently nudges the compiler to choose the ThreadSafe
version of a value if both are is possible.
This method is by reference, so it will resolve with lower priority than the by-value method on the ThreadSafe
type.
Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
Sourcepub fn prefer_thread_safe_ref<'b>(self: &&'b Self) -> &'b Self
pub fn prefer_thread_safe_ref<'b>(self: &&'b Self) -> &'b Self
Gently nudges the compiler to choose the ThreadSafe
version of a reference if both are is possible.
This method is twice by reference, so it will resolve with lower priority than the once-by-reference method on the ThreadSafe
type.
Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
Source§impl<S> CallbackRef<S, fn(event: Event)>where
S: ThreadSafety,
Separate impl
s due to Rust language limitation. See CallbackSignature
and expect future broadening.
impl<S> CallbackRef<S, fn(event: Event)>where
S: ThreadSafety,
Separate impl
s due to Rust language limitation. See CallbackSignature
and expect future broadening.
Sourcepub fn call(self, parameter: Event)
pub fn call(self, parameter: Event)
Invokes the stored handler with the stored receiver and parameter
,
provided that the original CallbackRegistration
hasn’t been dropped yet.
Source§impl<S, T> CallbackRef<S, fn(dom_ref: DomRef<&T>)>where
S: ThreadSafety,
Separate impl
s due to Rust language limitation. See CallbackSignature
and expect future broadening.
impl<S, T> CallbackRef<S, fn(dom_ref: DomRef<&T>)>where
S: ThreadSafety,
Separate impl
s due to Rust language limitation. See CallbackSignature
and expect future broadening.
Sourcepub fn call(self, parameter: DomRef<&T>)
pub fn call(self, parameter: DomRef<&T>)
Invokes the stored handler with the stored receiver and parameter
,
provided that the original CallbackRegistration
hasn’t been dropped yet.
Source§impl<S, C> CallbackRef<S, C>
These functions are intended as storage optimization for in-browser renderers.
impl<S, C> CallbackRef<S, C>
These functions are intended as storage optimization for in-browser renderers.
The CallbackRef
’s raw numerical value can be passed through JavaScript directly,
rather than adding another layer of indirection.
Only available with the "callbacks"
feature.
Most DOM renderers will still require an event listener table in order to unsubscribe from events.
§Example
use js_sys::Function;
use lignin::{CallbackRef, ThreadBound, web};
use wasm_bindgen::{closure::Closure, JsCast, JsValue, UnwrapThrowExt};
let element: web_sys::Element = // …
let callback_ref: CallbackRef<ThreadBound, fn(web::Event)> = // …
let common_handler = Closure::<dyn Fn(JsValue, web_sys::Event)>::wrap(
Box::new(|callback_ref: JsValue, event: web_sys::Event| {
unsafe { CallbackRef::<ThreadBound, fn(web::Event)>::from_js(&callback_ref) }
.expect_throw("Invalid `CallbackRef`.")
.call(event.into());
})
);
let listener = common_handler.as_ref().unchecked_ref::<Function>()
.bind1(&JsValue::UNDEFINED, &callback_ref.into_js());
// `common_handler` must be either leaked or stored somewhere,
// since otherwise it will throw when called from JavaScript.
let result = element.add_event_listener_with_callback("click", &listener);
Sourcepub fn into_js(self) -> JsValue
pub fn into_js(self) -> JsValue
Returns this CallbackRef
’s identity as JsValue
,
which can then for example be bound
to a generic event handler shim.
§Implementation Contract
The return value of this function must be treated as opaque handle.
Sourcepub unsafe fn from_js(key: &JsValue) -> Option<Self>
pub unsafe fn from_js(key: &JsValue) -> Option<Self>
Reconstructs a CallbackRef
that was previously converted into a JsValue
.
§Safety
key
must have been retrieved verbatim from [.into_js
],S
must be compatible (i.e. the same orThreadBound
),- if the original
S
wasThreadBound
, theCallbackRef
must be reconstructed on the same thread and C
must be the same except for lifetime changes that would be okay in an assignment.