Skip to content

Commit

Permalink
Porting leakDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
DevYeom committed May 23, 2021
1 parent 5b5f53d commit 06f19ec
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions ModernRIBs/Classes/LeakDetector/LeakDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public class LeakDetector {
///
/// The status changes between InProgress and DidComplete as units register for new detections, cancel existing
/// detections, and existing detections complete.
public var status: Observable<LeakDetectionStatus> {
public var status: AnyPublisher<LeakDetectionStatus, Never> {
return expectationCount
.asObservable()
.removeDuplicates()
.map { expectationCount in
expectationCount > 0 ? LeakDetectionStatus.InProgress : LeakDetectionStatus.DidComplete
}
.distinctUntilChanged()
.eraseToAnyPublisher()
}

/// Sets up an expectation for the given object to be deallocated within the given time.
Expand All @@ -75,14 +75,14 @@ public class LeakDetector {
/// - returns: The handle that can be used to cancel the expectation.
@discardableResult
public func expectDeallocate(object: AnyObject, inTime time: TimeInterval = LeakDefaultExpectationTime.deallocation) -> LeakDetectionHandle {
expectationCount.accept(expectationCount.value + 1)
expectationCount.send(expectationCount.value + 1)

let objectDescription = String(describing: object)
let objectId = String(ObjectIdentifier(object).hashValue) as NSString
trackingObjects.setObject(object, forKey: objectId)

let handle = LeakDetectionHandleImpl {
self.expectationCount.accept(self.expectationCount.value - 1)
self.expectationCount.send(self.expectationCount.value - 1)
}

Executor.execute(withDelay: time) {
Expand All @@ -102,7 +102,7 @@ public class LeakDetector {
}
}

self.expectationCount.accept(self.expectationCount.value - 1)
self.expectationCount.send(self.expectationCount.value - 1)
}

return handle
Expand All @@ -115,10 +115,10 @@ public class LeakDetector {
/// - returns: The handle that can be used to cancel the expectation.
@discardableResult
public func expectViewControllerDisappear(viewController: UIViewController, inTime time: TimeInterval = LeakDefaultExpectationTime.viewDisappear) -> LeakDetectionHandle {
expectationCount.accept(expectationCount.value + 1)
expectationCount.send(expectationCount.value + 1)

let handle = LeakDetectionHandleImpl {
self.expectationCount.accept(self.expectationCount.value - 1)
self.expectationCount.send(self.expectationCount.value - 1)
}

Executor.execute(withDelay: time) { [weak viewController] in
Expand All @@ -138,7 +138,7 @@ public class LeakDetector {
}
}

self.expectationCount.accept(self.expectationCount.value - 1)
self.expectationCount.send(self.expectationCount.value - 1)
}

return handle
Expand All @@ -153,14 +153,14 @@ public class LeakDetector {
/// Reset the state of Leak Detector, internal for UI test only.
func reset() {
trackingObjects.removeAllObjects()
expectationCount.accept(0)
expectationCount.send(0)
}
#endif

// MARK: - Private Interface

private let trackingObjects = NSMapTable<AnyObject, AnyObject>.strongToWeakObjects()
private let expectationCount = BehaviorRelay<Int>(value: 0)
private let expectationCount = CurrentValueSubject<Int, Never>(0)

lazy var disableLeakDetector: Bool = {
if let environmentValue = ProcessInfo().environment["DISABLE_LEAK_DETECTION"] {
Expand All @@ -179,15 +179,15 @@ fileprivate class LeakDetectionHandleImpl: LeakDetectionHandle {
return cancelledRelay.value
}

let cancelledRelay = BehaviorRelay<Bool>(value: false)
let cancelledRelay = CurrentValueSubject<Bool, Never>(false)
let cancelClosure: (() -> ())?

init(cancelClosure: (() -> ())? = nil) {
self.cancelClosure = cancelClosure
}

func cancel() {
cancelledRelay.accept(true)
cancelledRelay.send(true)
cancelClosure?()
}
}

0 comments on commit 06f19ec

Please sign in to comment.