Class: Interfacets::Client::System

Inherits:
Object
  • Object
show all
Defined in:
lib/interfacets/client/system.rb

Defined Under Namespace

Modules: Transmit

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transmit) ⇒ System

Returns a new instance of System.



28
29
30
# File 'lib/interfacets/client/system.rb', line 28

def initialize(transmit)
  @transmit = transmit
end

Class Attribute Details

.current_bus ⇒ Object

Returns the value of attribute current_bus.



13
14
15
# File 'lib/interfacets/client/system.rb', line 13

def current_bus
  @current_bus
end

Instance Attribute Details

#transmit ⇒ Object

Returns the value of attribute transmit.



27
28
29
# File 'lib/interfacets/client/system.rb', line 27

def transmit
  @transmit
end

Class Method Details

.logger ⇒ Object



18
19
20
# File 'lib/interfacets/client/system.rb', line 18

def logger
  @logger ||= InterfacetsLogger.main
end

.start(transmit: Transmit) ⇒ Object



22
23
24
# File 'lib/interfacets/client/system.rb', line 22

def start(transmit: Transmit)
  @instance = new(transmit)
end

Instance Method Details

#bus_registry ⇒ Object



83
84
85
# File 'lib/interfacets/client/system.rb', line 83

def bus_registry
  @bus_registry ||= {}
end

#config ⇒ Object

private



55
56
57
# File 'lib/interfacets/client/system.rb', line 55

def config
  @config ||= Config.new
end

#create_bus(payload) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/interfacets/client/system.rb', line 59

def create_bus(payload)
  id = payload.fetch("id")
  hydration_event = payload.fetch("hydration")

  channel_ids = payload.fetch("channel_ids")

  Bus.new(
    id:,
    channels: [
      Channels::Api.new(id: "interfacets:api"),
      Channels::React::Channel.new(id: "dom"),
      Channels::Url.new(id: "url"),
      (Channels::SpeechToText.new(id: "speechToText") if channel_ids.include?("speechToText")),
      (Channels::Timer.new(id: "timer") if channel_ids.include?("timer")),
      (Channels::Audio.new(id: "audio") if channel_ids.include?("audio")),
    ].compact,
    root_url: payload.fetch("config").fetch("root_url"),
  ).tap do
    bus_registry[id] = _1
  end

  handle(hydration_event)
end

#handle(event) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/interfacets/client/system.rb', line 32

def handle(event)
  if event.fetch("type") == "interfacets:system:create_bus"
    create_bus(event.fetch("payload"))
  else
    bus_id = event.dig("destination", "bus")
    raise("unknown event: #{event.inspect}") unless bus_id

    System.current_bus = bus_registry.fetch(bus_id)
    payload = System.current_bus.handle(event)

    transmit.(
      {
        type: "interfacets:system:render",
        payload:,
      },
    )
  end
ensure
  GC.start
end