Skip to content

Commit 88a0e30

Browse files
committed
docs: Update docs for async event handlers
1 parent 5b37c2f commit 88a0e30

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

docs/api/events.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ Events emitted by the chatbot provide the following **details** accessible via `
5151
| currPath | `string` | Represents the current path of the user. |
5252
| prevPath | `string` | Represents the previous path of the user. |
5353

54-
In addition, all events also contain **event-specific data** that are accessible via `event.data`. You may manipulate the data directly which will be reflected in the result of the event (e.g. modifying the content of a `RcbPreMessageInjectEvent` will modify the message sent).
54+
In addition, chatbot events are also initialized with **an empty promises array**, accessible via `event.promises`. This caters for use cases where event handlers are **async and require promises to be resolved before proceeding** with the chatbot logic. Push promises into `event.promises` to preserve the chatbot flow logic.
55+
56+
Lastly, all events also contain **event-specific data** that are accessible via `event.data`. You may manipulate the data directly which will be reflected in the result of the event (e.g. modifying the content of a `RcbPreMessageInjectEvent` will modify the message sent). For an example on how to use events, you may take a look at the [**custom events**](/docs/examples/custom_events) example.
5557

5658
Below is a detailed description of each event and how to use them.
5759

docs/api/hooks.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ The `useAudio` hook allows you to track and manage the chatbot's audio functiona
112112
| Name | Type | Description |
113113
| -------------- | ---------- | ------------------------------------------------------ |
114114
| audioToggledOn | `boolean ` | Indicates if the chatbot audio is currently on or off. |
115-
| toggleAudio | `function` | Toggles the chatbot audio on or off. |
115+
| toggleAudio | `async function` | Toggles the chatbot audio on or off. |
116116

117117
#### Code Example
118118
```jsx
@@ -184,7 +184,7 @@ The `useChatWindow` hook allows you to track and manage the chatbot's window sta
184184
| Name | Type | Description |
185185
| ------------------- | ---------- | -------------------------------------------------------- |
186186
| isChatWindowOpen | `boolean` | Indicates if the chat window is currently open or close. |
187-
| toggleChatWindow | `function` | Toggles the chat window open or close. |
187+
| toggleChatWindow | `async function` | Toggles the chat window open or close. |
188188

189189
#### Code Example
190190
```jsx
@@ -300,7 +300,7 @@ The `useNotifications` hook allows you to track and manage the chatbot's notific
300300
| Name | Type | Description |
301301
| ---------------------- | ---------- | -------------------------------------------------------------- |
302302
| notificationsToggledOn | `boolean ` | Indicates if the chatbot notifications is currently on or off. |
303-
| toggleNotifications | `function` | Toggles the chatbot notifications on or off. |
303+
| toggleNotifications | `async function` | Toggles the chatbot notifications on or off. |
304304
| playNotificationSound | `function` | Plays the notification sound. |
305305

306306
#### Code Example
@@ -426,7 +426,7 @@ The `useTextArea` hook allows you to track and manage the chatbot's text area fi
426426
| textAreaSensitiveMode | `boolean` | Indicates if the text area is in sensitive mode. |
427427
| toggleTextAreaSensitiveMode | `function` | Toggles the text area sensitive mode. |
428428
| getTextAreaValue | `function` | Retrieves the string value inside the text area. |
429-
| setTextAreaValue | `function` | Sets the value inside the text area, identical to `params.setTextAreaValue` detailed [**here**](/docs/api/params#settextareavalue). |
429+
| setTextAreaValue | `async function` | Sets the value inside the text area, identical to `params.setTextAreaValue` detailed [**here**](/docs/api/params#settextareavalue). |
430430
| focusTextArea | `function` | Focuses on the text area. |
431431

432432
#### Code Example
@@ -456,8 +456,8 @@ The `useToasts` hook allows you to track and manage the chatbot's toasts.
456456
#### Return Values
457457
| Name | Type | Description |
458458
| ---------------------------- | ----------- | -------------------------------------------------------- |
459-
| showToast | `function` | Shows a toast in chat, identical to `params.showToast` detailed [**here**](/docs/api/params#showtoast). |
460-
| dismissToast | `function` | Dismisses a toast from chat, identical to `params.dismissToast` detailed [**here**](/docs/api/params#dismisstoast). |
459+
| showToast | `async function` | Shows a toast in chat, identical to `params.showToast` detailed [**here**](/docs/api/params#showtoast). |
460+
| dismissToast | `async function` | Dismisses a toast from chat, identical to `params.dismissToast` detailed [**here**](/docs/api/params#dismisstoast). |
461461
| toasts | `Array<Toast>` | Array containing all toasts currently shown in the chatbot |
462462
| replaceToasts | `function` | Directly replaces the current toasts with provided toasts. |
463463

@@ -489,7 +489,7 @@ The `useVoice` hook allows you to track and manage the chatbot's voice functiona
489489
| Name | Type | Description |
490490
| -------------- | ---------- | ------------------------------------------------------ |
491491
| voiceToggledOn | `boolean ` | Indicates if the chatbot voice is currently on or off. |
492-
| toggleVoice | `function` | Toggles the chatbot voice on or off. |
492+
| toggleVoice | `async function` | Toggles the chatbot voice on or off. |
493493

494494
#### Code Example
495495
```jsx

