Module: Inform::Publisher

Includes:
Subscribers
Included in:
Event, Object, Runtime, System::Object
Defined in:
lib/runtime/publication.rb

Overview

The Publisher module to implement rudimentary pub-sub functionality

Constant Summary

Constants included from Subscribers

Subscribers::REGISTRY

Instance Method Summary collapse

Methods included from Subscribers

#explicit_subscribers, #subscribe, #unsubscribe, #unsubscribe_all

Instance Method Details

#muted?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/runtime/publication.rb', line 29

def muted?
  false
end

#publish(message, *args, &condition) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/runtime/publication.rb', line 79

def publish(message, *args, &condition)
  return publish_system_message(message, *args, &condition) if message.is_a?(Symbol)
  return if muted?
  subscribers.each do |subscriber|
    next if block_given? && !condition.call(subscriber)
    # log.info "#{subscriber.name}#update message: #{message.inspect}"
    subscriber.update(message, *args)
  end
  false
end

#publish_except(message, *args, &condition) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/runtime/publication.rb', line 60

def publish_except(message, *args, &condition)
  return if muted?
  (subscribers - args.flatten).each do |subscriber|
    next if block_given? && !condition.call(subscriber)
    log.warn "publishing message (#{message}) to subscriber: #{subscriber}"
    subscriber.update(message)
  end
  false
end

#publish_only(message, *args, &condition) ⇒ Object

rubocop: enable Metrics/AbcSize rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity



51
52
53
54
55
56
57
58
# File 'lib/runtime/publication.rb', line 51

def publish_only(message, *args, &condition)
  return if muted?
  (subscribers & args.flatten).each do |subscriber|
    next if block_given? && !condition.call(subscriber)
    subscriber.update(message)
  end
  false
end

#publish_system_message(message, *args, &condition) ⇒ Object

For system messages (to the InformLibrary, mainly)



71
72
73
74
75
76
77
# File 'lib/runtime/publication.rb', line 71

def publish_system_message(message, *args, &condition)
  subscribers.each do |subscriber|
    next if block_given? && !condition.call(subscriber, *args)
    subscriber.send(message, *args) if subscriber.respond_to?(message)
  end
  false
end

#subscribersObject

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/runtime/publication.rb', line 36

def subscribers
  safe_refresh if respond_to?(:safe_refresh)
  root_obj = attempt_twice { root } if self.respond_to?(:root) # TODO: FIXME
  return explicit_subscribers.dup if root_obj.nil?
  eavesdroppers = (root_obj&.safe_refresh&.branch || []) - [self]
  # explicit_subscribers + (eavesdroppers & Session.players) # TODO: FIXME
  players = defined?(Session) ? Session.players.dup : []
  explicit_subscribers + eavesdroppers.each_with_object([]) do |o1, memo|
    memo.concat(players.select { |o2| o1 == o2 })
  end
end