Class: Processing::Watcher
- Inherits:
-
Object
- Object
- Processing::Watcher
- Defined in:
- lib/ruby-processing/runners/watch.rb
Overview
A sketch loader, observer, and reloader, to tighten the feedback between code and effect.
Constant Summary collapse
- SLEEP_TIME =
0.2
Instance Method Summary collapse
-
#initialize ⇒ Watcher
constructor
A new instance of Watcher.
- #reload_files_to_watch ⇒ Object
-
#report_errors ⇒ Object
Convenience function to report errors when loading and running a sketch, instead of having them eaten by the thread they are loaded in.
- #start_runner ⇒ Object
-
#start_watching ⇒ Object
Kicks off a thread to watch the sketch, reloading Ruby-Processing and restarting the sketch whenever it changes.
Constructor Details
#initialize ⇒ Watcher
Returns a new instance of Watcher.
18 19 20 21 22 |
# File 'lib/ruby-processing/runners/watch.rb', line 18 def initialize reload_files_to_watch @time = Time.now start_watching end |
Instance Method Details
#reload_files_to_watch ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/ruby-processing/runners/watch.rb', line 61 def reload_files_to_watch @files = Dir.glob(File.join(SKETCH_ROOT, '**/*.{rb,glsl}')) count = @files.length max_watch = RP_CONFIG.fetch('MAX_WATCH', 20) return unless count > max_watch warn format(WATCH_MESSAGE, max_watch, count) abort end |
#report_errors ⇒ Object
Convenience function to report errors when loading and running a sketch, instead of having them eaten by the thread they are loaded in.
43 44 45 46 47 48 49 50 |
# File 'lib/ruby-processing/runners/watch.rb', line 43 def report_errors yield rescue Exception => e wformat = 'Exception occured while running sketch %s...' tformat = "Backtrace:\n\t%s" warn format(wformat, File.basename(SKETCH_PATH)) puts format(tformat, e.backtrace.join("\n\t")) end |
#start_runner ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/ruby-processing/runners/watch.rb', line 52 def start_runner @runner.kill if @runner && @runner.alive? @runner = Thread.start do report_errors do Processing.load_and_run_sketch end end end |
#start_watching ⇒ Object
Kicks off a thread to watch the sketch, reloading Ruby-Processing and restarting the sketch whenever it changes.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruby-processing/runners/watch.rb', line 26 def start_watching start_runner Kernel.loop do if @files.find { |file| FileTest.exist?(file) && File.stat(file).mtime > @time } puts 'reloading sketch...' $app && $app.close @time = Time.now java.lang.System.gc start_runner reload_files_to_watch end sleep SLEEP_TIME end end |