Class: Low::Event

Inherits:
Object
  • Object
show all
Includes:
Support::ValueObject, LowType
Defined in:
lib/events/event.rb

Overview

An event represents what is currently happening in your application.

Events are mutable in most cases (except for RenderEvent). They are action-driven, representing inputs and outputs that are currently happening in a linear pipeline-like flow. They are present-tense and one-to-many with one return value. The result of the previous event is made available to the next event. [UNRELEASED]

Integrations:

  • Observers for observer pattern via an event-centric API

  • EventPool for a tree of events and their child events

  • LowState for state machines to trigger multiple actions [UNLRELEASED]

Constant Summary collapse

ROOT_FIBER =
Fiber.current

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::ValueObject

#==, #eql?, #hash

Constructor Details

#initialize(key:, action: nil, children: []) ⇒ Event

Returns a new instance of Event.



26
27
28
29
30
31
# File 'lib/events/event.rb', line 26

def initialize(key:, action: nil, children: [])
  @key = key
  @action = action
  @children = children
  @created_at = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



21
22
23
# File 'lib/events/event.rb', line 21

def action
  @action
end

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#created_atObject (readonly)

Returns the value of attribute created_at.



21
22
23
# File 'lib/events/event.rb', line 21

def created_at
  @created_at
end

#keyObject (readonly)

Returns the value of attribute key.



21
22
23
# File 'lib/events/event.rb', line 21

def key
  @key
end

Class Method Details

.inherited(child) ⇒ Object



68
69
70
# File 'lib/events/event.rb', line 68

def inherited(child)
  child.include LowType
end

.take(**kwargs) ⇒ Object



64
65
66
# File 'lib/events/event.rb', line 64

def take(**kwargs)
  new(**kwargs).take
end

.trigger(**kwargs) ⇒ Object



60
61
62
# File 'lib/events/event.rb', line 60

def trigger(**kwargs)
  new(**kwargs).trigger
end

Instance Method Details

#takeObject



39
40
41
42
43
# File 'lib/events/event.rb', line 39

def take
  event_tree = branch
  key = Observers::Keys[@key] || raise(Observers::Keys::MissingKeyError)
  key.take(event: self) { restore_level(event_tree:) }
end

#triggerObject



33
34
35
36
37
# File 'lib/events/event.rb', line 33

def trigger
  event_tree = branch
  key = Observers::Keys[@key] || raise(Observers::Keys::MissingKeyError)
  key.trigger(event: self) { restore_level(event_tree:) }
end