Sha256: 54f1f7ee8e0ede70f9f0b31d87e2ca0433386e270281adf4b9ac1364d7c998aa

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module Simple::Httpd::Reloader
  def self.attach(target, paths:, reloading_instance: target)
    target.extend self
    target.load!(paths: paths, reloading_instance: reloading_instance)
    target
  end

  H = ::Simple::Httpd::Helpers

  attr_accessor :reloading_paths

  def load!(paths:, reloading_instance:)
    paths = Array(paths)
    paths = nil if paths.empty?

    @__reload_paths__ = paths
    @__reloading_instance__ = reloading_instance

    reload_all_changed_files
  end

  def reload!
    # if this is a class, and its superclass is also reloadable,
    # reload the superclass first.
    if respond_to?(:superclass) && superclass&.respond_to?(:reload!)
      superclass.reload!
    end

    reload_all_changed_files
  end

  private

  def reload_all_changed_files
    return unless @__reload_paths__

    @__reload_paths__.each do |path|
      reload_file_if_necessary(path)
    end
  end

  def reload_file_if_necessary(path)
    @__source_mtimes_by_path__ ||= {}

    mtime = File.mtime(path)
    return if @__source_mtimes_by_path__[path] == mtime

    Simple::Httpd.logger.debug do
      verb = @__source_mtimes_by_path__.key?(path) ? "reloading" : "loading"
      "#{verb} #{H.shorten_path path}"
    end

    if @__reloading_instance__
      @__reloading_instance__.instance_eval File.read(path), path, 1
    else
      load path
    end

    @__source_mtimes_by_path__[path] = mtime
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple-httpd-0.3.5 lib/simple/httpd/reloader.rb