Struct neon::event::Channel [−][src]
pub struct Channel { /* fields omitted */ }
napi-4
and task-api
only.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);
let this = cx.undefined();
let null = cx.null();
let args = vec![
cx.null().upcast::<JsValue>(),
cx.number(result).upcast(),
];
callback.call(&mut cx, this, args)?;
Ok(())
});
});
Ok(cx.undefined())
}
Implementations
Creates an unbounded channel for scheduling closures on the JavaScript main thread
Allow the Node event loop to exit while this Channel
exists.
Idempotent
Prevent the Node event loop from exiting while this Channel
exists. (Default)
Idempotent
pub fn send<T, F>(&self, f: F) -> JoinHandle<T> where
T: Send + 'static,
F: FnOnce(TaskContext<'_>) -> NeonResult<T> + Send + 'static,
pub fn send<T, F>(&self, f: F) -> JoinHandle<T> where
T: Send + 'static,
F: FnOnce(TaskContext<'_>) -> NeonResult<T> + Send + 'static,
Schedules a closure to execute on the JavaScript thread that created this Channel Panics if there is a libuv error
pub fn try_send<T, F>(&self, f: F) -> Result<JoinHandle<T>, SendError> where
T: Send + 'static,
F: FnOnce(TaskContext<'_>) -> NeonResult<T> + Send + 'static,
pub fn try_send<T, F>(&self, f: F) -> Result<JoinHandle<T>, SendError> where
T: Send + 'static,
F: FnOnce(TaskContext<'_>) -> 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.
Returns a boolean indicating if this Channel
will prevent the Node event
loop from exiting.
pub fn settle_with<V, F>(
&self,
deferred: Deferred,
complete: F
) -> JoinHandle<()> where
V: Value,
for<'a> F: FnOnce(&mut TaskContext<'a>) -> JsResult<'a, V> + Send + 'static,
This is supported on crate feature promise-api
only.
pub fn settle_with<V, F>(
&self,
deferred: Deferred,
complete: F
) -> JoinHandle<()> where
V: Value,
for<'a> F: FnOnce(&mut TaskContext<'a>) -> JsResult<'a, V> + Send + 'static,
promise-api
only.Settle a JsPromise
from Deferred
by sending a
closure to be executed on the main JavaScript thread.
The JsPromise
will be resolved with the value returned by the complete
closure. If an exception is thrown, the promise will be rejected with the exception.
Panics if there is a libuv error.
let channel = cx.channel();
let (deferred, promise) = cx.promise();
channel.settle_with(deferred, move |cx| Ok(cx.number(42)));
pub fn try_settle_with<V, F>(
&self,
deferred: Deferred,
complete: F
) -> Result<JoinHandle<()>, SendError> where
V: Value,
for<'a> F: FnOnce(&mut TaskContext<'a>) -> JsResult<'a, V> + Send + 'static,
This is supported on crate feature promise-api
only.
pub fn try_settle_with<V, F>(
&self,
deferred: Deferred,
complete: F
) -> Result<JoinHandle<()>, SendError> where
V: Value,
for<'a> F: FnOnce(&mut TaskContext<'a>) -> JsResult<'a, V> + Send + 'static,
promise-api
only.Settle a JsPromise
from Deferred
by
sending a closure to be executed on the main JavaScript thread.
Usage is identical to Channel::settle_with
.
Returns a SendError
if sending the closure to the main JavaScript thread fails.
See Channel::try_send
and SendError
for more details.
Trait Implementations
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.
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl RefUnwindSafe for Channel
impl UnwindSafe for Channel
Blanket Implementations
Mutably borrows from an owned value. Read more