Module: Redom::Connection

Includes:
Utils
Defined in:
lib/redom/connection.rb

Defined Under Namespace

Classes: Sender

Instance Method Summary collapse

Methods included from Utils

#_dispatcher, #_logger, dispatcher=, logger=

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blck) ⇒ Object



36
37
38
# File 'lib/redom/connection.rb', line 36

def method_missing(name, *args, &blck)
  window.method_missing(name, *args, &blck)
end

Instance Method Details

#_bulk(proxy) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/redom/connection.rb', line 54

def _bulk(proxy)
  fid = _fid
  stack = @proxies[fid] || @proxies[fid] = Hash.new
  stack[proxy._info[P_INFO_OID]] = proxy
  if stack.size == @buff_size
    sync
  end
end

#_cidObject



132
133
134
# File 'lib/redom/connection.rb', line 132

def _cid
  "#{@ws.__id__}@#{__id__}"
end

#_fidObject



136
137
138
# File 'lib/redom/connection.rb', line 136

def _fid
  "F#{Fiber.current.__id__}"
end

#_init(ws, opts) ⇒ Object

Initialization



45
46
47
48
49
50
51
52
# File 'lib/redom/connection.rb', line 45

def _init(ws, opts)
  @ws = ws
  @proxies = Hash.new
  @buff_size = opts[:buff_size]
  @sync = Sender.new(self, false)
  @async = Sender.new(self, true)
  self
end

#_on_openObject



127
128
129
130
# File 'lib/redom/connection.rb', line 127

def _on_open
  _bulk Proxy.new(self, [nil, nil, nil, _cid])
  on_open
end

#_send(msg) ⇒ Object



140
141
142
143
# File 'lib/redom/connection.rb', line 140

def _send(msg)
  _logger.debug "Message sent. ID='#{@ws.__id__}'\n#{msg}"
  @ws.send msg
end

#async(block = nil, &blck) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/redom/connection.rb', line 111

def async(block = nil, &blck)
  return @async unless block || blck

  if block
    block.call
  else
    _dispatcher.run_task(self, :async, blck)
  end

  nil
end

#connections(filter = self.class) ⇒ Object



40
41
42
# File 'lib/redom/connection.rb', line 40

def connections(filter = self.class)
  _dispatcher.connections(filter)
end

#documentObject



32
33
34
# File 'lib/redom/connection.rb', line 32

def document
  Proxy.new(self, [:document], {:backtrace => caller[0]})
end

#on_closeObject



25
# File 'lib/redom/connection.rb', line 25

def on_close; end

#on_error(err) ⇒ Object



26
# File 'lib/redom/connection.rb', line 26

def on_error(err); end

#on_openObject

Default event handlers



24
# File 'lib/redom/connection.rb', line 24

def on_open; end

#parse(str, file = '(file)') ⇒ Object



123
124
125
# File 'lib/redom/connection.rb', line 123

def parse(str, file='(file)')
  Parser.new(self).parse str, file
end

#sync(&blck) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/redom/connection.rb', line 63

def sync(&blck)
  return @sync unless blck

  fid = _fid
  stack = @proxies[fid]
  return if !stack || stack.empty?

  msg = []
  stack.each { |k, v|
    msg << v._info
  }

  rsp = Fiber.yield([self, msg])

  error = nil
  while result = rsp.shift
    proxy = stack[result[0]]
    if Array === result[1]
      case result[1][0]
      when TYPE_UNDEFINED
        # TODO
      when TYPE_PROXY
        proxy._origin proxy
      when TYPE_ERROR
        # TODO
      end
    else
      proxy._origin result[1]
    end
  end

  vars = (eval('local_variables', blck.binding) - [:_]).map { |v|
    "#{v} = map[#{v}.__id__].origin if Redom::Proxy === #{v} && map.key?(#{v}.__id__)"
  }.join("\n")
  set_var = eval %Q{
    lambda { |map|
      #{vars}
    }
  }, blck.binding
  set_var.call stack

  stack.clear

  on_error(error) if error

  nil
end

#windowObject

Proxy of browser objects



29
30
31
# File 'lib/redom/connection.rb', line 29

def window
  Proxy.new(self, [:window], {:backtrace => caller[0]})
end