Module: Hibiki::Rails::Channel
- Defined in:
- lib/hibiki/rails/channel.rb
Overview
Include into an ActionCable channel to host one connection-scoped signal graph:
class CounterChannel < ApplicationCable::Channel
include Hibiki::Rails::Channel
def build_graph # runs on the graph's own thread,
@count = Hibiki::State.new(0) # inside Hibiki.root
Hibiki::Effect.new { ... broadcast ... }
end
def increment = @count.value += 1 # actions are plain methods
end
Lifecycle: subscribed rejects without a cid param, then builds the
graph via #build_graph inside Hibiki.root on a dedicated GraphActor
thread; every incoming action runs on that same thread inside one
Hibiki.batch (N writes per action still mean one re-run per affected
effect); unsubscribed disposes the root and stops the actor.
A channel that overrides subscribed/unsubscribed itself must call
super.
NOTE: everything this module adds is private (except the #perform_action override, which ActionCable already exposes) — any other public method would be picked up by Channel#action_methods and become client-invocable.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#perform_action(data) ⇒ Object
ActionCable's single dispatch point for incoming actions.
Class Method Details
.included(base) ⇒ Object
33 34 35 36 |
# File 'lib/hibiki/rails/channel.rb', line 33 def self.included(base) base.extend(ClassMethods) base.include(Broadcasts) end |
Instance Method Details
#perform_action(data) ⇒ Object
ActionCable's single dispatch point for incoming actions. The whole action body is posted to the graph's thread and wrapped in one batch — cable threads never touch the graph. Spike-verified: batch per action is all the coalescing the replace-partial style needs.
rescue_from handlers still run (dispatch_action applies them
inside the job, now on the graph thread); what they don't handle
propagates to the actor's on_error — ::Rails.error by default.
56 57 58 59 60 |
# File 'lib/hibiki/rails/channel.rb', line 56 def perform_action(data) # A nil actor means the subscription was rejected or already torn # down; drop the action rather than blow up the cable thread. @__hibiki_actor&.post { Hibiki.batch { super(data) } } end |