Module: Phlex::Reactive

Defined in:
lib/phlex/reactive.rb,
lib/phlex/reactive/engine.rb,
lib/phlex/reactive/version.rb,
lib/phlex/reactive/component.rb,
lib/phlex/reactive/streamable.rb,
app/controllers/phlex/reactive/actions_controller.rb,
lib/generators/phlex/reactive/install/install_generator.rb,
lib/generators/phlex/reactive/component/component_generator.rb

Overview

phlex-reactive: reactive Phlex components for Rails.

Two cooperating mixins, one client runtime, one endpoint:

* Phlex::Reactive::Streamable — gives a component a stable `id` and class
  methods to render itself as a Turbo Stream (`.replace`, `.append`, ...)
  and to broadcast itself (`.broadcast_replace_to`, ...). The server->client
  half (controller responses + background broadcasts).

* Phlex::Reactive::Component — declares client-invokable `action`s and
  emits a signed identity token + the wiring the generic `reactive`
  Stimulus controller needs. The client->server half (clicks, form input).

Both halves converge on ONE re-render unit: the component, targeted by its ‘id`. See the README for the mental model and examples.

Defined Under Namespace

Modules: Component, Generators, Streamable Classes: ActionsController, Engine, Error, InvalidToken

Constant Summary collapse

IDENTITY_PURPOSE =

Purpose string bound into every identity token’s signature so a token minted for phlex-reactive can’t be replayed against another verifier use.

"phlex-reactive/identity"
VERSION =
"0.2.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.action_pathObject



69
70
71
# File 'lib/phlex/reactive.rb', line 69

def action_path
  @action_path ||= "/reactive/actions"
end

.authorization_errorsObject

Exception classes the action endpoint renders as 403. Append your authorization library’s error (Pundit::NotAuthorizedError, ActionPolicy::Unauthorized, …).



61
62
63
# File 'lib/phlex/reactive.rb', line 61

def authorization_errors
  @authorization_errors
end

.base_controller_nameObject



81
82
83
# File 'lib/phlex/reactive.rb', line 81

def base_controller_name
  @base_controller_name ||= "ActionController::Base"
end

.rendererObject



77
78
79
# File 'lib/phlex/reactive.rb', line 77

def renderer
  @renderer ||= defined?(::ActionController::Base) ? ::ActionController::Base : nil
end

.verifierObject



73
74
75
# File 'lib/phlex/reactive.rb', line 73

def verifier
  @verifier ||= default_verifier
end

Class Method Details

.base_controllerObject



85
86
87
# File 'lib/phlex/reactive.rb', line 85

def base_controller
  base_controller_name.constantize
end

.current_connection_idObject

The acting client’s SSE connection id during an action, or nil. Set by the ActionsController from the X-Pgbus-Connection header. A component action passes ‘exclude: Phlex::Reactive.current_connection_id` (or the `reactive_connection_id` helper) to suppress the actor’s own broadcast echo.



104
105
106
# File 'lib/phlex/reactive.rb', line 104

def current_connection_id
  Thread.current[:phlex_reactive_connection_id]
end

.sign(payload) ⇒ Object

Signs a payload hash into an identity token.



95
96
97
# File 'lib/phlex/reactive.rb', line 95

def sign(payload)
  verifier.generate(payload, purpose: IDENTITY_PURPOSE)
end

.verify(token) ⇒ Object

Returns the verified payload hash, or nil if the token is invalid.



90
91
92
# File 'lib/phlex/reactive.rb', line 90

def verify(token)
  verifier.verified(token, purpose: IDENTITY_PURPOSE)
end

.with_connection_id(connection_id) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/phlex/reactive.rb', line 108

def with_connection_id(connection_id)
  previous = Thread.current[:phlex_reactive_connection_id]
  Thread.current[:phlex_reactive_connection_id] = connection_id.presence
  yield
ensure
  Thread.current[:phlex_reactive_connection_id] = previous
end