lifecycle

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package lifecycle is the compiled source for docs/guide/lifecycle-hooks.md.

There are three hooks: mount, event, and teardown. This file is all three.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Authorize

Authorize is the event hook. It runs before the reducer for every event, at the single mailbox ingress, so a new event kind cannot skip it.

Returning nil allows the event. A *live.DenyError rejects one event and leaves the connection open. A *live.FatalDenyError rejects it and closes the connection with UNAUTHORIZED (close code 4006). An error of any other shape is treated as a denial, because a hook that failed open by accident is the one failure mode an authorization hook must not have.

func Init

Init is the mount hook. It runs once, as the first transition, before the first snapshot, and it returns two things: the session's initial state, and any startup effects.

Registration happens here, synchronously, and the long-running pump is returned as an effect. The order matters: if the pump did the registering, a change published between the mount and the pump's first loop would be missed, and the session would render a value that was already stale.

Values a mount needs from the upgrade request arrive through the context — that is the whole reason there is no Session.Request(), which would keep an *http.Request alive for the connection's life.

func Teardown

func Teardown(topic *Topic) func(context.Context, live.Session[live.AnonymousIdentity], State)

Teardown is the unmount hook. It runs after the session actor has exited, with the final state, and it is where a subscription taken at mount is released.

It is optional, and leaving it nil is what leaks. It is not called with a state value when Init itself failed, because there is no final state to hand over.

Types

type State

type State struct {
	Me      live.ID
	Subject string
}

State is one session's view.

type Topic

type Topic struct {
	// contains filtered or unexported fields
}

Topic is the application-owned pubsub the session subscribes to at mount and unsubscribes from at teardown.

func NewTopic

func NewTopic() *Topic

func (*Topic) Join

func (t *Topic) Join(id live.ID, subject string)

func (*Topic) Leave

func (t *Topic) Leave(id live.ID)

func (*Topic) Len

func (t *Topic) Len() int

func (*Topic) WatchEffect

func (t *Topic) WatchEffect() live.Effect[live.AnonymousIdentity]

WatchEffect is the subscription pump the mount hook schedules.

It is a constructor over a concrete live.Effect[live.AnonymousIdentity]: a source, which provenance and metrics see, and a Run closing over the topic. The Run holds the session open for as long as the session lasts, which is what a subscription is, and returns when the library cancels it at shutdown.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL