Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/delay #207

Merged
merged 5 commits into from Mar 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions motion/ruby_motion_query/app.rb
Expand Up @@ -83,6 +83,17 @@ def document_path
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
end

# @return [NSTimer] NSTimer instance, fires once after <seconds>
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 <seconds>
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
#
Expand Down
25 changes: 25 additions & 0 deletions spec/app.rb
Expand Up @@ -44,6 +44,31 @@
@app.document_path.should == NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
end

it 'should delay 0.01 second then fire callback' do
before_time = Time.now
@app.after 0.01 do
after_time = Time.now
(after_time - before_time).should.be >= 0.01
(after_time - before_time).should.be.close 0.01, 0.005
resume
end
wait {}
end

it 'should fire twice 0.01 seconds apart' do
before_time = Time.now
count = 0
timer = @app.every 0.01 do
after_time = Time.now
(after_time - before_time).should.be >= 0.01
(after_time - before_time).should.be.close 0.01, 0.005
count += 1
before_time = after_time # 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
Expand Down