abstract class Termisu::Event::Source

Overview

Abstract base class for event sources.

An Event::Source produces events and sends them to a shared channel. Subclasses implement specific event generation logic (input, timer, resize).

Implementing an Event::Source

Subclasses must implement four abstract methods:

Thread Safety

Implementations should use Atomic(Bool) for the running state and compare_and_set to prevent double-start issues.

Example

class MySource < Termisu::Event::Source
  @running = Atomic(Bool).new(false)
  @fiber : Fiber?

  def start(output : Channel(Event::Any)) : Nil
    return unless @running.compare_and_set(false, true)
    @fiber = spawn(name: "my-source") do
      while @running.get
        # Generate and send events
        output.send(my_event) rescue break
        sleep 100.milliseconds
      end
    end
  end

  def stop : Nil
    return unless @running.compare_and_set(true, false)
    # Cleanup resources if needed
  end

  def running? : Bool
    @running.get
  end

  def name : String
    "my-source"
  end
end

Direct Known Subclasses

Defined in:

termisu/event/source.cr

Instance Method Summary

Instance Method Detail

abstract def name : String #

Returns a descriptive name for this source.

Used for logging, debugging, and identifying sources in the Event::Loop. Examples: "input", "resize", "timer", "custom-network"


[View source]
abstract def running? : Bool #

Returns true if this source is currently running.

Used to check state and control the event production loop.


[View source]
abstract def start(output : Channel(Event::Any)) : Nil #

Starts producing events to the output channel.

The source should spawn a fiber to produce events asynchronously. Events are sent to the provided channel.

Implementations should:

  • Use compare_and_set(false, true) to prevent double-start
  • Spawn a named fiber for debugging: spawn(name: "termisu-xxx")
  • Handle Channel::ClosedError gracefully when sending
  • Check #running? in the event loop to enable clean shutdown

[View source]
abstract def stop : Nil #

Stops producing events.

Sets the running state to false, signaling the fiber to exit. This should be a quick, non-blocking operation. The actual fiber cleanup happens on the next loop iteration.


[View source]