user187676
user187676

Reputation:

Guard: batch watch notifications

Is it possible to batch guard's watch notifications?

For example, if a subfolder is moved, watch emits an event for every file. What I actually want is one notification, not several, if something changes.

Upvotes: 5

Views: 223

Answers (1)

gldnspud
gldnspud

Reputation: 421

Although this doesn't batch changes and therefore doesn't have all the benefits of doing so, if all you need to do is prevent multiple builds after several files are saved at once, you can use a debounce mechanism such as the following:

LAST_REBUILD = Time.now
DEBOUNCE = 2 #seconds
guard :shell do
  watch(%r{PATTERN}) do |m|
    since_last = Time.now - LAST_REBUILD
    if since_last > DEBOUNCE
      n 'Rebuilding'
      if system('LONG_RUNNING_PROCESS')
        n 'Build complete'
        LAST_REBUILD = Time.now
      end
    else
      n "Skipping rebuild after only #{since_last} seconds"
  end
end

Upvotes: 1

Related Questions