Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:
        const auto actualNewSize = _actualFont.GetSize();
        // Bubble this up, so our new control knows how big we want the font.
        _FontSizeChangedHandlers(actualNewSize.width, actualNewSize.height, true);

        // The renderer will be re-enabled in Initialize

        _AttachedHandlers(*this, nullptr);
    }

    TerminalConnection::ITerminalConnection ControlCore::Connection()
    {
        return _connection;
    }

    // Method Description:
    // - Setup our event handlers for this connection. If we've currently got a
    //   connection, then this'll revoke the existing connection's handlers.
    void ControlCore::Connection(const TerminalConnection::ITerminalConnection& newConnection)
    {
        if (_connection)
        {
            _connectionOutputEventRevoker.revoke();

            // Fire off a connection state changed notification, to let our hosting
            // app know that we're in a different state now.
            if (newConnection.State() != _connection.State())
            {
                _ConnectionStateChangedHandlers(*this, nullptr);
            }
        }

        // Subscribe to the connection's disconnected event and call our connection closed handlers.
        _connectionStateChangedRevoker = newConnection.StateChanged(winrt::auto_revoke, [this](auto&& /*s*/, auto&& /*v*/) {
            _ConnectionStateChangedHandlers(*this, nullptr);
        });

        // Get our current size in rows/cols, and hook them up to
        // this connection too.
        {
            const auto vp = _terminal->GetViewport();
            const auto width = vp.Width();
            const auto height = vp.Height();

            newConnection.Resize(height, width);
        }
        // Window owner too.
        if (auto conpty{ newConnection.try_as<TerminalConnection::ConptyConnection>() })
        {
            conpty.ReparentWindow(_owningHwnd);
        }
        const bool replacing = _connection != nullptr;

Review comment (suggestion):
wait, this has to go into the block **after** `_connection` changes (I think).

This is because somebody will call right back into TermControl to ask the current state (!)

At this point it will reflect the old connection.

Code after:
        const auto actualNewSize = _actualFont.GetSize();
        // Bubble this up, so our new control knows how big we want the font.
        _FontSizeChangedHandlers(actualNewSize.width, actualNewSize.height, true);

        // The renderer will be re-enabled in Initialize

        _AttachedHandlers(*this, nullptr);
    }

    TerminalConnection::ITerminalConnection ControlCore::Connection()
    {
        return _connection;
    }

    // Method Description:
    // - Setup our event handlers for this connection. If we've currently got a
    //   connection, then this'll revoke the existing connection's handlers.
    // - This will not call Start on the incoming connection. The caller should do that.
    // - If the caller doesn't want the old connection to be closed, then they
    //   should grab a reference to it before calling this (so that it doesn't
    //   destruct, and close) during this call.
    void ControlCore::Connection(const TerminalConnection::ITerminalConnection& newConnection)
    {
        auto oldState = ConnectionState(); // rely on ControlCore's automatic null handling
        // revoke ALL old handlers immediately

        _connectionOutputEventRevoker.revoke();
        _connectionStateChangedRevoker.revoke();

        _connection = newConnection;
        if (_connection)
        {
            // Subscribe to the connection's disconnected event and call our connection closed handlers.
            _connectionStateChangedRevoker = newConnection.StateChanged(winrt::auto_revoke, [this](auto&& /*s*/, auto&& /*v*/) {
                _ConnectionStateChangedHandlers(*this, nullptr);
            });

            // Get our current size in rows/cols, and hook them up to
            // this connection too.
            {
                const auto vp = _terminal->GetViewport();
                const auto width = vp.Width();
                const auto height = vp.Height();

                newConnection.Resize(height, width);
            }
            // Window owner too.
            if (auto conpty{ newConnection.try_as<TerminalConnection::ConptyConnection>() })
            {
                conpty.ReparentWindow(_owningHwnd);
            }