class Application < Protocol::HTTP::Middleware

Inherits from
Protocol::HTTP::Middleware

Represents the main Lively application middleware.

This class serves as the entry point for Lively applications, handling both standard HTTP requests for the initial page load and WebSocket connections for live updates. It integrates with the Live framework to provide real-time interactive web applications.

Use .[] to create a simple application class for a single view, optionally with shared state. For more complex applications, subclass and override #allowed_views, #state, and #configure_routes to route requests to pages.

Definitions

def self.[](*tags, **state)

Create a new application class configured for a specific Live view tag, optionally with shared state that is passed to all views.

Signature

parameter tag Class

The Live view class to render at the root route.

parameter state Hash

Shared state to pass to all views as keyword arguments.

returns Class

A new application class configured for the specified tag.

Implementation

def self.[](*tags, **state)
	klass = Class.new(self)
	
	klass.const_set(:VIEWS, tags)
	klass.const_set(:STATE, state)
	
	return klass
end

def state

The shared state for this application, passed to all views via the resolver. Override this in subclasses to provide custom state.

Signature

returns Hash

Key-value pairs passed as keyword arguments to view constructors.

Implementation

def state
	self.class::STATE
end

def allowed_views

The view classes that this application allows. Override this in subclasses to specify which views can be resolved.

Signature

returns Array(Class)

The allowed view classes.

Implementation

def allowed_views
	self.class::VIEWS
end

def resolver

The resolver for live components. Built from #allowed_views and #state.

Signature

returns Lively::Resolver

The resolver instance.

Implementation

def resolver
	@resolver ||= Resolver.new(self.state).tap do |resolver|
		resolver.allow(*self.allowed_views)
	end
end

def live(connection)

Handle a WebSocket connection for live updates.

Signature

parameter connection Async::WebSocket::Connection

The WebSocket connection.

Implementation

def live(connection)
	Live::Page.new(self.resolver).run(connection)
end

def title

Get the title for this application.

Signature

returns String

The class name of this application.

Implementation

def title
	self.class.name
end

def configure_routes(router)

Add application routes to the given router. Override this method to map paths to views or other handlers.

Signature

parameter router Router::Builder

The router to configure.

Implementation

def configure_routes(router)
	router.get("/") do
		view_class = self.allowed_views.first
		body = self.resolver.root(view_class) if view_class
		Pages::Index.new(title: self.title, body: body).call
	end
end

def router

The router for this application. Unmatched requests are passed to the application delegate.

Signature

returns Router

The configured router.

Implementation

def router
	@router ||= Router.build(delegate) do |router|
		configure_system_routes(router)
		configure_routes(router)
	end
end

def call(request)

Process an incoming HTTP request.

Signature

parameter request Protocol::HTTP::Request

The incoming HTTP request.

returns Protocol::HTTP::Response

The appropriate response for the request.

Implementation

def call(request)
	return router.call(request)
end

def configure_system_routes(router)

Add framework-owned routes to the given router.

Signature

parameter router Router::Builder

The router to configure.

Implementation

def configure_system_routes(router)
	router.route("/live") do |request|
		Async::WebSocket::Adapters::HTTP.open(request, &self.method(:live)) || Protocol::HTTP::Response[400]
	end
end