Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalTab.cpp (C++)

Code before:
        const bool isBroadcasting = _tabStatus.IsInputBroadcastActive();
        _rootPane->EnableBroadcast(isBroadcasting);

        auto weakThis{ get_weak() };

        // When we change the state of broadcasting, add or remove event
        // handlers appropriately, so that controls won't be propagating events
        // needlessly if no one is listening.

        _rootPane->WalkTree([&](const auto& p) {
            const auto paneId = p->Id();
            if (!paneId.has_value())
            {
                return;
            }
            if (const auto& control{ p->GetTerminalControl() })
            {
                auto it = _controlEvents.find(*paneId);
                if (it != _controlEvents.end())
                {
                    auto& events = it->second;

                    if (isBroadcasting)
                    {
                        // ADD EVENT HANDLERS HERE
                        events.keySentToken = control.KeySent([weakThis](auto&& sender, auto&& e) {
                            if (const auto tab{ weakThis.get() })
                            {
                                if (tab->_tabStatus.IsInputBroadcastActive())
                                {
                                    if (const auto termControl{ sender.try_as<winrt::Microsoft::Terminal::Control::TermControl>() })
                                    {
                                        tab->_rootPane->BroadcastKey(termControl, e.VKey(), e.ScanCode(), e.Modifiers(), e.KeyDown());
                                    }
                                }
                            }
                        });

                        events.charSentToken = control.CharSent([weakThis](auto&& sender, auto&& e) {
                            if (const auto tab{ weakThis.get() })
                            {
                                if (tab->_tabStatus.IsInputBroadcastActive())
                                {
                                    if (const auto termControl{ sender.try_as<winrt::Microsoft::Terminal::Control::TermControl>() })
                                    {
                                        tab->_rootPane->BroadcastChar(termControl, e.Character(), e.ScanCode(), e.Modifiers());
                                    }
                                }
                            }
                        });


Review comment (refactor):
in theory if we use auto revokers, we could simplify the code to this:

```
events.keySentToken.revoke();
xxx.revoke();

if (newIsBroadcasting) {
    keySentToken = foo;
    xxx = bar;
}
```

and simplify the else case plus stomp the event handlers properly every time

Code after:
    // - Toggle broadcasting input to all the panes in this tab.
    void TerminalTab::ToggleBroadcastInput()
    {
        const bool newIsBroadcasting = !_tabStatus.IsInputBroadcastActive();
        _tabStatus.IsInputBroadcastActive(newIsBroadcasting);
        _rootPane->EnableBroadcast(newIsBroadcasting);

        auto weakThis{ get_weak() };

        // When we change the state of broadcasting, add or remove event
        // handlers appropriately, so that controls won't be propagating events
        // needlessly if no one is listening.

        _rootPane->WalkTree([&](const auto& p) {
            const auto paneId = p->Id();
            if (!paneId.has_value())
            {
                return;
            }
            if (const auto& control{ p->GetTerminalControl() })
            {
                auto it = _controlEvents.find(*paneId);
                if (it != _controlEvents.end())
                {
                    auto& events = it->second;

                    // Always clear out old ones, just in case.
                    events.KeySent.revoke();
                    events.CharSent.revoke();
                    events.StringSent.revoke();

                    if (newIsBroadcasting)
                    {
                        _addBroadcastHandlers(control, events);
                    }
                }
            }
        });
    }

    void TerminalTab::_addBroadcastHandlers(const TermControl& termControl, ControlEventTokens& events)
    {
        auto weakThis{ get_weak() };
        // ADD EVENT HANDLERS HERE
        events.KeySent = termControl.KeySent(winrt::auto_revoke, [weakThis](auto&& sender, auto&& e) {
            if (const auto tab{ weakThis.get() })
            {
                if (tab->_tabStatus.IsInputBroadcastActive())
                {
                    tab->_rootPane->BroadcastKey(sender.try_as<TermControl>(),
                                                 e.VKey(),