Class: Lively::Application

Inherits:
Protocol::HTTP::Middleware
  • Object
show all
Defined in:
lib/lively/application.rb

Overview

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 Application.[] 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.

Constant Summary collapse

VIEWS =
[HelloWorld].freeze
STATE =
{}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate) ⇒ Application

Initialize a new Lively application.



47
48
49
# File 'lib/lively/application.rb', line 47

def initialize(delegate)
	super(delegate)
end

Instance Attribute Details

#delegateObject (readonly)

Returns the value of attribute delegate.



52
53
54
# File 'lib/lively/application.rb', line 52

def delegate
  @delegate
end

#The delegate middleware for request handling.(delegatemiddleware) ⇒ Object



52
# File 'lib/lively/application.rb', line 52

attr :delegate

Class Method Details

.[](*tags, **state) ⇒ Object

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



36
37
38
39
40
41
42
43
# File 'lib/lively/application.rb', line 36

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

Instance Method Details

#allowed_viewsObject

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



64
65
66
# File 'lib/lively/application.rb', line 64

def allowed_views
	self.class::VIEWS
end

#bodyObject

Create the body content for this application.



91
92
93
# File 'lib/lively/application.rb', line 91

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

#call(request) ⇒ Object

Process an incoming HTTP request.



111
112
113
114
115
116
117
# File 'lib/lively/application.rb', line 111

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

#handle(request) ⇒ Object

Handle a standard HTTP request.



104
105
106
# File 'lib/lively/application.rb', line 104

def handle(request)
	return Protocol::HTTP::Response[200, [], [self.index.call]]
end

#indexObject

Create the index page for this application.



97
98
99
# File 'lib/lively/application.rb', line 97

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

#live(connection) ⇒ Object

Handle a WebSocket connection for live updates.



79
80
81
# File 'lib/lively/application.rb', line 79

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

#resolverObject

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



71
72
73
74
75
# File 'lib/lively/application.rb', line 71

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

#stateObject

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



57
58
59
# File 'lib/lively/application.rb', line 57

def state
	self.class::STATE
end

#titleObject

Get the title for this application.



85
86
87
# File 'lib/lively/application.rb', line 85

def title
	self.class.name
end