motion/reactor/periodic_timer.rb in bubble-wrap-1.3.0 vs motion/reactor/periodic_timer.rb in bubble-wrap-1.4.0

- old
+ new

@@ -6,26 +6,30 @@ attr_accessor :interval # Create a new timer that fires after a given number of seconds def initialize(interval, *args, &blk) - options = args.last.is_a?(Hash) ? args.last : {} callback = args.first.respond_to?(:call) ? args.first : blk raise ArgumentError, "No callback or block supplied to periodic timer" unless callback - + + options = args.last.is_a?(Hash) ? args.last : {} + if options[:common_modes] + NSLog "[DEPRECATED - Option :common_modes] a Run Loop Mode is no longer needed." + end + self.interval = interval - fire = proc { + + leeway = interval + queue = Dispatch::Queue.current + @timer = Dispatch::Source.timer(leeway, interval, 0.0, queue) do callback.call trigger(:fired) - } - @timer = NSTimer.timerWithTimeInterval(interval, target: fire, selector: 'call:', userInfo: nil, repeats: true) - runloop_mode = options[:common_modes] ? NSRunLoopCommonModes : NSDefaultRunLoopMode - NSRunLoop.currentRunLoop.addTimer(@timer, forMode: runloop_mode) + end end # Cancel the timer def cancel - @timer.invalidate + @timer.cancel! trigger(:cancelled) end end end