Skip to content

Commit ae0648d

Browse files
authored
Remove duplicated runloop code (#1193)
1 parent 4cde9c5 commit ae0648d

File tree

4 files changed

+25
-55
lines changed

4 files changed

+25
-55
lines changed

Sources/Nimble/Utils/AsyncAwait.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#if !os(WASI)
22

3-
#if canImport(CoreFoundation)
4-
import CoreFoundation
5-
#endif
6-
73
import Dispatch
84
import Foundation
95

Sources/Nimble/Utils/AsyncTimerSequence.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#if !os(WASI)
22

3-
#if canImport(CoreFoundation)
4-
import CoreFoundation
5-
#endif
63
import Dispatch
74
import Foundation
85

Sources/Nimble/Utils/PollAwait.swift

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#if !os(WASI)
22

3-
#if canImport(CoreFoundation)
4-
import CoreFoundation
5-
#endif
63
import Dispatch
74
import Foundation
85

@@ -198,48 +195,24 @@ internal class AwaitPromiseBuilder<T> {
198195
let timedOutSem = DispatchSemaphore(value: 0)
199196
let semTimedOutOrBlocked = DispatchSemaphore(value: 0)
200197
semTimedOutOrBlocked.signal()
201-
#if canImport(CoreFoundation)
202-
let runLoop = CFRunLoopGetMain()
203-
#if canImport(Darwin)
204-
let runLoopMode = CFRunLoopMode.defaultMode.rawValue
205-
#else
206-
let runLoopMode = kCFRunLoopDefaultMode
207-
#endif
208-
CFRunLoopPerformBlock(runLoop, runLoopMode) {
209-
if semTimedOutOrBlocked.wait(timeout: .now()) == .success {
210-
timedOutSem.signal()
211-
semTimedOutOrBlocked.signal()
212-
if self.promise.resolveResult(.timedOut) {
213-
CFRunLoopStop(CFRunLoopGetMain())
214-
}
215-
}
216-
}
217-
// potentially interrupt blocking code on run loop to let timeout code run
218-
CFRunLoopStop(runLoop)
219-
#else
220198
let runLoop = RunLoop.main
221199
runLoop.perform(inModes: [.default], block: {
222200
if semTimedOutOrBlocked.wait(timeout: .now()) == .success {
223201
timedOutSem.signal()
224202
semTimedOutOrBlocked.signal()
225203
if self.promise.resolveResult(.timedOut) {
226-
RunLoop.main._stop()
204+
RunLoop.main.stop()
227205
}
228206
}
229207
})
230208
// potentially interrupt blocking code on run loop to let timeout code run
231-
runLoop._stop()
232-
#endif
209+
runLoop.stop()
233210
let now = DispatchTime.now() + forcefullyAbortTimeout.dispatchTimeInterval
234211
let didNotTimeOut = timedOutSem.wait(timeout: now) != .success
235212
let timeoutWasNotTriggered = semTimedOutOrBlocked.wait(timeout: .now()) == .success
236213
if didNotTimeOut && timeoutWasNotTriggered {
237214
if self.promise.resolveResult(.blockedRunLoop) {
238-
#if canImport(CoreFoundation)
239-
CFRunLoopStop(CFRunLoopGetMain())
240-
#else
241-
RunLoop.main._stop()
242-
#endif
215+
runLoop.stop()
243216
}
244217
}
245218
}
@@ -327,11 +300,7 @@ internal class Awaiter {
327300
if completionCount < 2 {
328301
func completeBlock() {
329302
if promise.resolveResult(.completed(result)) {
330-
#if canImport(CoreFoundation)
331-
CFRunLoopStop(CFRunLoopGetMain())
332-
#else
333-
RunLoop.main._stop()
334-
#endif
303+
RunLoop.main.stop()
335304
}
336305
}
337306

@@ -369,20 +338,12 @@ internal class Awaiter {
369338
do {
370339
if let result = try closure() {
371340
if promise.resolveResult(.completed(result)) {
372-
#if canImport(CoreFoundation)
373-
CFRunLoopStop(CFRunLoopGetCurrent())
374-
#else
375-
RunLoop.current._stop()
376-
#endif
341+
RunLoop.current.stop()
377342
}
378343
}
379344
} catch let error {
380345
if promise.resolveResult(.errorThrown(error)) {
381-
#if canImport(CoreFoundation)
382-
CFRunLoopStop(CFRunLoopGetCurrent())
383-
#else
384-
RunLoop.current._stop()
385-
#endif
346+
RunLoop.current.stop()
386347
}
387348
}
388349
}
@@ -416,4 +377,23 @@ internal func pollBlock(
416377
return result
417378
}
418379

380+
#if canImport(CoreFoundation)
381+
import CoreFoundation
382+
383+
extension RunLoop {
384+
func stop() {
385+
CFRunLoopStop(getCFRunLoop())
386+
}
387+
}
388+
389+
#else
390+
391+
extension RunLoop {
392+
func stop() {
393+
_stop()
394+
}
395+
}
396+
397+
#endif
398+
419399
#endif // #if !os(WASI)

Tests/NimbleTests/PollingTest+Require.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#if !os(WASI)
22

33
import Dispatch
4-
#if canImport(CoreFoundation)
5-
import CoreFoundation
6-
#endif
74
import Foundation
85
import XCTest
96
import Nimble

0 commit comments

Comments
 (0)