persistence

package
v0.0.0-...-c785a00 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package persistence defines where the state of shared documents is stored, keyed by room name as in ygo's persistence package. A store holds the head state of each room, which is the full text of the document. There is no history: storing replaces what was stored before.

Index

Constants

This section is empty.

Variables

View Source
var ErrRoomNotFound = errors.New("persistence: room not found")

ErrRoomNotFound is returned (wrapped) by Load when nothing was stored for the room yet. Test for it with errors.Is.

Functions

func WriteAtomic

func WriteAtomic(path string, data []byte) error

WriteAtomic replaces path through a temporary file in the same directory, so other processes never see a half-written file.

Types

type FilePersistence

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

FilePersistence keeps the state of a room in the file dir/room, so that other programs can read and edit it. It implements Watcher.

func NewFilePersistence

func NewFilePersistence(dir string) (*FilePersistence, error)

NewFilePersistence opens (creating if necessary) a store in the directory dir.

func (*FilePersistence) Load

func (f *FilePersistence) Load(ctx context.Context, room string) ([]byte, error)

func (*FilePersistence) Store

func (f *FilePersistence) Store(ctx context.Context, room string, state []byte) error

func (*FilePersistence) Watch

func (f *FilePersistence) Watch(ctx context.Context, room string) (<-chan struct{}, error)

Watch reports every event on the file of room. The directory is watched, not the file: editors that save by renaming and WriteAtomic replace the file, which ends a file watch.

type MemoryPersistence

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

MemoryPersistence keeps the state of rooms in memory. It is safe for concurrent use.

func NewMemoryPersistence

func NewMemoryPersistence() *MemoryPersistence

func (*MemoryPersistence) Load

func (m *MemoryPersistence) Load(_ context.Context, room string) ([]byte, error)

func (*MemoryPersistence) Store

func (m *MemoryPersistence) Store(_ context.Context, room string, state []byte) error

type Persistence

type Persistence interface {
	// Load returns the stored state of room, or an error wrapping
	// ErrRoomNotFound if there is none.
	Load(ctx context.Context, room string) ([]byte, error)
	// Store replaces the state of room.
	Store(ctx context.Context, room string, state []byte) error
}

Persistence stores the head state of rooms.

type Watcher

type Watcher interface {
	// Watch sends a value whenever the state of room may have changed. The
	// channel is closed when ctx is done. Sends never block: values are
	// dropped while one is already pending.
	Watch(ctx context.Context, room string) (<-chan struct{}, error)
}

Watcher is implemented by stores that can tell when the state of a room was changed by someone else. Consumers that find it on a store use it to react sooner than polling would.

Jump to

Keyboard shortcuts

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