docs/api/params.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ The following table provides details about the parameters available for attribut
1818
| userInput | `string` | All Attributes | Represents the user's input in the chat. |
1919
| currPath | `string` | All Attributes | Represents the current path in the chat (can be null if conversation flow has not started). |
2020
| prevPath | `string` | All Attributes | Represents the previous path in the chat (can be null if no previous path exists). |
21-
| goToPath | `function` | All Attributes | A utility function for navigating to another block. |
21+
| goToPath | `async function` | All Attributes | A utility function for navigating to another block. |
2222
| injectMessage | `async function` | All Attributes | A utility function to inject a message into the chat. |
2323
| streamMessage | `async function` | All Attributes | Streams a message into the chat. You can refer to the [**Real-Time Streaming**](/docs/examples/real_time_stream) example. |
2424
| endStreamMessage | `async function` | All Attributes | Ends an existing message stream. You can refer to the [**Real-Time Streaming**](/docs/examples/real_time_stream) example. |
2525
| removeMessage | `async function` | All Attributes | Removes a message from the chat by message id. |
26-
| setTextAreaValue| `function` | All Attributes | Sets a value directly within the text area. |
27-
| showToast | `function` | All Attributes | Shows a toast that is dismissed after a duration or on user click. |
28-
| dismissToast | `function` | All Attributes | Dismisses a toast by toast id. |
29-
| openChat | `function` | All Attributes | Opens or closes the chat component. |
26+
| setTextAreaValue| `async function` | All Attributes | Sets a value directly within the text area. |
27+
| showToast | `async function` | All Attributes | Shows a toast that is dismissed after a duration or on user click. |
28+
| dismissToast | `async function` | All Attributes | Dismisses a toast by toast id. |
29+
| openChat | `async function` | All Attributes | Opens or closes the chat component. |
3030
| files | `FileList` | Only `file` Attribute | Represents the files uploaded by the user. |
3131

3232
:::caution Caution
3333

34-
If you are using `params.injectMessage`, `params.streamMessage`, `params.endStreamMessage` or `params.removeMessage`, do remember that they are `async` and that without using `await`, behavior may be unexpected (e.g. multiple messages sent at once). This is a common pitfall!
34+
If you are using functions from `params`, do remember that they are `async` and that without using `await`, behaviors may be unexpected (e.g. multiple messages sent at once). This is a common pitfall!
3535

3636
:::
3737

