class Gotta::Mod::Hub
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.
Attributes
loaded_mods[R]
mods[R]
queue[R]
Public Class Methods
new(queue)
click to toggle source
# 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
Public Instance Methods
add_dependency(dep)
click to toggle source
# File lib/gotta/mod/hub.rb, line 62 def add_dependency(dep) @dependencies ||= Set.new @dependencies << dep end
check_dependencies()
click to toggle source
# 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)
click to toggle source
Add mods to certain events on a certain path. 'path' is relative to the project folder 'on' is :added or :modified or :removed
# 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
start()
click to toggle source
# 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
Private Instance Methods
get_matching_paths_for(event)
click to toggle source
Check if the @mods hash has a key with the file/directory path, a separator and the event type. `event.type` can be :added or :modified or :removed
# File lib/gotta/mod/hub.rb, line 96 def get_matching_paths_for(event) # puts "MONITORED? #{event.path}#{@key_separator}#{event.type}" @mutex.synchronize { @paths.map do |p| # puts "#{p} -> #{event.path} (#{File.fnmatch(p, event.path)})" next unless File.fnmatch(p, event.path) "#{p}#{@key_separator}#{event.type}" end.compact } end
run_mods(event, keys)
click to toggle source
Execute the mod code in a separate thread
# File lib/gotta/mod/hub.rb, line 77 def run_mods(event, keys) # puts "Running mods for event #{event.inspect}" @mutex.synchronize { keys.each do |key| # puts "Processing key #{key}" next unless @mods[key] @threads << @mods[key].map do |blk, mod_name| Thread.new do # puts "Running mod #{mod_name}" blk.call(event) end end end } end