Module: BubbleWrap::Reactor
- Defined in:
- motion/reactor.rb,
motion/reactor/queue.rb,
motion/reactor/timer.rb,
motion/reactor/future.rb,
motion/reactor/eventable.rb,
motion/reactor/deferrable.rb,
motion/reactor/periodic_timer.rb,
motion/reactor/default_deferrable.rb
Defined Under Namespace
Modules: Deferrable, Eventable, Future Classes: DefaultDeferrable, PeriodicTimer, Queue, Timer
Class Method Summary (collapse)
-
+ (Object) add_periodic_timer(interval, callback = nil, &blk)
Call `callback` or the passed block every `interval` seconds.
-
+ (Object) add_timer(interval, callback = nil, &blk)
Call `callback` or the passed block in `interval` seconds.
-
+ (Object) cancel_timer(timer)
Cancel a timer by passing in either a Timer object or a timer id (as returned by `add_timer` and `add_periodic_timer`).
-
+ (Object) defer(op = nil, cb = nil, &blk)
Defer is for integrating blocking operations into the reactor's control flow.
-
+ (Object) defer_on_main(op = nil, cb = nil, &blk)
A version of `defer` which schedules both the operator and callback operations on the application's main thread.
-
+ (Boolean) reactor_running?
(also: reactor_thread?)
Always returns true - for compatibility with EM.
-
+ (Object) schedule(*args, &blk)
Schedule a block for execution on the reactor queue.
-
+ (Object) schedule_on_main(*args, &blk)
Schedule a block for execution on your application's main thread.
Class Method Details
+ (Object) add_periodic_timer(interval, callback = nil, &blk)
Call `callback` or the passed block every `interval` seconds. Returns a timer signature that can be passed into `cancel_timer`
40 41 42 43 44 45 46 47 48 |
# File 'motion/reactor.rb', line 40 def add_periodic_timer(interval, callback=nil, &blk) @timers ||= {} timer = PeriodicTimer.new(interval,callback,&blk) timer.on(:cancelled) do @timers.delete(timer) end @timers[timer.object_id] = timer timer.object_id end |
+ (Object) add_timer(interval, callback = nil, &blk)
Call `callback` or the passed block in `interval` seconds. Returns a timer signature that can be passed into `cancel_timer`
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'motion/reactor.rb', line 14 def add_timer(interval, callback=nil, &blk) @timers ||= {} timer = Timer.new(interval,callback,&blk) timer.on(:fired) do @timers.delete(timer.object_id) end timer.on(:cancelled) do @timers.delete(timer.object_id) end @timers[timer.object_id] = timer timer.object_id end |
+ (Object) cancel_timer(timer)
Cancel a timer by passing in either a Timer object or a timer id (as returned by `add_timer` and `add_periodic_timer`).
30 31 32 33 34 35 |
# File 'motion/reactor.rb', line 30 def cancel_timer(timer) return timer.cancel if timer.respond_to?(:cancel) @timers ||= {} return @timers[timer].cancel if @timers[timer] false end |
+ (Object) defer(op = nil, cb = nil, &blk)
Defer is for integrating blocking operations into the reactor's control flow. Call defer with one or two blocks, the second block is optional.
operator = proc do
# perform a long running operation here
"result"
end
callback = proc do |result|
# do something with the result here, such as trigger a UI change
end
BubbleWrap::Reactor.defer(operation,callback)
The action of `defer` is to take the block specified in the first parameter (the "operation") and schedule it for asynchronous execution on a GCD concurrency queue. When the operation completes the result (if any) is passed into the callback (if present).
65 66 67 68 69 70 |
# File 'motion/reactor.rb', line 65 def defer(op=nil,cb=nil,&blk) schedule do result = (op||blk).call schedule(result, &cb) if cb end end |
+ (Object) defer_on_main(op = nil, cb = nil, &blk)
A version of `defer` which schedules both the operator and callback operations on the application's main thread.
74 75 76 77 78 79 |
# File 'motion/reactor.rb', line 74 def defer_on_main(op=nil,cb=nil,&blk) schedule_on_main do result = (op||blk).call schedule_on_main(result, &cb) if cb end end |
+ (Boolean) reactor_running? Also known as: reactor_thread?
Always returns true - for compatibility with EM
6 7 8 |
# File 'motion/reactor.rb', line 6 def reactor_running? true end |
+ (Object) schedule(*args, &blk)
Schedule a block for execution on the reactor queue.
82 83 84 85 86 87 88 89 90 |
# File 'motion/reactor.rb', line 82 def schedule(*args, &blk) @queue ||= ::Dispatch::Queue.concurrent("#{NSBundle.mainBundle.bundleIdentifier}.reactor") cb = proc do blk.call(*args) end @queue.async &cb nil end |
+ (Object) schedule_on_main(*args, &blk)
Schedule a block for execution on your application's main thread. This is useful as UI updates need to be executed from the main thread.
95 96 97 98 99 100 |
# File 'motion/reactor.rb', line 95 def schedule_on_main(*args, &blk) cb = proc do blk.call(*args) end ::Dispatch::Queue.main.async &cb end |