class EventCore::Source

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

Public Class Methods

new() click to toggle source
# File lib/event_core.rb, line 18
def initialize
  @closed = false
  @ready = false
  @timeout_secs = nil
  @trigger = nil
end

Public Instance Methods

close!() click to toggle source

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

# 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?() click to toggle source

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

# File lib/event_core.rb, line 69
def closed?
  @closed
end
consume_event_data!() click to toggle source

Consume pending event data and set readiness to false

# 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) click to toggle source

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

# File lib/event_core.rb, line 64
def event_factory(event_data)
  event_data
end
notify_trigger() click to toggle source

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

# File lib/event_core.rb, line 86
def notify_trigger
  event_data = consume_event_data!
  event = event_factory(event_data)
  if @trigger
    # Not just !@trigger.call(event), we want explicitly "false"
    close! if @trigger.call(event) == false
  end
end
ready!(event_data=nil) click to toggle source

Mark source as ready

# File lib/event_core.rb, line 33
def ready!(event_data=nil)
  @ready = true
  @event_data = event_data
end
ready?() click to toggle source

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.

# File lib/event_core.rb, line 28
def ready?
  @ready
end
select_io() click to toggle source

An optional IO object to select on

# File lib/event_core.rb, line 44
def select_io
  nil
end
select_type() click to toggle source

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

# File lib/event_core.rb, line 50
def select_type
  nil
end
timeout() click to toggle source

Timeout in seconds, or nil

# File lib/event_core.rb, line 39
def timeout
  @timeout_secs
end
trigger(&block) click to toggle source

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

# File lib/event_core.rb, line 80
def trigger(&block)
  @trigger = block
end