11#if !os(WASI)
22
3+ import CoreFoundation
34import Dispatch
45import Foundation
56
@@ -156,7 +157,7 @@ internal class AwaitPromiseBuilder<T> {
156157 self . trigger = trigger
157158 }
158159
159- func timeout( _ timeoutInterval: NimbleTimeInterval , forcefullyAbortTimeout: NimbleTimeInterval ) -> Self {
160+ func timeout( _ timeoutInterval: NimbleTimeInterval , forcefullyAbortTimeout: NimbleTimeInterval , isContinuous : Bool ) -> Self {
160161 /// = Discussion =
161162 ///
162163 /// There's a lot of technical decisions here that is useful to elaborate on. This is
@@ -195,24 +196,33 @@ internal class AwaitPromiseBuilder<T> {
195196 let timedOutSem = DispatchSemaphore ( value: 0 )
196197 let semTimedOutOrBlocked = DispatchSemaphore ( value: 0 )
197198 semTimedOutOrBlocked. signal ( )
198- let runLoop = RunLoop . main
199- runLoop. perform ( inModes: [ . default] , block: {
199+ let runLoop = CFRunLoopGetMain ( )
200+ #if canImport(Darwin)
201+ let runLoopMode = CFRunLoopMode . defaultMode. rawValue
202+ #else
203+ let runLoopMode = kCFRunLoopDefaultMode
204+ #endif
205+ CFRunLoopPerformBlock ( runLoop, runLoopMode) {
200206 if semTimedOutOrBlocked. wait ( timeout: . now( ) ) == . success {
201207 timedOutSem. signal ( )
202208 semTimedOutOrBlocked. signal ( )
203209 if self . promise. resolveResult ( . timedOut) {
204- RunLoop . main . stop ( )
210+ CFRunLoopStop ( CFRunLoopGetMain ( ) )
205211 }
206212 }
207- } )
213+ }
208214 // potentially interrupt blocking code on run loop to let timeout code run
209- runLoop . stop ( )
215+ CFRunLoopStop ( runLoop )
210216 let now = DispatchTime . now ( ) + forcefullyAbortTimeout. dispatchTimeInterval
211217 let didNotTimeOut = timedOutSem. wait ( timeout: now) != . success
212218 let timeoutWasNotTriggered = semTimedOutOrBlocked. wait ( timeout: . now( ) ) == . success
213219 if didNotTimeOut && timeoutWasNotTriggered {
214- if self . promise. resolveResult ( . blockedRunLoop) {
215- runLoop. stop ( )
220+ if self . promise. resolveResult ( isContinuous ? . timedOut : . blockedRunLoop) {
221+ #if canImport(CoreFoundation)
222+ CFRunLoopStop ( CFRunLoopGetMain ( ) )
223+ #else
224+ RunLoop . main. _stop ( )
225+ #endif
216226 }
217227 }
218228 }
@@ -300,7 +310,11 @@ internal class Awaiter {
300310 if completionCount < 2 {
301311 func completeBlock( ) {
302312 if promise. resolveResult ( . completed( result) ) {
303- RunLoop . main. stop ( )
313+ #if canImport(CoreFoundation)
314+ CFRunLoopStop ( CFRunLoopGetMain ( ) )
315+ #else
316+ RunLoop . main. _stop ( )
317+ #endif
304318 }
305319 }
306320
@@ -338,12 +352,20 @@ internal class Awaiter {
338352 do {
339353 if let result = try closure ( ) {
340354 if promise. resolveResult ( . completed( result) ) {
341- RunLoop . current. stop ( )
355+ #if canImport(CoreFoundation)
356+ CFRunLoopStop ( CFRunLoopGetCurrent ( ) )
357+ #else
358+ RunLoop . current. _stop ( )
359+ #endif
342360 }
343361 }
344362 } catch let error {
345363 if promise. resolveResult ( . errorThrown( error) ) {
346- RunLoop . current. stop ( )
364+ #if canImport(CoreFoundation)
365+ CFRunLoopStop ( CFRunLoopGetCurrent ( ) )
366+ #else
367+ RunLoop . current. _stop ( )
368+ #endif
347369 }
348370 }
349371 }
@@ -363,6 +385,7 @@ internal func pollBlock(
363385 timeoutInterval: NimbleTimeInterval ,
364386 sourceLocation: SourceLocation ,
365387 fnName: String = #function,
388+ isContinuous: Bool ,
366389 expression: @escaping ( ) throws -> PollStatus ) -> PollResult < Bool > {
367390 let awaiter = NimbleEnvironment . activeInstance. awaiter
368391 let result = awaiter. poll ( pollInterval) { ( ) throws -> Bool ? in
@@ -371,29 +394,10 @@ internal func pollBlock(
371394 }
372395 return nil
373396 }
374- . timeout ( timeoutInterval, forcefullyAbortTimeout: timeoutInterval. divided)
397+ . timeout ( timeoutInterval, forcefullyAbortTimeout: timeoutInterval. divided, isContinuous : isContinuous )
375398 . wait ( fnName, sourceLocation: sourceLocation)
376399
377400 return result
378401}
379402
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-
399403#endif // #if !os(WASI)
0 commit comments