Sha256: c195d5de121baa4016ca9a8ccf03083ec4bf079573ffb2352e2deaeb2015697c

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module Vedeu
  RefreshFailed = Class.new(StandardError)

  module Buffers
    extend self

    def create(interface)
      buffers[interface.name][:clear] = Clear.call(interface)

      Vedeu.events.on("_refresh_#{interface.name}_".to_sym, interface.delay) do
        refresh(interface.name)
      end

      # TODO: cannot refresh group since no logic to fetch group from buffer
      # unless interface.group.nil? || interface.group.empty?
      #   Vedeu.events.on("_refresh_#{interface.group}_".to_sym, interface.delay) do
      #     refresh_group(interface.group)
      #   end
      # end
    end

    def enqueue(name, sequence)
      buffers[name][:next].unshift(sequence)
    end

    def refresh(name)
      data = buffers.fetch(name) do
        fail RefreshFailed, 'Cannot refresh non-existent interface.'
      end

      sequence = if data[:next].any?
        data[:current] = data[:next].pop

      elsif data[:current].empty?
        data[:clear]

      else
        data[:current]

      end
      Terminal.output(sequence)
    end

    def refresh_all
      buffers.keys.map { |name| refresh(name) }
    end

    def reset
      @buffers = {}
    end

    private

    def buffers
      @buffers ||= Hash.new do |hash, key|
        hash[key] = { current: '', next: [], clear: '' }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.1.10 lib/vedeu/output/buffers.rb