Skip to main content

comlink_sys/
lib.rs

1//! Raw bindings to the [Comlink](https://github.com/GoogleChromeLabs/comlink)
2//! JavaScript library, covering its whole public surface and nothing more. The
3//! ergonomic layer lives in the [`comlink`](https://docs.rs/comlink) crate.
4//!
5//! Every item here is imported from the bare `comlink` module specifier, so the
6//! npm package of that name has to be resolvable by whatever bundler or module
7//! loader serves the generated JavaScript.
8//!
9//! Comlink gives several parameters JavaScript default values, which
10//! `wasm_bindgen` cannot express. Each of those is bound once per arity, named
11//! after the parameters it passes, following the `web-sys` convention.
12
13use wasm_bindgen::prelude::*;
14
15#[wasm_bindgen(module = "comlink")]
16extern "C" {
17    /// Serves calls on the global scope, dispatching them onto `obj`.
18    pub fn expose(obj: &JsValue);
19
20    /// Serves calls on `endpoint`, dispatching them onto `obj`.
21    #[wasm_bindgen(js_name = expose)]
22    pub fn expose_with_endpoint(obj: &JsValue, endpoint: &JsValue);
23
24    /// Serves calls on `endpoint`, ignoring messages whose origin matches none
25    /// of `allowed_origins` (an array of strings and regular expressions).
26    #[wasm_bindgen(js_name = expose)]
27    pub fn expose_with_endpoint_and_allowed_origins(
28        obj: &JsValue,
29        endpoint: &JsValue,
30        allowed_origins: &js_sys::Array,
31    );
32
33    /// Returns the object exposed on `endpoint` as a JavaScript `Proxy`.
34    pub fn wrap(endpoint: &JsValue) -> JsValue;
35
36    /// Returns a proxy for `endpoint` whose calls are applied to `target`.
37    #[wasm_bindgen(js_name = wrap)]
38    pub fn wrap_with_target(endpoint: &JsValue, target: &JsValue) -> JsValue;
39
40    /// Marks `transfers` as the transferables of the next post of `value`, and
41    /// returns `value` unchanged.
42    pub fn transfer(value: &JsValue, transfers: &js_sys::Array) -> JsValue;
43
44    /// Marks `obj` to cross as a `MessagePort`-backed proxy rather than being
45    /// structurally cloned.
46    pub fn proxy(obj: &JsValue) -> JsValue;
47
48    /// Wraps a window-like object into an endpoint that posts to it and listens
49    /// on the global scope.
50    #[wasm_bindgen(js_name = windowEndpoint)]
51    pub fn window_endpoint(window: &JsValue) -> JsValue;
52
53    /// Wraps a window-like object into an endpoint listening on `context`.
54    #[wasm_bindgen(js_name = windowEndpoint)]
55    pub fn window_endpoint_with_context(window: &JsValue, context: &JsValue) -> JsValue;
56
57    /// Wraps a window-like object into an endpoint listening on `context` and
58    /// posting with `target_origin`.
59    #[wasm_bindgen(js_name = windowEndpoint)]
60    pub fn window_endpoint_with_context_and_target_origin(
61        window: &JsValue,
62        context: &JsValue,
63        target_origin: &str,
64    ) -> JsValue;
65
66    /// The property marking an object as proxied rather than cloned, which is
67    /// what [`proxy`] sets.
68    #[wasm_bindgen(thread_local_v2, js_name = proxyMarker)]
69    pub static PROXY_MARKER: js_sys::Symbol;
70
71    /// The method on a proxy returning a new endpoint for the same object.
72    #[wasm_bindgen(thread_local_v2, js_name = createEndpoint)]
73    pub static CREATE_ENDPOINT: js_sys::Symbol;
74
75    /// The method on a proxy releasing the remote object.
76    #[wasm_bindgen(thread_local_v2, js_name = releaseProxy)]
77    pub static RELEASE_PROXY: js_sys::Symbol;
78
79    /// The property on an exposed object naming the method to call once the
80    /// last proxy for it is released.
81    #[wasm_bindgen(thread_local_v2, js_name = finalizer)]
82    pub static FINALIZER: js_sys::Symbol;
83
84    /// The name-keyed map of handlers serializing values comlink cannot clone.
85    #[wasm_bindgen(thread_local_v2, js_name = transferHandlers)]
86    pub static TRANSFER_HANDLERS: js_sys::Map;
87}