Skip to content

Commit

Permalink
revert formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jimexist committed May 21, 2024
1 parent 3d4b5df commit 164c970
Showing 1 changed file with 143 additions and 143 deletions.
286 changes: 143 additions & 143 deletions PostHog/Replay/URLSessionSwizzler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 164c970

Please sign in to comment.