-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcaster.rb
36 lines (30 loc) · 826 Bytes
/
broadcaster.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require './channel.rb'
require 'json'
class Broadcaster < Array
attr_accessor :channels, :archive_limit, :formatter
@@default_archive_limit = 20
def initialize
@archive_limit = @@default_archive_limit
@channels = []
@formatter = ->(msg) { JSON.dump msg }
end
def clear_archives!
@channels.each &:clear
end
# Used for when an object wants its most recent
# message to be automatically sent to a new sub-
# scriber.
def register_channel
new_chan = Channel.new self, @archive_limit
@channels.push new_chan
new_chan
end
def broadcast msg
msg = @formatter[msg]
each { |x| x.send msg }
end
def push x
@channels.each { |channel| x.send @formatter[channel.last] }
super x
end
end