@@ -71,21 +71,16 @@ import CoreFoundation
71
71
}
72
72
}
73
73
74
- internal protocol WakeableRunLoop : AnyObject {
75
- func wakeUp( )
76
- }
77
-
78
74
private class RunLoopCallbackInfo {
79
- private var task : SafeTask
80
- private var runLoops : [ WakeableRunLoop ] = [ ]
75
+ private let task : SafeTask
76
+ private var runLoops : [ RunLoop ] = [ ]
81
77
82
- init ( _ task: SafeTask ) {
78
+ init ( _ task: SafeTask ) {
83
79
self . task = task
84
- }
80
+ }
85
81
86
82
func run( ) {
87
83
task ( )
88
- print ( " Task finished " )
89
84
}
90
85
}
91
86
@@ -134,9 +129,19 @@ import CoreFoundation
134
129
}
135
130
}
136
131
137
- init ( _ task: SafeTask , priority: Int = 0 ) {
138
- self . info = RunLoopCallbackInfo ( task)
139
- self . priority = priority
132
+ init ( _ task: SafeTask , priority: Int = 0 , runOnce: Bool = false ) {
133
+ self . priority = priority
134
+ if runOnce {
135
+ var stopTask : SafeTask ?
136
+ self . info = RunLoopCallbackInfo ( {
137
+ task ( )
138
+ stopTask ? ( )
139
+ } )
140
+ stopTask = { self . stop ( ) }
141
+ } else {
142
+ self . info = RunLoopCallbackInfo ( task)
143
+ }
144
+
140
145
}
141
146
142
147
deinit {
@@ -209,19 +214,7 @@ import CoreFoundation
209
214
}
210
215
}
211
216
212
- private class CFRunLoopWakeupHolder : WakeableRunLoop {
213
- private let loop : CFRunLoop !
214
-
215
- init ( loop: CFRunLoop ! ) {
216
- self . loop = loop
217
- }
218
-
219
- func wakeUp( ) {
220
- CFRunLoopWakeUp ( loop)
221
- }
222
- }
223
-
224
- class RunLoop : WakeableRunLoop {
217
+ class RunLoop {
225
218
private let cfRunLoop : CFRunLoop !
226
219
227
220
private var taskQueueSource : RunLoopTaskQueueSource ? = nil
@@ -283,7 +276,7 @@ import CoreFoundation
283
276
taskQueueLock. lock ( )
284
277
self . taskQueueSource = RunLoopTaskQueueSource ( )
285
278
286
- addSource ( taskQueueSource!, mode: RunLoop . defaultMode, retainLoop : false )
279
+ addSource ( taskQueueSource!, mode: RunLoop . defaultMode)
287
280
}
288
281
289
282
func stopTaskQueue( ) {
@@ -336,15 +329,11 @@ import CoreFoundation
336
329
while true { run ( ) }
337
330
}
338
331
339
- func addSource( rls: RunLoopSource , mode: NSString , retainLoop : Bool = true ) {
332
+ func addSource( rls: RunLoopSource , mode: NSString ) {
340
333
let crls = unsafeBitCast ( rls. cfObject, CFRunLoopSource . self)
341
334
if CFRunLoopSourceIsValid ( crls) {
342
335
CFRunLoopAddSource ( cfRunLoop, crls, mode. cfString)
343
- if retainLoop {
344
- rls. info. runLoops. append ( self )
345
- } else {
346
- rls. info. runLoops. append ( CFRunLoopWakeupHolder ( loop: cfRunLoop) )
347
- }
336
+ rls. info. runLoops. append ( self )
348
337
wakeUp ( )
349
338
}
350
339
}
@@ -366,14 +355,9 @@ import CoreFoundation
366
355
if let queue = taskQueueSource {
367
356
queue. addTask ( task)
368
357
} else {
369
- var source : RunLoopSource ? = nil
370
- let stask = {
371
- print ( " Task without queue run " )
372
- task ( )
373
- source? . stop ( )
374
- }
375
- source = RunLoopSource ( stask, priority: 0 )
376
- addSource ( source!, mode: RunLoop . defaultMode)
358
+ let source = RunLoopSource ( task, priority: 0 , runOnce: true )
359
+ addSource ( source, mode: RunLoop . defaultMode)
360
+ source. signal ( )
377
361
}
378
362
}
379
363
0 commit comments