@@ -216,16 +216,16 @@ start: {
216216
Sets a value inside the input text area.
217217

218218
#### Type
219-
`function`
219+
`async function`
220220

221221
#### Parameters
222222
- `value` (required): a `string` value to set inside the input text area.
223223

224224
#### Code Example
225225
```jsx
226226
start: {
227-
message: (params) => {
228-
params.setTextAreaValue("This is the new input text area value!");
227+
message: async (params) => {
228+
await params.setTextAreaValue("This is the new input text area value!");
229229
}
230230
}
231231
```
@@ -236,7 +236,7 @@ start: {
236236
Shows a toast that lasts for a duration (if specified).
237237

238238
#### Type
239-
`function`
239+
`async function`
240240

241241
#### Parameters
242242
- `content` (required): a `string` value representing the toast content to show.
@@ -245,8 +245,8 @@ Shows a toast that lasts for a duration (if specified).
245245
#### Code Example
246246
```jsx
247247
start: {
248-
message: (params) => {
249-
params.showToast("Hello there, I last for 3 seconds!", 3000);
248+
message: async (params) => {
249+
await params.showToast("Hello there, I last for 3 seconds!", 3000);
250250
}
251251
}
252252
```
@@ -257,16 +257,16 @@ start: {
257257
Dismisses a toast with the given toast id.
258258

259259
#### Type
260-
`function`
260+
`async function`
261261

262262
#### Parameters
263263
- `id` (required): a `string` value representing the id of the toast to remove.
264264

265265
#### Code Example
266266
```jsx
267267
start: {
268-
message: (params) => {
269-
params.dismissToast("fb8bf309-2a33-4683-955c-9e915768dadf");
268+
message: async (params) => {
269+
await params.dismissToast("fb8bf309-2a33-4683-955c-9e915768dadf");
270270
}
271271
}
272272
```
@@ -277,16 +277,16 @@ start: {
277277
Sets the chat window to be open or close.
278278

279279
#### Type
280-
`function`
280+
`async function`
281281

282282
#### Parameters
283283
- `isOpen` (required): a `boolean` indicating if chat window should be set open.
284284

285285
#### Code Example
286286
```jsx
287287
start: {
288-
message: (params) => {
289-
params.openChat(true);
288+
message: async (params) => {
289+
await params.openChat(true);
290290
}
291291
}
292292
```
@@ -305,8 +305,8 @@ List of files uploaded by the user (only present in the [**file attribute**](/do
305305
#### Code Example
306306
```jsx
307307
start: {
308-
message: (params) => {
309-
params.openChat(true);
308+
file: (params) => {
309+
console.log(params.files);
310310
}
311311
}
312312
```

docs/examples/custom_events.md

+14
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,29 @@ const MyChatBot = () => {
2626
};
2727

2828
React.useEffect(() => {
29+
// synchronous function
2930
const handleUserSubmitText = (event: RcbUserSubmitTextEvent) => {
3031
if (event.data.inputText.toLowerCase().includes("hello")) {
3132
event.preventDefault();
3233
}
3334
};
3435

36+
// asynchronous function
37+
const handleToggleNotifications = async (event: RcbToggleNotificationsEvent) => {
38+
// simulates an async call with a 1 second delay
39+
const mockApiCall = new Promise((resolve) => setTimeout(() => {
40+
resolve("Notifications toggled successfully!");
41+
}, 1000));
42+
43+
// collect promises for eventual resolution before chatbot logic proceeds
44+
event.promises.push(mockApiCall);
45+
}
46+
3547
window.addEventListener("rcb-user-submit-text", handleUserSubmitText);
48+
window.addEventListener("rcb-toggle-notifications", handleToggleNotifications);
3649
return () => {
3750
window.removeEventListener("rcb-user-submit-text", handleUserSubmitText);
51+
window.removeEventListener("rcb-toggle-notifications", handleToggleNotifications);
3852
};
3953
}, []);
4054

0 commit comments

Comments
 (0)