effects

package
v0.2.6 Latest Latest
Warning

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

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

Documentation

Overview

Package effects is the compiled source for docs/guide/effects-and-server-push.md.

It is a counter shared by every session: a click returns an effect, the store applies it, and a per-session pump emits the result back into every subscribed session.

Index

Constants

View Source
const (
	EventInc  = "count.inc"
	EventSync = "count.sync"

	FieldValue   = "value"
	FieldVersion = "version"

	// SourceApply and SourceWatch are what EffectSource returns. They reach
	// the wire as the origin source "effect:<name>" on every patch the effect
	// causes, and they are the value the failure event carries in
	// live.EffectFailedSourceField.
	SourceApply = "count.apply"
	SourceWatch = "count.watch"
)

Variables

This section is empty.

Functions

func Reducer

func Reducer(store *Store) live.Reducer[State, live.AnonymousIdentity]

Reducer never changes Value. It returns an effect and learns the result the same way every other session does, which is what "server-authoritative" means concretely and why two tabs cannot disagree.

It is a constructor over the store because an effect carries its own Run, so a reducer that schedules one has to be able to build it. The transition still touches the store not at all.

Types

type State

type State struct {
	Value   int64
	Version uint64
}

State is one session's view of the shared counter.

type Store

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

Store is the shared counter. It is the application's, not the library's.

func NewStore

func NewStore() *Store

func (*Store) ApplyEffect

func (s *Store) ApplyEffect(delta int64, cause uint64) live.Effect[live.AnonymousIdentity]

ApplyEffect asks the store to change the shared counter by delta.

cause is the event that asked. It is carried through so the emission the store produces can name it — see Pump.

func (*Store) Pump

func (s *Store) Pump(ctx context.Context, id live.ID, emit live.Emitter) error

Pump delivers snapshots to one session until its context is cancelled.

It runs for the session's whole life, on a goroutine the library owns and waits for at shutdown, which is why it returns on cancellation rather than on a store signal it might never get.

func (*Store) SyncEventFor

func (s *Store) SyncEventFor(cause uint64) live.Event

SyncEventFor is syncEvent with a contributing claim attached.

Contributing is the one causal field an application sets rather than reads, and it exists because a fan-out through shared state splits the knowledge in two: the library knows the subscription was scheduled at mount, and only this application knows the value came from somebody's click. Naming that click here is what lets an operator holding the patch that changed the number reach the interaction that changed it.

At most 64 identifiers, and they must be events of this session. The Emitter rejects a longer list with an error rather than truncating, so the effect learns about it and the reducer sees a deterministic failure.

func (*Store) WatchEffect

func (s *Store) WatchEffect() live.Effect[live.AnonymousIdentity]

WatchEffect subscribes this session to the store for as long as it lives.

The live.Session[live.AnonymousIdentity] is a parameter of Run and not something to fish out of the context: an effect's identity is an input to what the effect does, and a signature that omitted it would invite the effect to carry addressing information the library already has.

Jump to

Keyboard shortcuts

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