Module: Smith::Events

Defined in:
lib/smith/events.rb,
lib/smith/events/bus.rb,
lib/smith/events/subscription.rb,
lib/smith/events/step_completed.rb

Defined Under Namespace

Classes: Scope, StepCompleted, Subscription

Class Method Summary collapse

Class Method Details

.emit(event) ⇒ Object



32
33
34
# File 'lib/smith/events/bus.rb', line 32

def emit(event)
  subscriptions.each { |sub| dispatch_to(sub, event) }
end

.on(event_class, **opts, &block) ⇒ Object



26
27
28
29
30
# File 'lib/smith/events/bus.rb', line 26

def on(event_class, **opts, &block)
  sub = Subscription.new(event_class, handler: block, predicate: opts[:if])
  subscriptions << sub
  sub
end

.reset!Object



43
44
45
# File 'lib/smith/events/bus.rb', line 43

def reset!
  @subscriptions = []
end

.subscriptionsObject



22
23
24
# File 'lib/smith/events/bus.rb', line 22

def subscriptions
  @subscriptions ||= []
end

.withinObject



36
37
38
39
40
41
# File 'lib/smith/events/bus.rb', line 36

def within
  scope = Scope.new
  yield scope
ensure
  scope&.cancel_all
end