Class: SvelteOnRails::TurboStream

Inherits:
Object
  • Object
show all
Defined in:
lib/svelte_on_rails/turbo_stream.rb

Class Method Summary collapse

Class Method Details

.configsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/svelte_on_rails/turbo_stream.rb', line 61

def self.configs
  @configs ||= begin

                 conf = SvelteOnRails::Configuration.instance
                 unless conf.configs[:turbo_stream]
                   raise '[SOR] missing configuration: :turbo_stream'
                 end
                 unless conf.configs[:turbo_stream][:target_html_id]
                   raise '[SOR] missing configuration: turbo_stream/target_html_id'
                 end
                 unless conf.configs[:turbo_stream][:channel]
                   raise '[SOR] missing configuration: turbo_stream/channel'
                 end

                 conf.configs[:turbo_stream]
               end
end

.dispatch(component = nil, event_detail = nil, event: 'stream-action', selector: nil, channel: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/svelte_on_rails/turbo_stream.rb', line 3

def self.dispatch(component = nil, event_detail = nil, event: 'stream-action', selector: nil, channel: nil)

  utils = SvelteOnRails::Lib::Utils
  conf = SvelteOnRails::Configuration.instance

  _comp = if component
            paths = utils.component_paths(component, nil)
            paths[:vite_path]
          end

  if event != 'stream-action' && !selector
    raise "Another event name than the default one is only possible together with a selector"
  end

  args = {
    eventDetail: event_detail,
    component: _comp,
    event: event,
    selector: selector
  }

  args_enc = Base64.strict_encode64(args.to_json)

  Turbo::StreamsChannel.send(
    "broadcast_append_to",
    channel || configs[:channel],
    target: configs[:target_html_id],
    content: "<div style=\"display: none;\" data-controller=\"svelte-on-rails-turbo-stream\" data-args=\"#{args_enc}\"></div>"
  )

end

.dispatch_by_selector(selector, event_detail = nil, event: 'stream-action', channel: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/svelte_on_rails/turbo_stream.rb', line 35

def self.dispatch_by_selector(selector, event_detail = nil, event: 'stream-action', channel: nil)

  if event != 'stream-action' && !selector
    raise "Another event name than the default one is only possible together with a selector"
  end

  args = {
    eventDetail: event_detail,
    component: ':false:',
    event: event,
    selector: selector
  }

  args_enc = Base64.strict_encode64(args.to_json)

  Turbo::StreamsChannel.send(
    "broadcast_append_to",
    require_channel(channel || configs[:channel]),
    target: configs[:target_html_id],
    content: "<div style=\"display: none;\" data-controller=\"svelte-on-rails-turbo-stream\" data-args=\"#{args_enc}\"></div>"
  )

end

.require_channel(channel) ⇒ Object



79
80
81
82
83
84
# File 'lib/svelte_on_rails/turbo_stream.rb', line 79

def self.require_channel(channel)
  unless channel.present?
    raise 'Missing attribute or configuration: turbo_stream/channel'
  end
  channel
end