Repository: Alamofire/Alamofire
File: Source/Request.swift (Swift)

Code before:

import Foundation

/// `Request` is the common superclass of all Alamofire request types and provides common state, delegate, and callback
/// handling.
public class Request {
    /// State of the `Request`, with managed transitions between states set when calling `resume()`, `suspend()`, or
    /// `cancel()` on the `Request`.
    ///
    /// - initialized: Initial state of the `Request`.
    /// - resumed:   Set when `resume()` is called. Any tasks created for the `Request` will have `resume()` called on
    ///              them in this state.
    /// - suspended: Set when `suspend()` is called. Any tasks created for the `Request` will have `suspend()` called on
    ///              them in this state.
    /// - cancelled: Set when `cancel()` is called. Any tasks created for the `Request` will have `cancel()` called on
    ///              them. Unlike `resumed` or `suspended`, once in the `cancelled` state, the `Request` can no longer
    ///              transition to any other state.
    /// - finished:  Set when all response serialization completion closures have been cleared on the `Request` and
    ///              queued on their respective queues.
    public enum State {
        case initialized, resumed, suspended, cancelled, finished

        /// Determines whether `self` can be transitioned to `state`.
        func canTransitionTo(_ state: State) -> Bool {
            switch (self, state) {
            case (.initialized, _):
                return true
            case (_, .initialized), (.cancelled, _):
                return false
            case (.resumed, .cancelled), (.suspended, .cancelled), (.resumed, .suspended), (.suspended, .resumed):
                return true
            case (.suspended, .suspended), (.resumed, .resumed):
                return false
            case (_, .finished):
                return true
            case (.finished, _):
                return false
            }
        }
    }

    // MARK: - Initial State

    /// `UUID` prividing a unique identifier for the `Request`, used in the `Hashable` and `Equatable` conformances.
    public let id: UUID
    /// The serial queue for all internal async actions.
    public let underlyingQueue: DispatchQueue
    /// The queue used for all serialization actions. By default it's a serial queue that targets `underlyingQueue`.
    public let serializationQueue: DispatchQueue
    /// `EventMonitor` used for event callbacks.
    public let eventMonitor: EventMonitor?

Review comment (suggestion):
I think the state machine need to be updated to ensure (or make explicit) we can only transition to `finished` from non-`cancelled` states.

Code after:

import Foundation

/// `Request` is the common superclass of all Alamofire request types and provides common state, delegate, and callback
/// handling.
public class Request {
    /// State of the `Request`, with managed transitions between states set when calling `resume()`, `suspend()`, or
    /// `cancel()` on the `Request`.
    ///
    /// - initialized: Initial state of the `Request`.
    /// - resumed:   Set when `resume()` is called. Any tasks created for the `Request` will have `resume()` called on
    ///              them in this state.
    /// - suspended: Set when `suspend()` is called. Any tasks created for the `Request` will have `suspend()` called on
    ///              them in this state.
    /// - cancelled: Set when `cancel()` is called. Any tasks created for the `Request` will have `cancel()` called on
    ///              them. Unlike `resumed` or `suspended`, once in the `cancelled` state, the `Request` can no longer
    ///              transition to any other state.
    /// - finished:  Set when all response serialization completion closures have been cleared on the `Request` and
    ///              queued on their respective queues.
    public enum State {
        case initialized, resumed, suspended, cancelled, finished

        /// Determines whether `self` can be transitioned to `state`.
        func canTransitionTo(_ state: State) -> Bool {
            switch (self, state) {
            case (.initialized, _):
                return true
            case (_, .initialized), (.cancelled, _), (.finished, _):
                return false
            case (.resumed, .cancelled), (.suspended, .cancelled), (.resumed, .suspended), (.suspended, .resumed):
                return true
            case (.suspended, .suspended), (.resumed, .resumed):
                return false
            case (_, .finished):
                return true
            }
        }
    }

    // MARK: - Initial State

    /// `UUID` prividing a unique identifier for the `Request`, used in the `Hashable` and `Equatable` conformances.
    public let id: UUID
    /// The serial queue for all internal async actions.
    public let underlyingQueue: DispatchQueue
    /// The queue used for all serialization actions. By default it's a serial queue that targets `underlyingQueue`.
    public let serializationQueue: DispatchQueue
    /// `EventMonitor` used for event callbacks.
    public let eventMonitor: EventMonitor?
    /// The `Request`'s interceptor.
    public let interceptor: RequestInterceptor?