Class: Processing::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-processing/runners/watch.rb

Overview

A sketch loader, observer, and reloader, to tighten the feedback between code and effect.

Instance Method Summary collapse

Constructor Details

#initialize(sketch) ⇒ Watcher

Sic a new Processing::Watcher on a given sketch.



10
11
12
13
14
15
# File 'lib/ruby-processing/runners/watch.rb', line 10

def initialize(sketch)
  @file = sketch
  @time = Time.now
  load @file
  start_watching
end

Instance Method Details

#start_watchingObject

Kicks off a thread to watch the sketch, reloading Ruby-Processing and restarting the sketch whenever it changes.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby-processing/runners/watch.rb', line 19

def start_watching
  thread = Thread.start do
    loop do
      file_mtime = File.stat(@file).mtime
      if file_mtime > @time
        @time = file_mtime
        Processing::App.wipe_out_current_app!
        GC.start
        begin
          load @file
        rescue SyntaxError
          print "Syntax Error in your sketch: ", $!, "\n"
        end
      end
      sleep 0.1
    end
  end
  thread.join
end