constructor()

in packages/react-composites/src/composites/CallWithChatComposite/adapter/AzureCommunicationCallWithChatAdapter.ts [206:257]


  constructor(callAdapter: CallAdapter, chatAdapter?: ChatAdapter) {
    this.bindPublicMethods();
    this.callAdapter = callAdapter;
    this.context = new CallWithChatContext(callWithChatAdapterStateFromBackingStates(callAdapter));

    const onChatStateChange = (newChatAdapterState: ChatAdapterState): void => {
      this.context.updateClientStateWithChatState(newChatAdapterState);
    };

    this.onChatStateChange = onChatStateChange;
    if (chatAdapter) {
      this.updateChatAdapter(chatAdapter);
      this.originCallChatAdapter = chatAdapter;
    }

    const onCallStateChange = (newCallAdapterState: CallAdapterState): void => {
      this.context.updateClientStateWithCallState(newCallAdapterState);
    };

    this.callAdapter.onStateChange(onCallStateChange);
    this.callAdapter.on('breakoutRoomsUpdated', async (eventData: BreakoutRoomsEventData) => {
      if (eventData.type === 'join') {
        await this.breakoutRoomJoined(eventData.data);
      } else if (eventData.type === 'assignedBreakoutRooms') {
        if (!eventData.data || eventData.data.state === 'closed') {
          if (
            this.originCallChatAdapter &&
            this.originCallChatAdapter?.getState().thread.threadId !== this.context.getState().chat?.threadId
          ) {
            this.updateChatAdapter(this.originCallChatAdapter);
          }
        }
      }
    });
    this.callAdapter.on('callEnded', () => {
      // If the call ended is a breakout room call with breakout room settings then update the chat adapter to the
      // origin call
      if (this.context.getState().call?.breakoutRooms?.breakoutRoomSettings) {
        // Unsubscribe from chat adapter state changes
        this.chatAdapter?.offStateChange(this.onChatStateChange);
        // Unassign chat adapter
        this.chatAdapter = undefined;
        // Set chat state to undefined to ensure that the chat thread of the breakout room is not shown
        this.context.unsetChatState();
        // Update chat state to the origin call chat adapter
        if (this.originCallChatAdapter) {
          this.updateChatAdapter(this.originCallChatAdapter);
        }
      }
    });
    this.onCallStateChange = onCallStateChange;
  }