Repository: rails/rails
File: actioncable/lib/action_cable/channel/base.rb (Ruby)

Code before:
          payload = { channel_class: self.class.name, action: action, data: data }
          ActiveSupport::Notifications.instrument("perform_action.action_cable", payload) do
            dispatch_action(action, data)
          end
        else
          logger.error "Unable to process #{action_signature(action, data)}"
        end
      end

      # This method is called after subscription has been added to the connection and
      # confirms or rejects the subscription.
      def subscribe_to_channel
        run_callbacks :subscribe do
          subscribed
        end

        reject_subscription if subscription_rejected?
        ensure_confirmation_sent
      end

      # Called by the cable connection when it's cut, so the channel has a chance to
      # cleanup with callbacks. This method is not intended to be called directly by
      # the user. Instead, override the #unsubscribed callback.
      def unsubscribe_from_channel # :nodoc:
        @unsubscribed = true
        run_callbacks :unsubscribe do
          unsubscribed
        end
      end

      def unsubscribed? # :nodoc:
        @unsubscribed
      end

      private
        # Called once a consumer has become a subscriber of the channel. Usually the
        # place to set up any streams you want this channel to be sending to the
        # subscriber.
        def subscribed # :doc:
          # Override in subclasses
        end

        # Called once a consumer has cut its cable connection. Can be used for cleaning
        # up connections or marking users as offline or the like.
        def unsubscribed # :doc:
          # Override in subclasses
        end

        # Transmit a hash of data to the subscriber. The hash will automatically be
        # wrapped in a JSON envelope with the proper channel identifier marked as the
        # recipient.

Review comment (suggestion):
Consider initializing the @unsubscribed instance variable to false (e.g., in the channel's constructor or subscribe_to_channel method) to ensure a clear and explicit boolean state throughout the channel lifecycle.

Code after:
        if processable_action?(action)
          payload = { channel_class: self.class.name, action: action, data: data }
          ActiveSupport::Notifications.instrument("perform_action.action_cable", payload) do
            dispatch_action(action, data)
          end
        else
          logger.error "Unable to process #{action_signature(action, data)}"
        end
      end

      # This method is called after subscription has been added to the connection and
      # confirms or rejects the subscription.
      def subscribe_to_channel
        run_callbacks :subscribe do
          subscribed
        end

        reject_subscription if subscription_rejected?
        ensure_confirmation_sent
      end

      # Called by the cable connection when it's cut, so the channel has a chance to
      # cleanup with callbacks. This method is not intended to be called directly by
      # the user. Instead, override the #unsubscribed callback.
      def unsubscribe_from_channel # :nodoc:
        @unsubscribed = true
        run_callbacks :unsubscribe do
          unsubscribed
        end
      end

      def unsubscribed? # :nodoc:
        @unsubscribed
      end

      private
        # Called once a consumer has become a subscriber of the channel. Usually the
        # place to set up any streams you want this channel to be sending to the
        # subscriber.
        def subscribed # :doc:
          # Override in subclasses
        end

        # Called once a consumer has cut its cable connection. Can be used for cleaning
        # up connections or marking users as offline or the like.
        def unsubscribed # :doc:
          # Override in subclasses
        end

        # Transmit a hash of data to the subscriber. The hash will automatically be
        # wrapped in a JSON envelope with the proper channel identifier marked as the