event

package
v0.0.0-...-389cf96 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Event

A simple event system, for triggering events at certain times.

Example use

Leet o'clock

package main

import (
	"fmt"
	"time"

	"github.com/xyproto/wallutils/pkg/event"
)

func main() {
	// Create a new event system, with a loop iteration delay of 1 second
	eventSys := event.NewSystem(1 * time.Second)
	// Add an event that will trigger every day at 13:37
	eventSys.ClockEvent(13, 37, func() error {
		fmt.Println("It's leet o'clock")
		return nil
	})
	// Run the event system (not verbose)
	eventSys.Run(false)
}

Clock

package main

import (
	"fmt"
	"time"

	"github.com/xyproto/wallutils/pkg/event"
)

func clockSystem() *event.EventSys {
	sys := event.NewSystem(1 * time.Second)
	for hour := 0; hour < 24; hour++ {
		for minute := 0; minute < 60; minute++ {
			// Create new variables that can be closed over by the new function below
			hour := hour
			minute := minute
			// Create a new event that will trigger at the specified hour and minute
			sys.ClockEvent(hour, minute, func() error {
				fmt.Printf("The clock is %02d:%02d\n", hour, minute)
				return nil
			})
		}
	}
	return sys
}

func main() {
	// Start the event system that will trigger an event at every minute
	clockSystem().RunBackground(false)
	// Wait endlessly while saying "tick" and "tock" every second
	for {
		fmt.Println("tick")
		time.Sleep(1 * time.Second)
		fmt.Println("tock")
		time.Sleep(1 * time.Second)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reset

func Reset(event Event)

Reset will remove a given event from the cool-off queue TODO: This function needs testing!

func ToToday

func ToToday(d time.Time) time.Time

ToToday moves the date of a given time.Time to today's date. The hour/minute/second is kept as it is.

Types

type Event

type Event interface {
	Trigger() error
	Hour() int
	Minute() int
	JustOnce() bool
}

Event is an interface that will trigger at a specific hour and minute. Trigger() is the function that will be triggered. JustOnce() is if it should only be triggered once, or each day.

type EventSys

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

EventSys represents an event system.

  • granularity is how long it should wait at each loop in the main loop
  • events is a map from Event to a margin of error on each side of the time when the event should kick in
  • coolOffGranularity is how long the system should wait per cool-off loop iteration

func NewSystem

func NewSystem(loopSleep time.Duration) *EventSys

NewSystem creates a new event system, where events can be registered and the event loop can be run. loopSleep is how long the event loop should sleep at every iteration.

func (*EventSys) ClockEvent

func (sys *EventSys) ClockEvent(h, m int, f func() error)

ClockEvent creates and registers an event that should happen at every HH:MM

func (*EventSys) EveryMinute

func (sys *EventSys) EveryMinute(h, m, n int, f func() error)

EveryMinute will trigger an event every minute for n minutes, starting from h:m

func (*EventSys) Register

func (sys *EventSys) Register(event Event)

Register will register an event with the event system.

func (*EventSys) Run

func (sys *EventSys) Run(verbose bool)

Run will start the event system in the foreground and never return

func (*EventSys) RunBackground

func (sys *EventSys) RunBackground(verbose bool)

RunBackground will start the event system in the background and immediately return

func (*EventSys) SimpleEvent

func (sys *EventSys) SimpleEvent(in time.Duration, once bool, f func() error)

SimpleEvent creates and registers an event that should happen in a certain amount of time from now, then may optionally be repeated at every matching hour and minute every 24 hours, if "once" is false.

type SimpleEvent

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

SimpleEvent is a simple event that will trigger a function at HH:MM

func NewClockEvent

func NewClockEvent(h, m int, f func() error) *SimpleEvent

NewClockEvent will create a simple event that can be triggered The event will trigger every time the hour and minute matches the one from time.Now()

func NewSimpleEvent

func NewSimpleEvent(in time.Duration, once bool, f func() error) *SimpleEvent

NewSimpleEvent will create a simple event that can be triggered

func NewTestEvent

func NewTestEvent() *SimpleEvent

NewTestEvent will create a new event that will trigger in 5 seconds along with a log message

func (*SimpleEvent) Hour

func (se *SimpleEvent) Hour() int

Hour will return the hour number for when the event should trigger

func (*SimpleEvent) JustOnce

func (se *SimpleEvent) JustOnce() bool

JustOnce returns true if the event should only ever trigger once

func (*SimpleEvent) Minute

func (se *SimpleEvent) Minute() int

Minute will return the minute number for when the event should trigger

func (*SimpleEvent) SetTriggerFunction

func (se *SimpleEvent) SetTriggerFunction(f func() error)

SetTriggerFunction can be used for replacing the trigger function for a single event

func (*SimpleEvent) Trigger

func (se *SimpleEvent) Trigger() error

Trigger will call the trigger function stored in the SimpleEvent struct

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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