Wisper_gem

This is just an example.. but it shows Publisher and Listeners.. both as class and block.

A better example: Let’s say you have areas defined on the terminal. You call them windows. You want to be able to shift between windows. Only one window can have focus at the time and all keystrokes should be sent there.

When one window gets focus.. the other windows must loose focus.

There wisper should be quite useful.

require 'wisper'
# frozen_string_literal: true

class Apa
  include Wisper::Publisher

  def greeting(name, age = 0)
    if name == 'olle'
      broadcast(:viktig_person, age)
    else
      broadcast(:age_zero, 'jorden går under') if age == 0
      broadcast(:oviktig_person, age)
    end
  end
end

class Lizten
  def viktig_person(age)
    puts 'Viktiga OLLE är %s år gammal.' % age
  end

  def oviktig_person(age)
    puts 'En oviktig person sägs vara %s år gammal.' % age
  end
end

apa = Apa.new
apa.subscribe(Lizten.new)


apa.greeting('kalle', 12)
apa.greeting('olle', 25)

apa.on(:age_zero) do |msg|
  puts msg
end


apa.greeting('ior')

The window focus is better (maybe) solved like this:

class Window
  @@wins = []

  def initialize(name)
    @name = name
    @focus = false
    @@wins << self
  end

  def do_focus
    @@wins.map!(&:unfocus)
    @focus = true
  end

  def unfocus = ( @focus = false )
end