lib/svelte_on_rails/action_cable.rb
module SvelteOnRails class ActionCable 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 utils.validate_filename(component) "/#{conf.components_folder + component}" end _channel = require_channel(svelte_on_rails_configs['channel'] || channel) 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 } ::ActionCable.server.broadcast( _channel, args ) end def self.dispatch_by_selector(selector, event_detail = nil, event: 'stream-action', channel: nil) _channel = require_channel(svelte_on_rails_configs['channel'] || channel) 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 } ::ActionCable.server.broadcast( _channel, args ) end private def self.svelte_on_rails_configs cnf = SvelteOnRails::Configuration.instance.configs[:action_cable] raise 'ActionCable not configured for SvelteOnRails' if cnf.nil? cnf end def self.require_channel(channel) unless channel.present? raise 'Missing attribute or configuration: action_cable/channel' end channel end end end