Class: RestCore::EventSource

Inherits:
Struct
  • Object
show all
Includes:
RestCore
Defined in:
lib/rest-core/event_source.rb

Constant Summary

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, Simple, TIMER, Universal, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RestCore

eagerload, id

Instance Attribute Details

#clientObject

Returns the value of attribute client

Returns:

  • (Object)

    the current value of client



5
6
7
# File 'lib/rest-core/event_source.rb', line 5

def client
  @client
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



5
6
7
# File 'lib/rest-core/event_source.rb', line 5

def opts
  @opts
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



5
6
7
# File 'lib/rest-core/event_source.rb', line 5

def path
  @path
end

#queryObject

Returns the value of attribute query

Returns:

  • (Object)

    the current value of query



5
6
7
# File 'lib/rest-core/event_source.rb', line 5

def query
  @query
end

#socketObject

Returns the value of attribute socket

Returns:

  • (Object)

    the current value of socket



5
6
7
# File 'lib/rest-core/event_source.rb', line 5

def socket
  @socket
end

Instance Method Details

#closeObject



23
24
25
26
# File 'lib/rest-core/event_source.rb', line 23

def close
  socket && socket.close
rescue IOError
end

#closed?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rest-core/event_source.rb', line 19

def closed?
  !!(socket && socket.closed?)
end

#onerror(error = nil, sock = nil, &cb) ⇒ Object

would also be called upon closing, would always be called at least once



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rest-core/event_source.rb', line 62

def onerror error=nil, sock=nil, &cb
  if block_given?
    @onerror = cb
  else
    begin
      @onerror.call(error, sock) if @onerror
      onreconnect(error, sock)
    rescue
      condv.signal # so we can't be reconnecting, need to try to unblock
      raise
    end
  end
  self
end

#onmessage(event = nil, data = nil, sock = nil, &cb) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/rest-core/event_source.rb', line 52

def onmessage event=nil, data=nil, sock=nil, &cb
  if block_given?
    @onmessage = cb
  elsif @onmessage
    @onmessage.call(event, data, sock)
  end
  self
end

#onopen(sock = nil, &cb) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rest-core/event_source.rb', line 34

def onopen sock=nil, &cb
  if block_given?
    @onopen = cb
  else
    self.socket = sock # for you to track the socket
    @onopen.call(sock) if @onopen
    onmessage_for(sock)
  end
  self
rescue Exception => e
  begin # close the socket since we're going to stop anyway
    sock.close # if we don't close it, client might wait forever
  rescue IOError
  end
  # let the client has a chance to handle this, and make signal
  onerror(e, sock)
end

#onreconnect(error = nil, sock = nil, &cb) ⇒ Object

would be called upon closing, and would try to reconnect if a callback is set and return true



79
80
81
82
83
84
85
86
87
88
# File 'lib/rest-core/event_source.rb', line 79

def onreconnect error=nil, sock=nil, &cb
  if block_given?
    @onreconnect = cb
  elsif closed? && @onreconnect && @onreconnect.call(error, sock)
    reconnect
  else
    condv.signal # we could be closing, let's try to unblock it
  end
  self
end

#startObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/rest-core/event_source.rb', line 8

def start
  self.mutex = Mutex.new
  self.condv = ConditionVariable.new
  @onopen      ||= nil
  @onmessage   ||= nil
  @onerror     ||= nil
  @onreconnect ||= nil
  reconnect
  self
end

#waitObject

Raises:



28
29
30
31
32
# File 'lib/rest-core/event_source.rb', line 28

def wait
  raise RC::Error.new("Not yet started for: #{self}") unless mutex
  mutex.synchronize{ condv.wait(mutex) until closed? } unless closed?
  self
end