filewatch

package module
v0.0.0-...-6c3834a Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2025 License: MIT Imports: 13 Imported by: 0

README

File watch state for the Simple State Machine

Build Status

This implements a way to include filewatch logic in your state machine built with the SSM library.

	printEventFn := func(fsnotify.Event) ssm.Fn {
		_, _ = fmt.Fprintf(out, "Event: %#v\n", event)
		return Stop
	}

	watchTemp := Watch(On(printEventFn, os.TempDir()))

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	fmt.Printf("Err: %v\n", ssm.Run(ctx, watchTemp))

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Stop = func(ctx context.Context) ssm.Fn {
		return ssm.ErrorEnd(stopErr)
	}
)

Functions

func IsStop

func IsStop(fn ssm.Fn) bool

func New

func New() (*w, error)

func Watch

func Watch(eventMappers ...PathEventMapper) ssm.Fn
Example
t := testing.T{}
tempDir := filepath.Join(os.TempDir(), "test")
defer func() {
	_ = os.RemoveAll(tempDir)
}()

mkdir(&t, tempDir)
go func() {
	eventSeparator()
	touch(&t, filepath.Join(tempDir, "1"))
}()

printEventFn := func(out io.Writer) func(fsnotify.Event) ssm.Fn {
	return func(event fsnotify.Event) ssm.Fn {
		_, _ = fmt.Fprintf(out, "Event: %#v\n", event)
		return Stop
	}
}

ww := Watch(On(printEventFn(os.Stdout), tempDir))
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

err := ssm.Run(ctx, ww)
fmt.Printf("Err: %v\n", err)
Output:

Event: fsnotify.Event{Name:"/tmp/test/1", Op:0x1, renamedFrom:""}
Err: stop

Types

type EventFn

type EventFn func(fsnotify.Event) ssm.Fn

type PathEventMapper

type PathEventMapper interface {
	Paths() []string
	Event() EventFn
}

func On

func On(fn EventFn, paths ...string) PathEventMapper

Jump to

Keyboard shortcuts

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