From 164c9707e40720de2e4a0149198856faf278b070 Mon Sep 17 00:00:00 2001 From: Jiayu Liu Date: Tue, 21 May 2024 19:34:20 +0800 Subject: [PATCH] revert formatting --- PostHog/Replay/URLSessionSwizzler.swift | 286 ++++++++++++------------ 1 file changed, 143 insertions(+), 143 deletions(-) diff --git a/PostHog/Replay/URLSessionSwizzler.swift b/PostHog/Replay/URLSessionSwizzler.swift index acd8ef276..a635af84f 100644 --- a/PostHog/Replay/URLSessionSwizzler.swift +++ b/PostHog/Replay/URLSessionSwizzler.swift @@ -60,192 +60,192 @@ /// Swizzles the `URLSession.dataTask(with:completionHandler:)` for `URLRequest`. class DataTaskWithURLRequestAndCompletion: MethodSwizzler< - @convention(c) (URLSession, Selector, URLRequest, CompletionHandler?) -> URLSessionDataTask, + @convention(c) (URLSession, Selector, URLRequest, CompletionHandler?) -> URLSessionDataTask, @convention(block) (URLSession, URLRequest, CompletionHandler?) -> URLSessionDataTask - > { - private static let selector = #selector( - URLSession.dataTask(with:completionHandler:) as (URLSession) -> (URLRequest, @escaping CompletionHandler) -> URLSessionDataTask - ) - - private let method: FoundMethod - private let interceptor: URLSessionInterceptor - - static func build(interceptor: URLSessionInterceptor) throws -> DataTaskWithURLRequestAndCompletion { - try DataTaskWithURLRequestAndCompletion( - selector: selector, - klass: URLSession.self, - interceptor: interceptor + > { + private static let selector = #selector( + URLSession.dataTask(with:completionHandler:) as (URLSession) -> (URLRequest, @escaping CompletionHandler) -> URLSessionDataTask ) - } - private init(selector: Selector, klass: AnyClass, interceptor: URLSessionInterceptor) throws { - method = try Self.findMethod(with: selector, in: klass) - self.interceptor = interceptor - super.init() - } + private let method: FoundMethod + private let interceptor: URLSessionInterceptor - func swizzle() { - typealias Signature = @convention(block) (URLSession, URLRequest, CompletionHandler?) -> URLSessionDataTask - swizzle(method) { previousImplementation -> Signature in { session, urlRequest, completionHandler -> URLSessionDataTask in - let task: URLSessionDataTask - if completionHandler != nil { - var taskReference: URLSessionDataTask? - let newCompletionHandler: CompletionHandler = { data, response, error in - if let task = taskReference { // sanity check, should always succeed - if let data = data { - self.interceptor.taskReceivedData(task: task, data: data) + static func build(interceptor: URLSessionInterceptor) throws -> DataTaskWithURLRequestAndCompletion { + try DataTaskWithURLRequestAndCompletion( + selector: selector, + klass: URLSession.self, + interceptor: interceptor + ) + } + + private init(selector: Selector, klass: AnyClass, interceptor: URLSessionInterceptor) throws { + method = try Self.findMethod(with: selector, in: klass) + self.interceptor = interceptor + super.init() + } + + func swizzle() { + typealias Signature = @convention(block) (URLSession, URLRequest, CompletionHandler?) -> URLSessionDataTask + swizzle(method) { previousImplementation -> Signature in { session, urlRequest, completionHandler -> URLSessionDataTask in + let task: URLSessionDataTask + if completionHandler != nil { + var taskReference: URLSessionDataTask? + let newCompletionHandler: CompletionHandler = { data, response, error in + if let task = taskReference { // sanity check, should always succeed + if let data = data { + self.interceptor.taskReceivedData(task: task, data: data) + } + self.interceptor.taskCompleted(task: task, error: error) } - self.interceptor.taskCompleted(task: task, error: error) + completionHandler?(data, response, error) } - completionHandler?(data, response, error) - } - task = previousImplementation(session, Self.selector, urlRequest, newCompletionHandler) - taskReference = task - } else { - // The `completionHandler` can be `nil` in two cases: - // - on iOS 11 or 12, where `dataTask(with:)` (for `URL` and `URLRequest`) calls - // the `dataTask(with:completionHandler:)` (for `URLRequest`) internally by nullifying the completion block. - // - when `[session dataTaskWithURL:completionHandler:]` is called in Objective-C with explicitly passing - // `nil` as the `completionHandler` (it produces a warning, but compiles). - task = previousImplementation(session, Self.selector, urlRequest, completionHandler) + task = previousImplementation(session, Self.selector, urlRequest, newCompletionHandler) + taskReference = task + } else { + // The `completionHandler` can be `nil` in two cases: + // - on iOS 11 or 12, where `dataTask(with:)` (for `URL` and `URLRequest`) calls + // the `dataTask(with:completionHandler:)` (for `URLRequest`) internally by nullifying the completion block. + // - when `[session dataTaskWithURL:completionHandler:]` is called in Objective-C with explicitly passing + // `nil` as the `completionHandler` (it produces a warning, but compiles). + task = previousImplementation(session, Self.selector, urlRequest, completionHandler) + } + self.interceptor.taskCreated(task: task, session: session) + return task + } } - self.interceptor.taskCreated(task: task, session: session) - return task - } } } - } /// Swizzles the `URLSession.dataTask(with:completionHandler:)` for `URL`. class DataTaskWithURLAndCompletion: MethodSwizzler< - @convention(c) (URLSession, Selector, URL, CompletionHandler?) -> URLSessionDataTask, + @convention(c) (URLSession, Selector, URL, CompletionHandler?) -> URLSessionDataTask, @convention(block) (URLSession, URL, CompletionHandler?) -> URLSessionDataTask - > { - private static let selector = #selector( - URLSession.dataTask(with:completionHandler:) as (URLSession) -> (URL, @escaping CompletionHandler) -> URLSessionDataTask - ) - - private let method: FoundMethod - private let interceptor: URLSessionInterceptor - - static func build(interceptor: URLSessionInterceptor) throws -> DataTaskWithURLAndCompletion { - try DataTaskWithURLAndCompletion( - selector: selector, - klass: URLSession.self, - interceptor: interceptor + > { + private static let selector = #selector( + URLSession.dataTask(with:completionHandler:) as (URLSession) -> (URL, @escaping CompletionHandler) -> URLSessionDataTask ) - } - private init(selector: Selector, klass: AnyClass, interceptor: URLSessionInterceptor) throws { - method = try Self.findMethod(with: selector, in: klass) - self.interceptor = interceptor - super.init() - } + private let method: FoundMethod + private let interceptor: URLSessionInterceptor - func swizzle() { - typealias Signature = @convention(block) (URLSession, URL, CompletionHandler?) -> URLSessionDataTask - swizzle(method) { previousImplementation -> Signature in { session, url, completionHandler -> URLSessionDataTask in - let task: URLSessionDataTask - if completionHandler != nil { - var taskReference: URLSessionDataTask? - let newCompletionHandler: CompletionHandler = { data, response, error in - if let task = taskReference { // sanity check, should always succeed - if let data = data { - self.interceptor.taskReceivedData(task: task, data: data) + static func build(interceptor: URLSessionInterceptor) throws -> DataTaskWithURLAndCompletion { + try DataTaskWithURLAndCompletion( + selector: selector, + klass: URLSession.self, + interceptor: interceptor + ) + } + + private init(selector: Selector, klass: AnyClass, interceptor: URLSessionInterceptor) throws { + method = try Self.findMethod(with: selector, in: klass) + self.interceptor = interceptor + super.init() + } + + func swizzle() { + typealias Signature = @convention(block) (URLSession, URL, CompletionHandler?) -> URLSessionDataTask + swizzle(method) { previousImplementation -> Signature in { session, url, completionHandler -> URLSessionDataTask in + let task: URLSessionDataTask + if completionHandler != nil { + var taskReference: URLSessionDataTask? + let newCompletionHandler: CompletionHandler = { data, response, error in + if let task = taskReference { // sanity check, should always succeed + if let data = data { + self.interceptor.taskReceivedData(task: task, data: data) + } + self.interceptor.taskCompleted(task: task, error: error) } - self.interceptor.taskCompleted(task: task, error: error) + completionHandler?(data, response, error) } - completionHandler?(data, response, error) + task = previousImplementation(session, Self.selector, url, newCompletionHandler) + taskReference = task + } else { + // The `completionHandler` can be `nil` in one case: + // - when `[session dataTaskWithURL:completionHandler:]` is called in Objective-C with explicitly passing + // `nil` as the `completionHandler` (it produces a warning, but compiles). + task = previousImplementation(session, Self.selector, url, completionHandler) } - task = previousImplementation(session, Self.selector, url, newCompletionHandler) - taskReference = task - } else { - // The `completionHandler` can be `nil` in one case: - // - when `[session dataTaskWithURL:completionHandler:]` is called in Objective-C with explicitly passing - // `nil` as the `completionHandler` (it produces a warning, but compiles). - task = previousImplementation(session, Self.selector, url, completionHandler) + self.interceptor.taskCreated(task: task, session: session) + return task + } } - self.interceptor.taskCreated(task: task, session: session) - return task - } } } - } /// Swizzles the `URLSession.dataTask(with:)` for `URLRequest`. class DataTaskWithURLRequest: MethodSwizzler< - @convention(c) (URLSession, Selector, URLRequest) -> URLSessionDataTask, + @convention(c) (URLSession, Selector, URLRequest) -> URLSessionDataTask, @convention(block) (URLSession, URLRequest) -> URLSessionDataTask - > { - private static let selector = #selector( - URLSession.dataTask(with:) as (URLSession) -> (URLRequest) -> URLSessionDataTask - ) - - private let method: FoundMethod - private let interceptor: URLSessionInterceptor - - static func build(interceptor: URLSessionInterceptor) throws -> DataTaskWithURLRequest { - try DataTaskWithURLRequest( - selector: selector, - klass: URLSession.self, - interceptor: interceptor + > { + private static let selector = #selector( + URLSession.dataTask(with:) as (URLSession) -> (URLRequest) -> URLSessionDataTask ) - } - private init(selector: Selector, klass: AnyClass, interceptor: URLSessionInterceptor) throws { - method = try Self.findMethod(with: selector, in: klass) - self.interceptor = interceptor - super.init() - } + private let method: FoundMethod + private let interceptor: URLSessionInterceptor - func swizzle() { - typealias Signature = @convention(block) (URLSession, URLRequest) -> URLSessionDataTask - swizzle(method) { previousImplementation -> Signature in { session, urlRequest -> URLSessionDataTask in - let task = previousImplementation(session, Self.selector, urlRequest) - self.interceptor.taskCreated(task: task, session: session) - return task + static func build(interceptor: URLSessionInterceptor) throws -> DataTaskWithURLRequest { + try DataTaskWithURLRequest( + selector: selector, + klass: URLSession.self, + interceptor: interceptor + ) } + + private init(selector: Selector, klass: AnyClass, interceptor: URLSessionInterceptor) throws { + method = try Self.findMethod(with: selector, in: klass) + self.interceptor = interceptor + super.init() + } + + func swizzle() { + typealias Signature = @convention(block) (URLSession, URLRequest) -> URLSessionDataTask + swizzle(method) { previousImplementation -> Signature in { session, urlRequest -> URLSessionDataTask in + let task = previousImplementation(session, Self.selector, urlRequest) + self.interceptor.taskCreated(task: task, session: session) + return task + } + } } } - } /// Swizzles the `URLSession.dataTask(with:)` for `URL`. class DataTaskWithURL: MethodSwizzler< - @convention(c) (URLSession, Selector, URL) -> URLSessionDataTask, + @convention(c) (URLSession, Selector, URL) -> URLSessionDataTask, @convention(block) (URLSession, URL) -> URLSessionDataTask - > { - private static let selector = #selector( - URLSession.dataTask(with:) as (URLSession) -> (URL) -> URLSessionDataTask - ) - - private let method: FoundMethod - private let interceptor: URLSessionInterceptor - - static func build(interceptor: URLSessionInterceptor) throws -> DataTaskWithURL { - try DataTaskWithURL( - selector: selector, - klass: URLSession.self, - interceptor: interceptor + > { + private static let selector = #selector( + URLSession.dataTask(with:) as (URLSession) -> (URL) -> URLSessionDataTask ) - } - private init(selector: Selector, klass: AnyClass, interceptor: URLSessionInterceptor) throws { - method = try Self.findMethod(with: selector, in: klass) - self.interceptor = interceptor - super.init() - } + private let method: FoundMethod + private let interceptor: URLSessionInterceptor - func swizzle() { - typealias Signature = @convention(block) (URLSession, URL) -> URLSessionDataTask - swizzle(method) { previousImplementation -> Signature in { session, url -> URLSessionDataTask in - let task = previousImplementation(session, Self.selector, url) - self.interceptor.taskCreated(task: task, session: session) - return task + static func build(interceptor: URLSessionInterceptor) throws -> DataTaskWithURL { + try DataTaskWithURL( + selector: selector, + klass: URLSession.self, + interceptor: interceptor + ) } + + private init(selector: Selector, klass: AnyClass, interceptor: URLSessionInterceptor) throws { + method = try Self.findMethod(with: selector, in: klass) + self.interceptor = interceptor + super.init() + } + + func swizzle() { + typealias Signature = @convention(block) (URLSession, URL) -> URLSessionDataTask + swizzle(method) { previousImplementation -> Signature in { session, url -> URLSessionDataTask in + let task = previousImplementation(session, Self.selector, url) + self.interceptor.taskCreated(task: task, session: session) + return task + } + } } } - } } #endif