neon::event

Struct Channel

Source
pub struct Channel { /* private fields */ }
Expand description

Channel for scheduling Rust closures to execute on the JavaScript main thread.

Cloning a Channel will create a new channel that shares a backing queue for events.

§Example

The following example spawns a standard Rust thread to complete a computation and calls back to a JavaScript function asynchronously with the result.

fn async_fibonacci(mut cx: FunctionContext) -> JsResult<JsUndefined> {
    // These types (`f64`, `Root<JsFunction>`, `Channel`) may all be sent
    // across threads.
    let n = cx.argument::<JsNumber>(0)?.value(&mut cx);
    let callback = cx.argument::<JsFunction>(1)?.root(&mut cx);
    let channel = cx.channel();

    // Spawn a thread to complete the execution. This will _not_ block the
    // JavaScript event loop.
    std::thread::spawn(move || {
        let result = fibonacci(n);

        // Send a closure as a task to be executed by the JavaScript event
        // loop. This _will_ block the event loop while executing.
        channel.send(move |mut cx| {
            let callback = callback.into_inner(&mut cx);

            callback
                .bind(&mut cx)
                .args(((), result))?
                .exec()?;

            Ok(())
        });
    });

    Ok(cx.undefined())
}

Implementations§

Source§

impl Channel

Source

pub fn new<'a, C: Context<'a>>(cx: &mut C) -> Self

Creates an unbounded channel for scheduling closures on the JavaScript main thread

Source

pub fn unref<'a, C: Context<'a>>(&mut self, cx: &mut C) -> &mut Self

Allow the Node event loop to exit while this Channel exists. Idempotent

Source

pub fn reference<'a, C: Context<'a>>(&mut self, cx: &mut C) -> &mut Self

Prevent the Node event loop from exiting while this Channel exists. (Default) Idempotent

Source

pub fn send<T, F>(&self, f: F) -> JoinHandle<T>
where T: Send + 'static, F: FnOnce(Cx<'_>) -> NeonResult<T> + Send + 'static,

Schedules a closure to execute on the JavaScript thread that created this Channel Panics if there is a libuv error

Source

pub fn try_send<T, F>(&self, f: F) -> Result<JoinHandle<T>, SendError>
where T: Send + 'static, F: FnOnce(Cx<'_>) -> NeonResult<T> + Send + 'static,

Schedules a closure to execute on the JavaScript thread that created this Channel Returns an Error if the task could not be scheduled.

See SendError for additional details on failure causes.

Source

pub fn has_ref(&self) -> bool

Returns a boolean indicating if this Channel will prevent the Node event loop from exiting.

Trait Implementations§

Source§

impl Clone for Channel

Source§

fn clone(&self) -> Self

Returns a clone of the Channel instance that shares the internal unbounded queue with the original channel. Scheduling callbacks on the same queue is faster than using separate channels, but might lead to starvation if one of the threads posts significantly more callbacks on the channel than the other one.

Cloned and referenced Channel instances might trigger additional event-loop tick when dropped. Channel can be wrapped into an Arc and shared between different threads/callers to avoid this.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Channel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Channel

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.