Module: PagesCore::PubSub

Defined in:
lib/pages_core/pub_sub.rb

Defined Under Namespace

Classes: Subscriber

Class Method Summary collapse

Class Method Details

.publish(name, payload = {}) ⇒ Object



6
7
8
9
# File 'lib/pages_core/pub_sub.rb', line 6

def publish(name, payload = {})
  subscribers.select { |s| s.name == name }
             .each { |s| s.call(payload) }
end

.subscribe(name, &block) ⇒ Object



11
12
13
14
15
# File 'lib/pages_core/pub_sub.rb', line 11

def subscribe(name, &block)
  subscriber = PagesCore::PubSub::Subscriber.new(name, block)
  subscribers << subscriber
  subscriber
end

.subscribersObject



17
18
19
# File 'lib/pages_core/pub_sub.rb', line 17

def subscribers
  @subscribers ||= []
end

.unsubscribe(subscriber) ⇒ Object



21
22
23
# File 'lib/pages_core/pub_sub.rb', line 21

def unsubscribe(subscriber)
  @subscribers = subscribers.reject { |s| s == subscriber }
end