LivelySourceLivelyApplication

class Application

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 #body.

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 use as the application body.

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 initialize(delegate)

Initialize a new Lively application.

Signature

parameter delegate Protocol::HTTP::Middleware

The next middleware in the chain.

Implementation

def initialize(delegate)
	super(delegate)
end

attr :delegate

Signature

attribute Protocol::HTTP::Middleware

The delegate middleware for request handling.

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 body

Create the body content for this application.

Signature

returns Live::View

A new view instance.

Implementation

def body
	self.allowed_views.first.new(**self.state)
end

def index

Create the index page for this application.

Signature

returns Pages::Index

A new index page instance.

Implementation

def index
	Pages::Index.new(title: self.title, body: self.body)
end

def handle(request)

Handle a standard HTTP request.

Signature

parameter request Protocol::HTTP::Request

The incoming HTTP request.

returns Protocol::HTTP::Response

The HTTP response with the rendered page.

Implementation

def handle(request)
	return Protocol::HTTP::Response[200, [], [self.index.call]]
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)
	if request.path == "/live"
		return Async::WebSocket::Adapters::HTTP.open(request, &self.method(:live)) || Protocol::HTTP::Response[400]
	else
		return handle(request)
	end
end