Represents a uloop task communication pipe instance, passed as sole argument to the task function by task().
*
const task = uloop.task((pipe) => {
…
pipe.send();
…
pipe.receive();
…
}, …);
- Source
- See
Methods
receive() → {*}nullable
Reads input from the task handle.
This method reads input from the task communication pipe. The input callback function registered with the task handle is invoked to return the input data, which is then serialized, sent over the pipe, and deserialized by the receive method.
Returns the deserialized message read from the task communication pipe. Returns null
on error, such as when there's no input callback registered on the task handle.
// Read input from the task communication pipe
const message = pipe.receive();
if (message !== null)
printf("Received message: %s\n", message);
else
die(`Error receiving message: ${uloop.error()}\n`);
- Source
receiving() → {boolean}
Checks if the task handle reads output.
This method checks if the task handle has an output callback registered. It returns a boolean value indicating whether an output callback is present.
Returns true
if the task handle has an output callback registered, otherwise returns false
.
// Check if the task handle has an output callback
const hasOutputCallback = pipe.receiving();
if (hasOutputCallback)
printf("Output callback is registered on task handle\n");
else
printf("No output callback on the task handle\n");
- Source
send(msg) → {boolean}nullable
Sends a serialized message to the task handle.
This method serializes the provided message and sends it over the task communication pipe. In the main thread, the message is deserialized and passed as an argument to the output callback function registered with the task handle.
Name | Type | Description |
---|---|---|
msg | * | The message to be serialized and sent over the pipe. It can be of arbitrary type. |
Returns true
on success, indicating that the message was successfully sent over the pipe. Returns null
on error, such as when there's no output callback registered with the task handle.
// Send a message over the uloop pipe
const success = pipe.send(message);
if (success)
printf("Message sent successfully\n");
else
die(`Error sending message: ${uloop.error()}\n`);
- Source
sending() → {boolean}
Checks if the task handle provides input.
This method checks if the task handle has an input callback registered. It returns a boolean value indicating whether an input callback is present.
Returns true
if the remote task handle has an input callback registered, otherwise returns false
.
// Check if the remote task handle has an input callback
const hasInputCallback = pipe.sending();
if (hasInputCallback)
printf("Input callback is registered on task handle\n");
else
printf("No input callback on the task handle\n");
- Source