Class: QML::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin, Singleton
Defined in:
lib/qml/dispatcher.rb

Constant Summary collapse

MAX_DURATION =
1/10.to_r

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

Returns a new instance of Dispatcher.



12
13
14
15
# File 'lib/qml/dispatcher.rb', line 12

def initialize
  super
  @tasks = []
end

Instance Method Details

#add_task(&task) ⇒ Object



17
18
19
20
21
22
# File 'lib/qml/dispatcher.rb', line 17

def add_task(&task)
  synchronize do
    Kernel.set_event_loop_hook_enabled_later true if @tasks.empty?
    @tasks << task
  end
end

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/qml/dispatcher.rb', line 24

def empty?
  synchronize do
    @tasks.empty?
  end
end

#run_tasksObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/qml/dispatcher.rb', line 30

def run_tasks
  synchronize do
    start_time = Time.now
    loop do
      break if @tasks.empty?
      break if start_time.to_r - Time.now.to_r > MAX_DURATION
      task = @tasks.shift
      task.call
    end
    Kernel.set_event_loop_hook_enabled_later false if @tasks.empty?
  end
end