Class: Jekyll::Webpack::Debouncer

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/webpack/debouncer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, debounce_config) ⇒ Debouncer

Returns a new instance of Debouncer.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jekyll/webpack/debouncer.rb', line 11

def initialize(site, debounce_config)
  @config = debounce_config
  @run_once = debounce_config.dig('run_once')
  @run_every_n = debounce_config.dig('every')
  @watch_nodes = debounce_config.dig('watch')

  if @watch_nodes
    if Array === @watch_nodes
      watch_paths = @watch_nodes.map { |file| File.join(site.source, file) }
    else
      watch_paths = [@watch_nodes]
    end

    @listener = Listen.to(*watch_paths) do |modified, added, removed|
      @has_run = false
    end
    @listener.start
  end

  @run_counter = 0
  @has_run = false
  @site = site
  @dist_tmpdir = Dir.mktmpdir("jekyll_webpack_dist_#{File.split(site.dest).last}")
  @dist_path = File.expand_path('dist', site.dest)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/jekyll/webpack/debouncer.rb', line 9

def config
  @config
end

#dist_pathObject (readonly)

Returns the value of attribute dist_path.



9
10
11
# File 'lib/jekyll/webpack/debouncer.rb', line 9

def dist_path
  @dist_path
end

#has_runObject (readonly)

Returns the value of attribute has_run.



9
10
11
# File 'lib/jekyll/webpack/debouncer.rb', line 9

def has_run
  @has_run
end

#listenerObject (readonly)

Returns the value of attribute listener.



9
10
11
# File 'lib/jekyll/webpack/debouncer.rb', line 9

def listener
  @listener
end

#run_every_nObject (readonly)

Returns the value of attribute run_every_n.



9
10
11
# File 'lib/jekyll/webpack/debouncer.rb', line 9

def run_every_n
  @run_every_n
end

#run_onceObject (readonly)

Returns the value of attribute run_once.



9
10
11
# File 'lib/jekyll/webpack/debouncer.rb', line 9

def run_once
  @run_once
end

#siteObject (readonly)

Returns the value of attribute site.



9
10
11
# File 'lib/jekyll/webpack/debouncer.rb', line 9

def site
  @site
end

#watch_nodesObject (readonly)

Returns the value of attribute watch_nodes.



9
10
11
# File 'lib/jekyll/webpack/debouncer.rb', line 9

def watch_nodes
  @watch_nodes
end

Instance Method Details

#buildObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jekyll/webpack/debouncer.rb', line 37

def build
  if run_once || watch_nodes
    if has_run
      restore_dist
    else
      yield
      extract_dist
    end
  elsif run_every_n
    if !has_run
      yield
      @run_counter += 1
      extract_dist
    elsif run_every_n == @run_counter
      yield
      @run_counter = 0
      extract_dist
    else
      restore_dist
      @run_counter += 1
    end
  end

  @has_run = true
end

#extract_distObject



63
64
65
# File 'lib/jekyll/webpack/debouncer.rb', line 63

def extract_dist
  FileUtils.cp_r(dist_path, @dist_tmpdir)
end

#restore_distObject



67
68
69
# File 'lib/jekyll/webpack/debouncer.rb', line 67

def restore_dist
  FileUtils.cp_r(File.join(@dist_tmpdir, 'dist'), File.join(site.dest, 'dist'))
end