Skip to content

Commit 0322454

Browse files
committed
Replace implementation of WeakSet with WeakSequence
1 parent 2bc96d0 commit 0322454

21 files changed

+462
-204
lines changed

LeakDetectorDemo/Leak/Coordinator/BaseCoordinator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ open class BaseCoordinator<T>: UINavigationController, Coordinator {
1111
public let dependency: T
1212
private var cancellables = Set<AnyCancellable>()
1313

14-
private var trackingViewControllers = WeakSet<UIViewController>()
14+
private var trackingViewControllers = WeakSequenceOf<AnyObject>()
1515

1616
public init(with dependency: T) {
1717
self.dependency = dependency

LeakDetectorDemo/Leak/Delegate/DelegateRootViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// Copyright © 2021 An Tran. All rights reserved.
33
//
44

5+
import Combine
56
import Foundation
6-
import UIKit
77
import LeakDetectorCombine
8-
import Combine
8+
import UIKit
99

1010
final class DelegateRootViewController: LeakDetectableTableViewController {
1111

LeakDetectorDemo/Leak/LeakDetectorRxSwift/LeakDetectorRxSwiftChildViewControllers.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ final class LeakDetectorRxSwiftViewController1: ChildViewController {
2626
}
2727
}
2828

29-
3029
// MARK: - No Leak
3130

3231
final class NoLeakLeakDetectorRxSwiftViewController1: ChildViewController {

LeakDetectorDemo/Leak/LeakDetectorRxSwift/LeakDetectorRxSwiftMultiVCRootViewController.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
//
44

55
import Foundation
6-
import UIKit
76
import LeakDetectorCore
87
import LeakDetectorRxSwift
8+
import UIKit
99

1010
final class LeakDetectorRxSwiftMultiVCRootViewController: UITableViewController {
1111

12-
private var viewControllers = WeakSet<UIViewController>()
12+
private var viewControllers = WeakSequenceOf<UIViewController>()
1313
private var leakSubscription: LeakDetectionHandle?
1414

1515
private enum Scenarios {
@@ -35,9 +35,7 @@ final class LeakDetectorRxSwiftMultiVCRootViewController: UITableViewController
3535
}
3636

3737
override func viewDidAppear(_ animated: Bool) {
38-
if !viewControllers.isEmpty {
39-
executeLeakDetector()
40-
}
38+
executeLeakDetector()
4139
}
4240

4341
override func numberOfSections(in tableView: UITableView) -> Int {

LeakDetectorDemo/Leak/RxSwift/RxViewController.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ class NoLeakRxSwiftViewController1: ChildViewController {
7979
class NoLeakRxSwiftViewController2: ChildViewController {
8080

8181
private let disposeBag = DisposeBag()
82-
private lazy var button = {
83-
UIButton()
84-
}()
82+
private lazy var button = UIButton()
8583

8684
override func viewDidLoad() {
8785
super.viewDidLoad()

LeakDetectorDemo/Leak/UICollectionView/UICollectionViewControllers.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//
2-
// UICollectionViewControllers.swift
3-
// LeakDetectorDemo
4-
//
5-
// Created by An Tran on 2/11/22.
2+
// Copyright © 2021 An Tran. All rights reserved.
63
//
74

85
import Foundation
@@ -69,7 +66,6 @@ class BaseUICollectionViewController: UIViewController {
6966
return NSCollectionLayoutSection(group: group)
7067
}
7168

72-
7369
func makeDataSource() -> UICollectionViewDiffableDataSource<Section, Product> {
7470
let cellRegistration = makeCellRegistration()
7571

@@ -95,7 +91,6 @@ class BaseUICollectionViewController: UIViewController {
9591
}
9692
}
9793

98-
9994
@available(iOS 14.0, *)
10095
final class LeakUICollectionViewController: BaseUICollectionViewController {
10196

LeakDetectorDemo/Leak/UICollectionView/UICollectionViewRootViewController.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
//
2-
// UICollectionViewRootViewController.swift
3-
// LeakDetectorDemo
4-
//
5-
// Created by An Tran on 2/11/22.
2+
// Copyright © 2021 An Tran. All rights reserved.
63
//
74

8-
import Foundation
9-
import UIKit
105
import Combine
6+
import Foundation
117
import LeakDetectorCombine
8+
import UIKit
129

1310
final class UICollectionViewRootViewController: LeakDetectableTableViewController {
1411
private enum Scenarios {

LeakDetectorDemo/Utils/LeakDetectableRxSwiftTableViewController.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
//
2-
// LeakDetectableRxSwiftTableViewController.swift
3-
// LeakDetectorDemo
4-
//
5-
// Created by Binh An Tran on 1/9/21.
2+
// Copyright © 2021 An Tran. All rights reserved.
63
//
74

85
import Foundation
96
import LeakDetectorRxSwift
10-
import UIKit
117
import RxSwift
8+
import UIKit
129

1310
class LeakDetectableRxSwiftTableViewController: UITableViewController {
1411

@@ -43,4 +40,3 @@ class LeakDetectableRxSwiftTableViewController: UITableViewController {
4340
}
4441
}
4542
}
46-

LeakDetectorDemo/Utils/UIViewController+Alert.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//
2-
// UIViewController+Alert.swift
3-
// LeakDetectorDemo
4-
//
5-
// Created by An Tran on 2/11/22.
2+
// Copyright © 2021 An Tran. All rights reserved.
63
//
74

85
import Foundation

LeakDetectorDemoUITests/LeakTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ final class LeakTests: XCTestCase {
112112
)
113113
}
114114

115-
116115
private func assert(testCase: String, subcase: String? = nil, exitAction: () -> Void, timeout: TimeInterval = .viewDisappearExpectation + LeakTests.bufferInternal) {
117116
app.tables.firstMatch.scrollTo(element: app.tables.staticTexts[testCase])
118117
app.tables.staticTexts[testCase].tap()

LeakDetectorDemoUITests/XCUIElement+Extensions.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//
2-
// XCUIElement+Extensions.swift
3-
// LeakDetectorDemoUITests
4-
//
5-
// Created by An Tran on 2/11/22.
2+
// Copyright © 2021 An Tran. All rights reserved.
63
//
74

85
import Foundation

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let package = Package(
4040
"LeakDetectorCore",
4141
]
4242
),
43-
43+
4444
.target(
4545
name: "LeakDetectorRxSwift",
4646
dependencies: [
@@ -49,7 +49,7 @@ let package = Package(
4949
.product(name: "RxCocoa", package: "RxSwift"),
5050
]
5151
),
52-
52+
5353
.testTarget(
5454
name: "LeakDetectorTests",
5555
dependencies: [

Sources/LeakDetectorCombine/LeakDetector.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class LeakDetector {
3333

3434
// MARK: - Private Interface
3535

36-
private(set) var trackingObjects = WeakSet<AnyObject>()
36+
private(set) var trackingObjects = WeakSequenceOf<AnyObject>()
3737
@Published var expectationCount: Int = 0 {
3838
didSet {
3939
if expectationCount == 0 {
@@ -50,11 +50,11 @@ public class LeakDetector {
5050
/// - parameter objects: The weak set of objects to track for deallocation.
5151
/// - parameter inTime: The time the given object is expected to be deallocated within.
5252
/// - returns: `Publishers.First` that outputs after delay.
53-
public func expectDeallocate<Element>(
54-
objects: WeakSet<Element>,
53+
public func expectDeallocate<T>(
54+
objects: WeakSequenceOf<T>,
5555
inTime time: TimeInterval = .deallocationExpectation
5656
) -> AnyPublisher<Void, Never> {
57-
guard !objects.isEmpty else { return Empty().eraseToAnyPublisher() }
57+
guard !objects.asArray.isEmpty else { return Empty().eraseToAnyPublisher() }
5858
return Timer
5959
.execute(withDelay: time)
6060
.receive(on: DispatchQueue.main)
@@ -64,7 +64,7 @@ public class LeakDetector {
6464
self.expectationCount += 1
6565
},
6666
receiveOutput: {
67-
if !objects.isEmpty {
67+
if !objects.asArray.isEmpty {
6868
let message = "\(objects) have leaked. Objects are expected to be deallocated at this time: \(self.trackingObjects)"
6969
if self.isEnabled {
7070
assertionFailure(message)
@@ -107,7 +107,7 @@ public class LeakDetector {
107107
},
108108
receiveOutput: { [weak object] in
109109
if let object = object {
110-
let message = "<\(memoryAddressDescription(for: object))> has leaked. Objects are expected to be deallocated at this time: \(self.trackingObjects)"
110+
let message = "<\(object)> has leaked. Objects are expected to be deallocated at this time: \(self.trackingObjects)"
111111

112112
if self.isEnabled {
113113
assertionFailure(message)
@@ -138,12 +138,12 @@ public class LeakDetector {
138138

139139
public var isLeaked = CurrentValueSubject<String?, Never>(nil)
140140

141-
#if DEBUG
141+
#if DEBUG
142142
/// Reset the state of Leak Detector, internal for UI test only.
143143
func reset() {
144144
trackingObjects.removeAll()
145145
expectationCount = 0
146146
isLeaked.send(nil)
147147
}
148-
#endif
148+
#endif
149149
}

Sources/LeakDetectorCombine/Timer.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
//
2-
// Copyright (c) 2021. Adam Share
3-
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
2+
// Copyright © 2021 An Tran. All rights reserved.
153
//
164

175
#if canImport(Combine)

Sources/LeakDetectorCore/LeakDetectorStatus.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Binh An Tran on 1/9/21.
2+
// Copyright © 2021 An Tran. All rights reserved.
63
//
74

85
import Foundation

0 commit comments

Comments
 (0)