Class: EventCore::Source

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

Overview

Low level event source representation. Only needed when the convenience APIs on EventLoop are not enough.

Instance Method Summary collapse

Constructor Details

#initializeSource

Returns a new instance of Source.



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

def initialize
  @closed = false
  @ready = false
  @timeout_secs = nil
  @trigger = nil
end

Instance Method Details

#close!Object

Close this source, marking it for removal from the main loop.



74
75
76
77
# File 'lib/event_core.rb', line 74

def close!
  @closed = true
  @trigger = nil # Help the GC, if the closure holds onto some data
end

#closed?Boolean

Check to see if close!() has been called.

Returns:

  • (Boolean)


69
70
71
# File 'lib/event_core.rb', line 69

def closed?
  @closed
end

#consume_event_data!Object

Consume pending event data and set readiness to false



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

def consume_event_data!
  raise "Source not ready: #{self}" unless ready?
  data = @event_data
  @event_data = nil
  @ready = false
  data
end

#event_factory(event_data) ⇒ Object

Raw event data is passed to this function before passed to the trigger



64
65
66
# File 'lib/event_core.rb', line 64

def event_factory(event_data)
  event_data
end

#notify_triggerObject

Consume pending event data and fire the trigger, closing if the trigger returns (explicitly) false.



86
87
88
89
90
91
92
93
# File 'lib/event_core.rb', line 86

def notify_trigger
  event_data = consume_event_data!
  event = event_factory(event_data)
  if @trigger
    # Not just [email protected](event), we want explicitly "false"
    close! if @trigger.call(event) == false
  end
end

#ready!(event_data = nil) ⇒ Object

Mark source as ready



33
34
35
36
# File 'lib/event_core.rb', line 33

def ready!(event_data=nil)
  @ready = true
  @event_data = event_data
end

#ready?Boolean

Check if a source is ready. Called on each main loop iteration. May have side effects, but should not leave ready state until consume_event_data!() has been called.

Returns:

  • (Boolean)


28
29
30
# File 'lib/event_core.rb', line 28

def ready?
  @ready
end

#select_ioObject

An optional IO object to select on



44
45
46
# File 'lib/event_core.rb', line 44

def select_io
  nil
end

#select_typeObject

Returns :read, :write, or nil. If select_io is non-nil, then the select_type must not be nil.



50
51
52
# File 'lib/event_core.rb', line 50

def select_type
  nil
end

#timeoutObject

Timeout in seconds, or nil



39
40
41
# File 'lib/event_core.rb', line 39

def timeout
  @timeout_secs
end

#trigger(&block) ⇒ Object

Set the trigger function to call on events to the given block



80
81
82
# File 'lib/event_core.rb', line 80

def trigger(&block)
  @trigger = block
end