Skip to content

Commit cf81735

Browse files
committed
move the extension method into class so we can debug on variables.
1 parent e07ab48 commit cf81735

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

BuildTimeAnalyzer/LogProcessor.swift

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,56 @@ protocol LogProcessorProtocol: class {
1818
func processingDidFinish()
1919
}
2020

21-
extension LogProcessorProtocol {
21+
class LogProcessor: NSObject, LogProcessorProtocol {
22+
23+
var rawMeasures: [String: RawMeasure] = [:]
24+
var updateHandler: CMUpdateClosure?
25+
var shouldCancel = false
26+
var timer: Timer?
27+
28+
func processingDidStart() {
29+
DispatchQueue.main.async {
30+
self.timer = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(self.timerCallback(_:)), userInfo: nil, repeats: true)
31+
}
32+
}
33+
34+
func processingDidFinish() {
35+
DispatchQueue.main.async {
36+
self.timer?.invalidate()
37+
self.timer = nil
38+
let didCancel = self.shouldCancel
39+
self.shouldCancel = false
40+
self.updateResults(didComplete: true, didCancel: didCancel)
41+
}
42+
}
43+
44+
@objc func timerCallback(_ timer: Timer) {
45+
updateResults(didComplete: false, didCancel: false)
46+
}
47+
2248
func processDatabase(database: XcodeDatabase, updateHandler: CMUpdateClosure?) {
2349
guard let text = database.processLog() else {
2450
updateHandler?([], true, false)
2551
return
2652
}
27-
53+
2854
self.updateHandler = updateHandler
29-
DispatchQueue.global().async {
55+
DispatchQueue.global(qos: .background).async {
3056
self.process(text: text)
3157
}
3258
}
33-
59+
3460
// MARK: Private methods
35-
61+
3662
private func process(text: String) {
37-
let text = text as NSString
63+
let text: NSString = text as NSString
3864
let characterSet = CharacterSet(charactersIn:"\r\"")
3965
var remainingRange = NSMakeRange(0, text.length)
4066

4167
rawMeasures.removeAll()
42-
68+
4369
processingDidStart()
44-
70+
4571
while true {
4672
let nextRange = text.rangeOfCharacter(from: characterSet, options: .literal, range: remainingRange)
4773
guard nextRange.location != NSNotFound else { break }
@@ -53,7 +79,7 @@ extension LogProcessorProtocol {
5379
defer {
5480
remainingRange = NSMakeRange(endIdx, remainingRange.upperBound - endIdx)
5581
}
56-
82+
5783
let range = NSMakeRange(beginIdx, textCount)
5884
guard let match = regex.firstMatch(in: text as String, options: [], range: range) else { continue }
5985
let timeString = text.substring(with: NSMakeRange(beginIdx, match.range.length - 4))
@@ -70,7 +96,7 @@ extension LogProcessorProtocol {
7096
}
7197
processingDidFinish()
7298
}
73-
99+
74100
fileprivate func updateResults(didComplete completed: Bool, didCancel: Bool) {
75101
DispatchQueue.global(qos: .userInteractive).async {
76102
let measures = self.rawMeasures.values
@@ -91,22 +117,22 @@ extension LogProcessorProtocol {
91117
}
92118
}
93119
}
94-
120+
95121
private func processResult(_ unprocessedResult: [RawMeasure]) -> [CompileMeasure] {
96122
let characterSet = CharacterSet(charactersIn:"\r\"")
97-
123+
98124
var result: [CompileMeasure] = []
99125
for entry in unprocessedResult {
100126
let code = entry.text.split(separator: "\t").map(String.init)
101127
let method = code.count >= 2 ? trimPrefixes(code[1]) : "-"
102-
128+
103129
if let path = code.first?.trimmingCharacters(in: characterSet), let measure = CompileMeasure(time: entry.time, rawPath: path, code: method, references: entry.references) {
104130
result.append(measure)
105131
}
106132
}
107133
return result
108134
}
109-
135+
110136
private func trimPrefixes(_ code: String) -> String {
111137
var code = code
112138
["@objc ", "final ", "@IBAction "].forEach { (prefix) in
@@ -117,31 +143,3 @@ extension LogProcessorProtocol {
117143
return code
118144
}
119145
}
120-
121-
class LogProcessor: NSObject, LogProcessorProtocol {
122-
123-
var rawMeasures: [String: RawMeasure] = [:]
124-
var updateHandler: CMUpdateClosure?
125-
var shouldCancel = false
126-
var timer: Timer?
127-
128-
func processingDidStart() {
129-
DispatchQueue.main.async {
130-
self.timer = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(self.timerCallback(_:)), userInfo: nil, repeats: true)
131-
}
132-
}
133-
134-
func processingDidFinish() {
135-
DispatchQueue.main.async {
136-
self.timer?.invalidate()
137-
self.timer = nil
138-
let didCancel = self.shouldCancel
139-
self.shouldCancel = false
140-
self.updateResults(didComplete: true, didCancel: didCancel)
141-
}
142-
}
143-
144-
@objc func timerCallback(_ timer: Timer) {
145-
updateResults(didComplete: false, didCancel: false)
146-
}
147-
}

0 commit comments

Comments
 (0)