Class: Viewing::ViewProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/view_proxy.rb

Overview

ViewProxy class threading proxy

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ ViewProxy

initialization

Parameters:

  • obj (Object)

    Target object



18
19
20
21
22
23
# File 'lib/view_proxy.rb', line 18

def initialize(obj)
  @obj = obj
  @tg = ThreadGroup.new
  @upd_mon = Monitor.new
  @update = @upd_mon.new_cond
end

Instance Method Details

#closeObject

close call



64
65
66
67
68
# File 'lib/view_proxy.rb', line 64

def close
  @obj.close if @obj.respond_to?('close')
  @thread.raise(StopError, 'stop')
  @thread.join
end

#store(key, values, options) ⇒ Object

thread-safe store call

Parameters:

  • key (String)

    Value in first cell of data row

  • values (Array)

    Values in cells of row

  • options (Hash)

    Options: color styles



30
31
32
33
34
35
# File 'lib/view_proxy.rb', line 30

def store(key,values,options)
  @upd_mon.synchronize {
    @obj.store(key,values,options) if @obj.respond_to?('store')
    @update.signal
  }
end

#update(name = nil) ⇒ Object

update loop

Parameters:

  • name (String) (defaults to: nil)

    name of viewing thread



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

def update(name=nil)
  @thread = Thread.new {
    Thread.current[:name] = (name || "thread #{Thread.current.object_id}")
    loop {
      begin
        @upd_mon.synchronize {
          @update.wait(1)
          Thread.handle_interrupt(RuntimeError => :on_blocking) {
            @obj.update if @obj.respond_to?('update')
            Thread.handle_interrupt(StopError => :immediate) {} if Thread.pending_interrupt?
          }
        }
        Thread.pass
      rescue StopError
        puts "#{Thread.current[:name]}\n  #{Thread.current.inspect}"
        Thread.current.exit
      rescue
        sleep(1)
      end
    }
  }
end