@@ -52,27 +52,33 @@ import CoreFoundation
5252
5353 private protocol RunLoopCallback {
5454 var info : RunLoopCallbackInfo { get }
55- var cfObject : AnyObject { get }
55+ var cfObject : AnyObject { mutating get }
5656 }
5757
5858 struct RunLoopSource : RunLoopCallback {
5959 private let info : RunLoopCallbackInfo
6060 private let priority : Int
61+ private var _source : CFRunLoopSource ! = nil
6162
6263 private var cfObject : AnyObject {
63- var context = CFRunLoopSourceContext (
64- version: 0 ,
65- info: UnsafeMutablePointer < Void > ( Unmanaged . passUnretained ( info) . toOpaque ( ) ) ,
66- retain: runLoopCallbackInfoRetain,
67- release: runLoopCallbackInfoRelease,
68- copyDescription: nil ,
69- equal: nil ,
70- hash: nil ,
71- schedule: nil ,
72- cancel: nil ,
73- perform: runLoopCallbackInfoRun
74- )
75- return CFRunLoopSourceCreate ( nil , priority, & context)
64+ mutating get {
65+ if _source == nil {
66+ var context = CFRunLoopSourceContext (
67+ version: 0 ,
68+ info: UnsafeMutablePointer < Void > ( Unmanaged . passUnretained ( info) . toOpaque ( ) ) ,
69+ retain: runLoopCallbackInfoRetain,
70+ release: runLoopCallbackInfoRelease,
71+ copyDescription: nil ,
72+ equal: nil ,
73+ hash: nil ,
74+ schedule: nil ,
75+ cancel: nil ,
76+ perform: runLoopCallbackInfoRun
77+ )
78+ _source = CFRunLoopSourceCreate ( nil , priority, & context)
79+ }
80+ return _source
81+ }
7682 }
7783
7884 init ( _ task: SafeTask , priority: Int = 0 ) {
@@ -88,16 +94,22 @@ import CoreFoundation
8894 struct RunLoopDelay : RunLoopCallback {
8995 private let info : RunLoopCallbackInfo
9096 private let delay : Double
97+ private var _timer : CFRunLoopTimer ! = nil
9198
9299 private var cfObject : AnyObject {
93- var context = CFRunLoopTimerContext (
94- version: 0 ,
95- info: UnsafeMutablePointer < Void > ( Unmanaged . passUnretained ( info) . toOpaque ( ) ) ,
96- retain: runLoopCallbackInfoRetain,
97- release: runLoopCallbackInfoRelease,
98- copyDescription: nil
99- )
100- return CFRunLoopTimerCreate ( nil , CFAbsoluteTimeGetCurrent ( ) + delay, - 1 , 0 , 0 , timerRunCallback, & context)
100+ mutating get {
101+ if _timer == nil {
102+ var context = CFRunLoopTimerContext (
103+ version: 0 ,
104+ info: UnsafeMutablePointer < Void > ( Unmanaged . passUnretained ( info) . toOpaque ( ) ) ,
105+ retain: runLoopCallbackInfoRetain,
106+ release: runLoopCallbackInfoRelease,
107+ copyDescription: nil
108+ )
109+ _timer = CFRunLoopTimerCreate ( nil , CFAbsoluteTimeGetCurrent ( ) + delay, - 1 , 0 , 0 , timerRunCallback, & context)
110+ }
111+ return _timer
112+ }
101113 }
102114
103115 init ( _ task: SafeTask , delay: Double ) {
@@ -155,14 +167,14 @@ import CoreFoundation
155167 while true { run ( ) }
156168 }
157169
158- func addSource( rls: RunLoopSource , mode: NSString ) {
170+ func addSource( var rls: RunLoopSource , mode: NSString ) {
159171 rls. info. runLoops. append ( self )
160172 CFRunLoopAddSource ( cfRunLoop, unsafeBitCast ( rls. cfObject, CFRunLoopSource . self) , mode. cfString)
161173 CFRunLoopSourceSignal ( unsafeBitCast ( rls. cfObject, CFRunLoopSource . self) )
162174 CFRunLoopWakeUp ( cfRunLoop)
163175 }
164176
165- func addDelay( rld: RunLoopDelay , mode: NSString ) {
177+ func addDelay( var rld: RunLoopDelay , mode: NSString ) {
166178 rld. info. runLoops. append ( self )
167179 CFRunLoopAddTimer ( cfRunLoop, unsafeBitCast ( rld. cfObject, CFRunLoopTimer . self) , mode. cfString)
168180 CFRunLoopWakeUp ( cfRunLoop)
0 commit comments