Class: Timers::Events

### Home Classes Methods

### In Files

- lib/timers/events.rb

### Parent

Object

### Namespace

- CLASS Timers::Events::Handle

### Methods

- ::new
- #fire
- #first
- #schedule
- #size

### Class/Module Index

Quicksearch

- Timers
- Timers::Events
- Timers::Events::Handle
- Timers::Group
- Timers::Timer
- Timers::Wait

No matching classes.

# Timers::Events

Maintains an ordered list of events, which can be cancelled.

### Public Class Methods

new() click to toggle source

    # File lib/timers/events.rb, line 48
    def initialize
      # A sequence of handles, maintained in sorted order, future to present.
      # @sequence.last is the next event to be fired.
      @sequence = []
    end

### Public Instance Methods

fire(time) click to toggle source

Fire all handles for which Handle#time is less than the given time.

    # File lib/timers/events.rb, line 83
    def fire(time)
      pop(time).reverse_each do |handle|
        handle.fire(time)
      end
    end

first() click to toggle source

Returns the first non-cancelled handle.

    # File lib/timers/events.rb, line 67
    def first
      while handle = @sequence.last
        if handle.cancelled?
          @sequence.pop
        else
          return handle
        end
      end
    end

schedule(time, callback) click to toggle source

Add an event at the given time.

    # File lib/timers/events.rb, line 55
    def schedule(time, callback)
      handle = Handle.new(time.to_f, callback)

      index = bisect_left(@sequence, handle)

      # Maintain sorted order, O(logN) insertion time.
      @sequence.insert(index, handle)

      return handle
    end

size() click to toggle source

Returns the number of pending (possibly cancelled) events.

    # File lib/timers/events.rb, line 78
    def size
      @sequence.size
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.