mutable

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: MIT Imports: 2 Imported by: 9

Documentation

Overview

Package mutable provides types to make DSP components mutable.

Each DSP component is running in its own goroutine. The only way to avoid data races and locking is to mutate components in that same goroutine. To achieve that, two things must be done: the component should be mutable and component should have defined mutations.

Mutable component

Mutability is a part of DSP component structures. Nil value of mutability is immutable. To make it mutable, the one should do the following:

Mutability: mutable.Mutable()

This will make component recognized as mutable during pipe start and enable mutations execution. See examples for more details.

Example (Mutation)
package main

import (
	"fmt"

	"pipelined.dev/pipe/mutable"
)

type mutableType struct {
	mutable.Context
	parameter int
}

func (v *mutableType) setParameter(value int) mutable.Mutation {
	return v.Context.Mutate(func() error {
		v.parameter = value
		return nil
	})
}

func main() {
	// create new mutable component
	component := &mutableType{
		Context: mutable.Mutable(),
	}
	fmt.Println(component.parameter)

	// create new mutation
	mutation := component.setParameter(10)
	fmt.Println(component.parameter)

	// apply mutation
	mutation.Apply()
	fmt.Println(component.parameter)

}
Output:
0
0
10

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context [16]byte

Context can be embedded to make structure behaviour mutable.

func Immutable

func Immutable() Context

Immutable returns immutable mutable.

func Mutable

func Mutable() Context

Mutable returns new mutable mutable.

func (Context) IsMutable

func (c Context) IsMutable() bool

IsMutable returns true if object is mutable.

func (Context) Mutate

func (c Context) Mutate(m MutatorFunc) Mutation

Mutate associates provided mutator with mutable and return mutation.

type Destination added in v0.10.0

type Destination chan Mutations

Destination is a channel that used as source of mutations.

func NewDestination added in v0.11.0

func NewDestination() Destination

type Mutation

type Mutation struct {
	Context
	// contains filtered or unexported fields
}

Mutation is mutator function associated with a certain mutable context.

func (Mutation) Apply

func (m Mutation) Apply()

Apply mutator function.

type Mutations

type Mutations map[Context][]MutatorFunc

Mutations is a set of Mutations mapped their Mutables.

func (Mutations) Append

func (ms Mutations) Append(source Mutations) Mutations

Append param set to another set.

func (Mutations) ApplyTo

func (ms Mutations) ApplyTo(id Context) error

ApplyTo consumes Mutations defined for consumer in this param set.

func (Mutations) Detach

func (ms Mutations) Detach(id Context) Mutations

Detach params for provided component id.

func (Mutations) Put

func (ms Mutations) Put(m Mutation) Mutations

Put mutation to the set of Mutations.

type MutatorFunc

type MutatorFunc func() error

MutatorFunc mutates the object.

type Pusher added in v0.10.0

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

Pusher allows to push mutations to mutable contexts.

func NewPusher added in v0.10.0

func NewPusher() Pusher

NewPusher creates new pusher.

func (Pusher) AddDestination added in v0.10.0

func (p Pusher) AddDestination(ctx Context, mc chan Mutations)

AddDestination adds new mapping of mutable context to destination.

func (Pusher) Push added in v0.10.0

func (p Pusher) Push(ctx context.Context)

Push mutations to the destinations.

func (Pusher) Put added in v0.10.0

func (p Pusher) Put(mutations ...Mutation)

Put mutations to the pusher. Function will panic if pusher contains unknown context.

Jump to

Keyboard shortcuts

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