Created
October 20, 2015 09:34
-
-
Save tarunon/fb0889a752d6702c3165 to your computer and use it in GitHub Desktop.
Takahashi-meijin can push a button 16 times per 1 second.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class UTContainer<T> { | |
var value: T { | |
didSet { | |
if self.fired { | |
self.fired = false | |
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: NSBlockOperation { [weak self] in | |
guard let strongSelf = self else { | |
return | |
} | |
strongSelf.fired = true | |
strongSelf.handler(strongSelf.value) | |
}, selector: "main", userInfo: nil, repeats: false) | |
} else { | |
self.timer?.fireDate = NSDate(timeIntervalSinceNow: 1.0) | |
} | |
} | |
} | |
private var timer: NSTimer? | |
private var handler: T -> () | |
private var fired = true | |
init(_ value: T, handler: T -> ()) { | |
self.value = value | |
self.handler = handler | |
} | |
} |
Author
tarunon
commented
Oct 20, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment