diff --git a/motion/ruby_motion_query/app.rb b/motion/ruby_motion_query/app.rb index 4a18552..702927c 100644 --- a/motion/ruby_motion_query/app.rb +++ b/motion/ruby_motion_query/app.rb @@ -83,6 +83,17 @@ module RubyMotionQuery NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0] end + # @return [NSTimer] NSTimer instance, fires once after + def after(seconds, &callback) + NSTimer.scheduledTimerWithTimeInterval(seconds, target: callback, selector: 'call:', userInfo: nil, repeats: false) + end + alias delay after + + # @return [NSTimer] NSTimer instance, set to repeat every + def every(seconds, &callback) + NSTimer.scheduledTimerWithTimeInterval(seconds, target: callback, selector: 'call:', userInfo: nil, repeats: true) + end + # Returns the current view controller in the app. If the current controller is a tab or # navigation controller, then it gets the current tab or topmost controller in the nav, etc # diff --git a/spec/app.rb b/spec/app.rb index 882392b..8de4405 100644 --- a/spec/app.rb +++ b/spec/app.rb @@ -44,6 +44,31 @@ describe 'app' do @app.document_path.should == NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0] end + it 'should delay 0.1 second then fire callback' do + before = Time.now + @app.after 0.1 do + after = Time.now + (after - before).should.be > 0.09 + (after - before).should.be < 0.11 + resume + end + wait {} + end + + it 'should fire twice 0.01 seconds apart' do + before = Time.now + count = 0 + timer = @app.every 0.01 do + after = Time.now + (after - before).should.be > 0.009 + (after - before).should.be < 0.011 + count += 1 + before = after # reset + resume if count >= 2 + end + wait { timer.invalidate } + end + describe 'environment' do it 'should return environment as symbol, :test in this case' do @app.environment.should == :test