loader

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package loader implements the ability to define a graph of tasks and dependencies that produce results, and solve the graph incrementally or totally, including concurrently.

For example, this could be used to implement a loading screen for a computer game with a progress bar that updates in real time, with images being decoded concurrently with files being loaded from disk, and synchronised with the main thread for safe OpenGL operations such as creating texture objects on the GPU.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Inputs

type Inputs struct {
	Named  map[string]Result
	Direct []Result
}

func (*Inputs) AssertNoErrors

func (n *Inputs) AssertNoErrors()

AssertNoErrors panics unless all the inputs (if any) have nil error values.

func (*Inputs) AssertNone

func (n *Inputs) AssertNone()

AssertNone panics unless the inputs are empty

func (*Inputs) AssertNoneDirect

func (n *Inputs) AssertNoneDirect()

AssertNoneDirect panics unless the direct inputs are empty

func (*Inputs) AssertNoneNamed

func (n *Inputs) AssertNoneNamed()

AssertNoneNamed panics unless the named inputs are empty

func (*Inputs) GetNamed

func (n *Inputs) GetNamed(name string) Result

GetNamed gets a named input from the input set. It panics if an input with that name does not exist

type Loader

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

Loader is a dependency graph (represented as a DAG) modelling tasks that perform a unit of work, either alone or on the results of some previous tasks that must complete first.

func New

func New() *Loader

New creates a new Loader.

func (*Loader) Add

func (l *Loader) Add(t Task, namedInputs map[string]Vertex, directInputs []Vertex) Vertex

Add creates a new vertex in a loader dependency graph. Inputs are provided as arguments to the [Task.Load] method.

func (*Loader) Result

func (l *Loader) Result(v Vertex) Result

func (*Loader) Step

func (l *Loader) Step() bool

Step performs (or continues to perform) each task in the dependency graph, where possible using concurrency. It does not block, and must be repeatedly called until done, when it returns false. If busy-waiting, it is a good idea to have some sort of small delay between each iteration.

func (*Loader) Walk

func (l *Loader) Walk()

Walk performs each task in the dependency graph, where possible using concurrency. It blocks until complete.

func (*Loader) WalkSynchronous

func (l *Loader) WalkSynchronous(
	visit func(t Task, i int, total int),
)

WalkSynchronous performs each task in the dependency graph, in topological sorted order, one-by-one in sequence until complete. The visit function is a callback, called for each task, where i is the progress out of the total number of tasks.

type Manager

type Manager interface {
	Queue(WorkerTask)
	Start(chan WorkerResult)
	Stop()
}

type ManagerAsync

type ManagerAsync struct {
	MaxWorkers int
	// contains filtered or unexported fields
}

func (*ManagerAsync) Queue

func (m *ManagerAsync) Queue(t WorkerTask)

func (*ManagerAsync) Start

func (m *ManagerAsync) Start(resultChan chan<- WorkerResult, bufSize int)

func (*ManagerAsync) Stop

func (m *ManagerAsync) Stop()

type ManagerSync

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

func (*ManagerSync) Queue

func (m *ManagerSync) Queue(t WorkerTask)

func (*ManagerSync) Start

func (m *ManagerSync) Start(resultChan chan<- WorkerResult, bufSize int)

func (*ManagerSync) Stop

func (m *ManagerSync) Stop()

type Result

type Result struct {
	Value any
	Err   error
}

type Task

type Task struct {

	// Description is a present-tense sentence fragment describing what the
	// task is doing e.g. "reading file" or "decoding image".
	Description string

	Load func(inputs Inputs) (any, error)
	Free func(input Result) error
	// contains filtered or unexported fields
}

type Vertex

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

Vertex represents a task in the loader dependency graph.

func (Vertex) Keep

func (v Vertex) Keep()

func (Vertex) Require

func (v Vertex) Require()

type WorkerResult

type WorkerResult struct {
	ID     int
	Result Result
}

type WorkerTask

type WorkerTask struct {
	ID     int
	Inputs Inputs
	Work   func(Inputs) Result
}

Jump to

Keyboard shortcuts

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