addEventListener (callback)
Registers a listener to handle incoming messages. The listener receives two arguments: the message and an acknowledgment (ack
) function.
Syntax
import messageHandler from '@vcita/intandem-app-com';
// ...initialize the handler
messageHandler.addEventListener((message, ack) => { ... });
Parameters
-
message
MessageType:
The incoming message type.Available message types
Type Payload Description ChangeView team|staff Send the state of the view for either team or staff
-
callback
Function:result
String | Object :ack
(Function):
A function to acknowledge receipt of the message. Can optionally include:- Success or failure status (
boolean
). - Additional data, such as status messages or payloads (
Object | String
).
- Success or failure status (
Usage
Here’s how to add a listener and use the ack
function:
Example: Basic Success Acknowledgment
messageHandler.addEventListener(message:MessageType, (message, ack) => {
console.log('Message received', message);
ack(); // Acknowledge success with no additional data
});
Example: Success with Additional Information
messageHandler.addEventListener(message:MessageType, (message, ack) => {
console.log('Processing message:', message);
ack(true, '3 records added'); // Acknowledge success with a status message
});
Example: Success with Payload
messageHandler.addEventListener(message:MessageType, (message, ack) => {
ack(true, { Uid: 123 }); // Acknowledge success with additional payload
});
Example: Failure with Error Message
messageHandler.addEventListener(message:MessageType (message, ack) => {
ack(false, 'Unauthorized'); // Acknowledge failure with a reason
});
Example: Failure with Detailed Error Object
messageHandler.addEventListener(message:MessageType (message, ack) => {
ack(false, { errorCode: 404 }); // Acknowledge failure with an error object
});
ack
Function
ack
FunctionSignature:
ack(status?: boolean, data?: string | object);
Parameters:
-
status
(Optional,boolean
):true
: Indicates success.false
: Indicates failure.
-
data
(Optional,string | object | number
):
Additional data to provide context for the acknowledgment. Could be a string message or an object containing more detailed information.
Notes
- If
ack
is not called, the message will not be acknowledged, which may cause retries or other behavior depending on the implementation. - Use meaningful messages or payloads in the acknowledgment to help downstream systems handle the response effectively.
Updated 21 days ago