helpers

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: MIT Imports: 7 Imported by: 0

README

/pkg/helpers

-> go back to monorepo /

[!NOTE] asyncmachine can transform blocking APIs into controllable state machines with ease. It shares similarities with Ergo's actor model, and focuses on distributed workflows like Temporal. It's lightweight and most features are optional.

/pkg/helpers - because of the minimalistic approach of asyncmachine, helpers and sugars often end up in this package.

Import

import amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"

Synchronous Calls

Synchronous wrappers of async state machine calls, which assume a single, blocking scenario controlled with context. Multi states are handled automatically.

Example - add state StateNameSelected and wait until it becomes active

res := amhelp.Add1Block(ctx, mach, ss.StateNameSelected, am.A{"state": state})
print(mach.Is1(ss.StateNameSelected)) // true
print(res) // am.Executed or am.Canceled, never am.Queued

Example - wait for ScrollToTx, triggered by ScrollToMutTx

res := amhelp.Add1AsyncBlock(ctx, mach, ss.ScrollToTx, ss.ScrollToMutTx, am.A{
    "state": state,
    "fwd":   true,
})

print(mach.Is1(ss.ScrollToTx)) // true
print(res) // am.Executed or am.Canceled, never am.Queued

Debugging

Example - enable telemetry for am-dbg

// read env
amDbgAddr := os.Getenv("AM_DBG_ADDR")
logLvl := am.EnvLogLevel("")

// debug
amhelp.MachDebug(mach, amDbgAddr, logLvl, true)

Example - enable telemetry for am-dbg using conventional env vars.

// debug
amhelp.MachDebugEnv(mach, true)

Documentation

monorepo

Go back to the monorepo root to continue reading.

Documentation

Overview

Package helpers is a set of useful functions when working with state machines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add1AsyncBlock

func Add1AsyncBlock(
	ctx context.Context, mach types.MachineApi, waitState string,
	addState string, args am.A,
) am.Result

Add1AsyncBlock adds a state from an async op and waits for another one from the op to become active. Theoretically, it should work with any state pair, including Multi states.

func Add1Block

func Add1Block(
	ctx context.Context, mach types.MachineApi, state string, args am.A,
) am.Result

Add1Block activates a state and waits until it becomes active. If it's a multi state, it also waits for it te de-activate. Returns early if a non-multi state is already active. Useful to avoid the queue.

func IndexesToStates

func IndexesToStates(mach types.MachineApi, indexes []int) am.S

IndexesToStates converts a list of state indexes to a list of state names, for a given machine.

func IsMulti

func IsMulti(mach types.MachineApi, state string) bool

IsMulti returns true if a state is a multi state.

func MachDebug

func MachDebug(mach *am.Machine, amDbgAddr string, logLvl am.LogLevel,
	stdout bool,
)

MachDebug sets up a machine for debugging, based on the AM_DEBUG env var, passed am-dbg address, log level and stdout flag.

func MachDebugEnv

func MachDebugEnv(mach *am.Machine, stdout bool)

MachDebugEnv sets up a machine for debugging, based on env vars only.

func MachDebugT

func MachDebugT(t *testing.T, mach *am.Machine, amDbgAddr string,
	logLvl am.LogLevel, stdout bool,
)

MachDebugT sets up a machine for debugging in tests, based on the AM_DEBUG env var, passed am-dbg address, log level and stdout flag.

func StatesToIndexes

func StatesToIndexes(mach types.MachineApi, states am.S) []int

StatesToIndexes converts a list of state names to a list of state indexes, for a given machine.

Types

This section is empty.

Jump to

Keyboard shortcuts

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