event

package module
v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2019 License: MIT Imports: 3 Imported by: 3

README

Event Build Status GoDoc License Go Report Card

A simple event system.

General info

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Between

func Between(t, a, b time.Time) bool

Between returns true if the given time t is between the two timestamps a (inclusive) and b (exclusive)

Example
before := time.Now()
A := time.Now()
t := time.Now()
B := time.Now()
after := time.Now()

fmt.Println(Between(t, A, B))          // true
fmt.Println(Between(t, before, A))     // false
fmt.Println(Between(t, B, after))      // false
fmt.Println(Between(t, before, after)) // true
fmt.Println(Between(t, before, B))     // true
fmt.Println(Between(t, A, after))      // true
fmt.Println(Between(t, after, before)) // false

fmt.Println(Between(t, t, B)) // true (from inclusive, to exclusive)
fmt.Println(Between(t, A, t)) // false (from inclusive, to exclusive)
Output:
true
false
false
true
true
true
false
true
false

func ProgressWrapper

func ProgressWrapper(from, upto time.Time, progressFunction func(float64)) func()

ProgressWrapper wraps a function that takes a float64 in such a way that a float between 0 and 1 is passed in when it is called. The float signifies how far time has progressed from the given `from` time up to the given `upto` time. The wrapped function that takes no arguments is returned. The final "100%" (1.0) value will never be sent to the wrapped function, because the `upto` time is exclusive, not inclusive.

func ProgressWrapperInterval

func ProgressWrapperInterval(from, upto time.Time, interval time.Duration, progressFunction func(float64)) func()

ProgressWrapperInterval behaves like ProgressWrapper, except that an interval is given, that lets the function receive a "1.0" value at the final run, when progress is complete. The interval duration is subtracted from the `upto` time, at the time when the progress float is calculated.

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.

func ToTomorrow

func ToTomorrow(d time.Time) time.Time

ToTomorrow 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 struct {
	// contains filtered or unexported fields
}

Event stores a time window for when the event can be triggered (`from` up to `upTo`), how long it should take to cooldown before being able to be re-triggered, which action should be performed when triggered, when it was last triggered, a mutex and a boolean variable for keeping track of if the action is still ongoing or not.

func New

func New(when time.Time, window, cooldown time.Duration, action func()) *Event

New creates a new Event, that should happen at the given "when" time, within the given time window, with an associated cooldown period after the event has been triggered. The event can be retriggered after every cooldown, within the time window. Only hour/minute/second will be considered.

func NewDateEvent

func NewDateEvent(when time.Time, window, cooldown time.Duration, action func()) *Event

NewDateEvent is like New, but the date will also be considered when triggering events.

func (*Event) Cooldown

func (e *Event) Cooldown() time.Duration

Cooldown is how long to wait after the event has been triggered, before being possible to trigger again.

func (*Event) Duration

func (e *Event) Duration() time.Duration

Duration is for how long the window that this event can be triggered is

func (*Event) From

func (e *Event) From() time.Time

From is the time from when the event should be able to be triggered.

func (*Event) Has

func (e *Event) Has(t time.Time) bool

Has checks if the Event has time t in its interval: from p.From() and up to but not including p.UpTo()

func (*Event) ShouldTrigger

func (e *Event) ShouldTrigger() (retval bool)

ShouldTrigger returns true if the current time is in the interval of the event AND it is not ongoing AND it is not in the cooldown period.

func (*Event) String

func (e *Event) String() string

String returns a string with information about this event

func (*Event) Trigger

func (e *Event) Trigger()

Trigger triggers this event. The trigger time is noted, the associated action is performed and a cooldown period is initiated with time.Sleep. It is expected that this function will be called as a goroutine.

func (*Event) UpTo

func (e *Event) UpTo() time.Time

UpTo is the time where the event should no longer be able to be triggered.

type Loop

type Loop []*Event

Loop is a collection of events

func NewLoop

func NewLoop() *Loop

NewLoop creates an empty collection of events

func (*Loop) Add

func (l *Loop) Add(e *Event)

Add adds an event to the collection

func (*Loop) Go

func (l *Loop) Go(sleep time.Duration)

Go launches an event loop that will sleep the given duration at every loop.

func (*Loop) Once

func (l *Loop) Once(when time.Time, action func())

Once runs a given action only once, within a 1 second window of time

func (*Loop) OnceWindow

func (l *Loop) OnceWindow(when time.Time, window time.Duration, action func())

OnceWindow runs a given action only once, within a custom duration

Jump to

Keyboard shortcuts

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