|
2 | 2 | // Licensed under the MIT license.
|
3 | 3 | /// <reference lib="esnext.asynciterable" />
|
4 | 4 |
|
5 |
| -import { logger } from "./models/logger"; |
6 |
| -import { EventEmitter } from "events"; |
7 |
| -import { CommunicationTokenCredential } from "@azure/communication-common"; |
8 |
| -import { getSignalingClient } from "./signaling/signalingClient"; |
9 |
| -import { PagedAsyncIterableIterator } from "@azure/core-paging"; |
10 |
| -import { SpanStatusCode } from "@azure/core-tracing"; |
11 |
| -import { createSpan } from "./tracing"; |
12 |
| -import { ChatThreadClient } from "./chatThreadClient"; |
13 | 5 | import {
|
14 | 6 | ChatClientOptions,
|
15 | 7 | CreateChatThreadOptions,
|
16 |
| - ListChatThreadsOptions, |
17 |
| - DeleteChatThreadOptions |
| 8 | + DeleteChatThreadOptions, |
| 9 | + ListChatThreadsOptions |
18 | 10 | } from "./models/options";
|
| 11 | +import { |
| 12 | + ChatEventId, |
| 13 | + ChatMessageDeletedEvent, |
| 14 | + ChatMessageEditedEvent, |
| 15 | + ChatMessageReceivedEvent, |
| 16 | + ChatThreadCreatedEvent, |
| 17 | + ChatThreadDeletedEvent, |
| 18 | + ChatThreadPropertiesUpdatedEvent, |
| 19 | + ParticipantsAddedEvent, |
| 20 | + ParticipantsRemovedEvent, |
| 21 | + ReadReceiptReceivedEvent, |
| 22 | + TypingIndicatorReceivedEvent |
| 23 | +} from "./models/events"; |
| 24 | +import { ChatThreadItem, CreateChatThreadResult, ListPageSettings } from "./models/models"; |
| 25 | +import { ConnectionState, SignalingClient } from "@azure/communication-signaling"; |
19 | 26 | import {
|
20 | 27 | mapToChatParticipantRestModel,
|
21 | 28 | mapToCreateChatThreadOptionsRestModel,
|
22 | 29 | mapToCreateChatThreadResultSdkModel
|
23 | 30 | } from "./models/mappers";
|
24 |
| -import { ChatThreadItem, CreateChatThreadResult, ListPageSettings } from "./models/models"; |
25 |
| -import { InternalPipelineOptions } from "@azure/core-rest-pipeline"; |
| 31 | + |
26 | 32 | import { ChatApiClient } from "./generated/src";
|
| 33 | +import { ChatThreadClient } from "./chatThreadClient"; |
| 34 | +import { CommunicationTokenCredential } from "@azure/communication-common"; |
27 | 35 | import { CreateChatThreadRequest } from "./models/requests";
|
| 36 | +import { EventEmitter } from "events"; |
| 37 | +import { InternalPipelineOptions } from "@azure/core-rest-pipeline"; |
| 38 | +import { PagedAsyncIterableIterator } from "@azure/core-paging"; |
| 39 | +import { SpanStatusCode } from "@azure/core-tracing"; |
28 | 40 | import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
|
| 41 | +import { createSpan } from "./tracing"; |
29 | 42 | import { generateUuid } from "./models/uuid";
|
30 |
| -import { SignalingClient } from "@azure/communication-signaling"; |
31 |
| -import { |
32 |
| - ChatEventId, |
33 |
| - ChatMessageReceivedEvent, |
34 |
| - ChatMessageEditedEvent, |
35 |
| - ChatMessageDeletedEvent, |
36 |
| - ReadReceiptReceivedEvent, |
37 |
| - TypingIndicatorReceivedEvent, |
38 |
| - ChatThreadCreatedEvent, |
39 |
| - ChatThreadDeletedEvent, |
40 |
| - ChatThreadPropertiesUpdatedEvent, |
41 |
| - ParticipantsAddedEvent, |
42 |
| - ParticipantsRemovedEvent |
43 |
| -} from "./models/events"; |
| 43 | +import { getSignalingClient } from "./signaling/signalingClient"; |
| 44 | +import { logger } from "./models/logger"; |
44 | 45 |
|
45 | 46 | /**
|
46 | 47 | * The client to do chat operations
|
@@ -341,12 +342,29 @@ export class ChatClient {
|
341 | 342 | */
|
342 | 343 | public on(event: "participantsRemoved", listener: (e: ParticipantsRemovedEvent) => void): void;
|
343 | 344 |
|
344 |
| - public on(event: ChatEventId, listener: (e: any) => void): void { |
| 345 | + /** |
| 346 | + * Subscribe function for realTimeNotificationConnected. |
| 347 | + * @param event - The realTimeNotificationConnected Event |
| 348 | + * @param listener - The listener to handle the event. |
| 349 | + */ |
| 350 | + public on(event: "realTimeNotificationConnected", listener: () => void): void; |
| 351 | + |
| 352 | + /** |
| 353 | + * Subscribe function for realTimeNotificationDisconnected. |
| 354 | + * @param event - The realTimeNotificationDisconnected Event |
| 355 | + * @param listener - The listener to handle the event. |
| 356 | + */ |
| 357 | + public on(event: "realTimeNotificationDisconnected", listener: () => void): void; |
| 358 | + |
| 359 | + public on(event: ChatEventId, listener: (e?: any) => void): void { |
345 | 360 | if (this.signalingClient === undefined) {
|
346 | 361 | throw new Error("Realtime notifications are only supported in the browser.");
|
347 | 362 | }
|
348 |
| - |
349 |
| - if (!this.isRealtimeNotificationsStarted) { |
| 363 | + if ( |
| 364 | + !this.isRealtimeNotificationsStarted && |
| 365 | + event !== "realTimeNotificationConnected" && |
| 366 | + event !== "realTimeNotificationDisconnected" |
| 367 | + ) { |
350 | 368 | throw new Error(
|
351 | 369 | "You must call startRealtimeNotifications before you can subscribe to events."
|
352 | 370 | );
|
@@ -444,6 +462,14 @@ export class ChatClient {
|
444 | 462 | throw new Error("Realtime notifications are only supported in the browser.");
|
445 | 463 | }
|
446 | 464 |
|
| 465 | + this.signalingClient.on("connectionChanged", (payload) => { |
| 466 | + if (payload === ConnectionState.Connected) { |
| 467 | + this.emitter.emit("realTimeNotificationConnected"); |
| 468 | + } else if (payload === ConnectionState.Disconnected) { |
| 469 | + this.emitter.emit("realTimeNotificationDisconnected"); |
| 470 | + } |
| 471 | + }); |
| 472 | + |
447 | 473 | this.signalingClient.on("chatMessageReceived", (payload) => {
|
448 | 474 | this.emitter.emit("chatMessageReceived", payload);
|
449 | 475 | });
|
|
0 commit comments