Class: Interfacets::Client::Channels::React::Channel

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/interfacets/client/channels/react/channel.rb

Instance Attribute Summary

Attributes included from Base

#id

Instance Method Summary collapse

Methods included from Base

#initialize, #type

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Instance Method Details

#builder(stream = "default") ⇒ Object



25
26
27
# File 'lib/interfacets/client/channels/react/channel.rb', line 25

def builder(stream = "default")
  @builder
end

#handle(entity:, event:, build_entity:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/interfacets/client/channels/react/channel.rb', line 39

def handle(entity:, event:, build_entity:)
  raise if event.nil?

  actions = [
    "interfacets:react-dom:action",
    "interfacets:react-dom:memoized-action",
    "interfacets:react-dom:online",
  ]

  raise("unknown event: #{event}") unless actions.include?(event.fetch("type"))

  return if event.fetch("type") == "interfacets:react-dom:online"

  payload = event.fetch("payload")
  callback_id = payload.fetch("id")
  callback = @callbacks.fetch(callback_id)
  Evaluator.call(entity, callback, payload["event"])
end

#prepare(entity) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/interfacets/client/channels/react/channel.rb', line 12

def prepare(entity)
  @cache ||= {}

  if entity.internal_entity_id == @builder&.entity&.internal_entity_id
    # keep all the callbacks we've ever assigned, if its the same entity
    # should probably prune these? Only need to remember last renders
    # callbacks?
    builder.__reset__
  else
    @builder = Builder.new(entity, @cache)
  end
end

#result ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/interfacets/client/channels/react/channel.rb', line 29

def result
  @callbacks = builder.callbacks

  {
    streams: {
      default: { dom: builder.nodes }
    }
  }
end