Class: Gotta::Mod::Hub

Inherits:
Object
  • Object
show all
Defined in:
lib/gotta/mod/hub.rb

Overview

Starts a new thread and goes on a blocking operation to constantly pop values from the event queue. If the file changed resides within a directory that is being monitored by the hub, all the mods that monitor that file will be executed. The execution of the mod code is done asynchronously on a separate thread.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ Hub

Returns a new instance of Hub.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gotta/mod/hub.rb', line 24

def initialize(queue)
  # This is the separator to generate the key
  # for the mods hash.
  @key_separator = ":::"
  @queue = queue
  @mutex = Mutex.new
  @mods = {}
  @loaded_mods = Set.new
  @paths = Set.new
  @threads = Set.new
end

Instance Attribute Details

#loaded_modsObject (readonly)

Returns the value of attribute loaded_mods.



23
24
25
# File 'lib/gotta/mod/hub.rb', line 23

def loaded_mods
  @loaded_mods
end

#modsObject (readonly)

Returns the value of attribute mods.



23
24
25
# File 'lib/gotta/mod/hub.rb', line 23

def mods
  @mods
end

#queueObject (readonly)

Returns the value of attribute queue.



23
24
25
# File 'lib/gotta/mod/hub.rb', line 23

def queue
  @queue
end

Instance Method Details

#add_dependency(dep) ⇒ Object



62
63
64
65
# File 'lib/gotta/mod/hub.rb', line 62

def add_dependency(dep)
  @dependencies ||= Set.new
  @dependencies << dep
end

#check_dependenciesObject



67
68
69
70
71
72
73
# File 'lib/gotta/mod/hub.rb', line 67

def check_dependencies
  @dependencies.each do |deps|
    source, dep = deps.to_a[0]
    next if @loaded_mods.include?(dep)
    raise MissingDependencyError.new(source, dep)
  end
end

#register_mod(mod_name:, path:, on:, &block) ⇒ Object

Add mods to certain events on a certain path. ‘path’ is relative to the project folder ‘on’ is :added or :modified or :removed



53
54
55
56
57
58
59
60
# File 'lib/gotta/mod/hub.rb', line 53

def register_mod(mod_name:, path:, on:, &block)
  key = "#{path}#{@key_separator}#{on}"
  @mutex.synchronize {
    @mods[key] ||= []
    @mods[key] << [block, mod_name]
    @paths << path
  }
end

#startObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gotta/mod/hub.rb', line 36

def start
  loop do
    begin
      event = queue.pop
      keys = get_matching_paths_for(event)
      next unless keys.any?
      run_mods(event, keys)
    rescue StandardError => e
      puts e.full_message
      next
    end
  end
end