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)

Class Method Details

+ (Object) add_periodic_timer(interval, *args, &blk)

Call `callback` or the passed block every `interval` seconds. Returns a timer signature that can be passed into `cancel_timer` Optionally supply a callback as a second argument instead of a block (as per EventMachine API) Optionally supply :common_modes => true in args to schedule the timer for the runloop “common modes” (NSRunLoopCommonModes) instead of the default runloop mode.



45
46
47
48
49
50
51
52
53
# File 'motion/reactor.rb', line 45

def add_periodic_timer(interval, *args, &blk)
  @timers ||= {}
  timer = PeriodicTimer.new(interval,*args,&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.

operation = 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).



70
71
72
73
74
75
# File 'motion/reactor.rb', line 70

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.



79
80
81
82
83
84
# File 'motion/reactor.rb', line 79

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.



87
88
89
90
91
92
93
94
95
96
# File 'motion/reactor.rb', line 87

def schedule(*args, &blk)
  @queue ||= ::Dispatch::Queue.concurrent("#{NSBundle.mainBundle.bundleIdentifier}.reactor")
  blk.weak! if blk && BubbleWrap.use_weak_callbacks?

  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.



101
102
103
104
105
106
107
# File 'motion/reactor.rb', line 101

def schedule_on_main(*args, &blk)
  blk.weak! if blk && BubbleWrap.use_weak_callbacks?
  cb = proc do
    blk.call(*args)
  end
  ::Dispatch::Queue.main.async &cb
end