Documentation
¶
Overview ¶
Package cooldown is used to make cooldowns in go. It has basic and nonbasic implementations.
Basic is very easy designed and have no features. Unlike others: ValuedHandler and CoolDown: they're starting timer via time.AfterFunc, that cancels itself, when cooldown expires.
Index ¶
- Variables
- type Basic
- func (cooldown *Basic) Active() bool
- func (cooldown *Basic) ActiveUnsafe() bool
- func (cooldown *Basic) Pause() bool
- func (cooldown *Basic) PauseUnsafe() bool
- func (cooldown *Basic) Paused() bool
- func (cooldown *Basic) PausedUnsafe() bool
- func (cooldown *Basic) Remaining() time.Duration
- func (cooldown *Basic) RemainingUnsafe() time.Duration
- func (cooldown *Basic) Reset()
- func (cooldown *Basic) ResetUnsafe()
- func (cooldown *Basic) Resume() bool
- func (cooldown *Basic) ResumeUnsafe() bool
- func (cooldown *Basic) Set(dur time.Duration)
- func (cooldown *Basic) SetUnsafe(dur time.Duration)
- func (cooldown *Basic) State() (res BasicState)
- func (cooldown *Basic) StateUnsafe() (state BasicState)
- func (cooldown *Basic) TogglePause() (paused bool)
- func (cooldown *Basic) TogglePauseUnsafe() (paused bool)
- type BasicState
- type Context
- type CoolDown
- func (cooldown *CoolDown) Active() bool
- func (cooldown *CoolDown) ActiveUnsafe() bool
- func (cooldown *CoolDown) Handle(handler Handler)
- func (cooldown *CoolDown) Handler() Handler
- func (cooldown *CoolDown) Pause() bool
- func (cooldown *CoolDown) PauseUnsafe() bool
- func (cooldown *CoolDown) Remaining() time.Duration
- func (cooldown *CoolDown) RemainingUnsafe() time.Duration
- func (cooldown *CoolDown) Renew()
- func (cooldown *CoolDown) RenewUnsafe()
- func (cooldown *CoolDown) Resume() bool
- func (cooldown *CoolDown) ResumeUnsafe() bool
- func (cooldown *CoolDown) Start(dur time.Duration)
- func (cooldown *CoolDown) StartUnsafe(dur time.Duration)
- func (cooldown *CoolDown) Stop()
- func (cooldown *CoolDown) StopUnsafe()
- func (cooldown *CoolDown) TogglePause() bool
- func (cooldown *CoolDown) TogglePauseUnsafe() bool
- func (cooldown *CoolDown) Valued() *Valued[struct{}]
- type Handler
- type NopHandler
- type NopValuedHandler
- func (NopValuedHandler[T]) HandlePause(*ValuedContext[T], T)
- func (NopValuedHandler[T]) HandleRenew(*ValuedContext[T], time.Duration, T)
- func (NopValuedHandler[T]) HandleResume(*ValuedContext[T], T)
- func (NopValuedHandler[T]) HandleStart(*ValuedContext[T], time.Duration, T)
- func (NopValuedHandler[T]) HandleStop(*Valued[T], StopCause, T)
- type Option
- type StopCause
- type Valued
- func (cooldown *Valued[T]) Active() bool
- func (cooldown *Valued[T]) ActiveUnsafe() bool
- func (cooldown *Valued[T]) Duration() time.Duration
- func (cooldown *Valued[T]) DurationUnsafe() time.Duration
- func (cooldown *Valued[T]) Handle(handler ValuedHandler[T])
- func (cooldown *Valued[T]) Handler() ValuedHandler[T]
- func (cooldown *Valued[T]) L() *sync.RWMutex
- func (cooldown *Valued[T]) Pause(val T) bool
- func (cooldown *Valued[T]) PauseUnsafe(val T) bool
- func (cooldown *Valued[T]) Paused() bool
- func (cooldown *Valued[T]) PausedUnsafe() bool
- func (cooldown *Valued[T]) Remaining() time.Duration
- func (cooldown *Valued[T]) RemainingUnsafe() time.Duration
- func (cooldown *Valued[T]) Renew(val T)
- func (cooldown *Valued[T]) RenewUnsafe(val T)
- func (cooldown *Valued[T]) Resume(val T) bool
- func (cooldown *Valued[T]) ResumeUnsafe(val T) bool
- func (cooldown *Valued[T]) Start(dur time.Duration, val T) bool
- func (cooldown *Valued[T]) StartUnsafe(dur time.Duration, val T) bool
- func (cooldown *Valued[T]) Stop(val T)
- func (cooldown *Valued[T]) StopUnsafe(val T)
- func (cooldown *Valued[T]) TogglePause(val T) bool
- func (cooldown *Valued[T]) TogglePauseUnsafe(val T) bool
- type ValuedContext
- type ValuedHandler
- type ValuedOption
Constants ¶
This section is empty.
Variables ¶
var ( // ErrStopCauseExpired used when cooldown is expired. ErrStopCauseExpired = errors.New("cooldown expired") // ErrStopCauseCancelled used when cooldown is canceled in event by user. ErrStopCauseCancelled = errors.New("cooldown cancelled") )
Functions ¶
This section is empty.
Types ¶
type Basic ¶
Basic represents very basic 'lazy' cooldown. You can create it simpy via new(cooldown.Basic).
func (*Basic) ActiveUnsafe ¶
func (*Basic) Pause ¶
Pause pauses cooldown if it is not already paused. Returns true if successfully paused.
func (*Basic) PauseUnsafe ¶
func (*Basic) PausedUnsafe ¶
func (*Basic) RemainingUnsafe ¶
func (*Basic) ResetUnsafe ¶
func (cooldown *Basic) ResetUnsafe()
func (*Basic) Resume ¶
Resume resumes the cooldown if it is paused. Returns true if successfully resumed.
func (*Basic) ResumeUnsafe ¶
func (*Basic) Set ¶
Set updates state of the cooldown. If provided duration is negative, cooldown will just reset.
func (*Basic) State ¶
func (cooldown *Basic) State() (res BasicState)
State returns the current state of the basic cooldown.
func (*Basic) StateUnsafe ¶
func (cooldown *Basic) StateUnsafe() (state BasicState)
func (*Basic) TogglePause ¶
TogglePause toggles the pause state of the cooldown. Returns true if cooldown was paused.
func (*Basic) TogglePauseUnsafe ¶
type BasicState ¶
type BasicState struct {
// Active marks if cooldown is active.
Active,
Paused bool
// Expiration is the expiration date of the cooldown.
// If cooldown is inactive, it'll be zero time.Time,
Expiration,
PausedDate time.Time
}
BasicState represents the state of Basic cooldown.
type CoolDown ¶
type CoolDown struct {
// contains filtered or unexported fields
}
CoolDown is the same Valued cooldown but without values. It uses underlying Valued with empty struct as value. The handler type is also different here, but its logic is the same, because it's just a wrapper for ValuedHandler.
func (*CoolDown) ActiveUnsafe ¶
func (*CoolDown) PauseUnsafe ¶
func (*CoolDown) RemainingUnsafe ¶
func (*CoolDown) RenewUnsafe ¶
func (cooldown *CoolDown) RenewUnsafe()
func (*CoolDown) ResumeUnsafe ¶
func (*CoolDown) StartUnsafe ¶
func (*CoolDown) StopUnsafe ¶
func (cooldown *CoolDown) StopUnsafe()
func (*CoolDown) TogglePauseUnsafe ¶
type Handler ¶
type Handler interface {
// HandleStart handles start of the cooldown allowing user to cancel it via
// context.
HandleStart(ctx *Context, dur time.Duration)
// HandleRenew handles cooldown renew allowing user to cancel it via
// context.
HandleRenew(ctx *Context, dur time.Duration)
// HandleStop handles stop of the cooldown. You can identify stop cause by
// errors.Is method. Example:
//
// switch cause {
// case cooldown.ErrStopCauseExpired:
// // ...
// case cooldown.ErrStopCauseCancelled:
// // ...
// }
HandleStop(cooldown *CoolDown, cause StopCause)
// HandlePause handles user pausing the cooldown allowing to cancel event
// via context.
HandlePause(ctx *Context)
// HandleResume handles user resuming the cooldown allowing to cancel event
// via context.
HandleResume(ctx *Context)
}
Handler allows to handle actions with CoolDown, additionally providing a Context allowing to cancel the event.
Note: you're NOT allowed to call locking CoolDown methods on handler events, because it is already in lock. Otherwise, it'll cause deadlock. Note, that you're still able to use Unsafe methods.
type NopHandler ¶
type NopHandler struct{}
NopHandler is no-operation implementation of Handler.
func (NopHandler) HandlePause ¶
func (NopHandler) HandlePause(*Context)
func (NopHandler) HandleRenew ¶
func (NopHandler) HandleRenew(*Context, time.Duration)
func (NopHandler) HandleResume ¶
func (NopHandler) HandleResume(*Context)
func (NopHandler) HandleStart ¶
func (NopHandler) HandleStart(*Context, time.Duration)
func (NopHandler) HandleStop ¶
func (NopHandler) HandleStop(*CoolDown, StopCause)
type NopValuedHandler ¶
type NopValuedHandler[T any] struct{}
NopValuedHandler is no-operation implementation of ValuedHandler.
func (NopValuedHandler[T]) HandlePause ¶
func (NopValuedHandler[T]) HandlePause(*ValuedContext[T], T)
func (NopValuedHandler[T]) HandleRenew ¶
func (NopValuedHandler[T]) HandleRenew(*ValuedContext[T], time.Duration, T)
func (NopValuedHandler[T]) HandleResume ¶
func (NopValuedHandler[T]) HandleResume(*ValuedContext[T], T)
func (NopValuedHandler[T]) HandleStart ¶
func (NopValuedHandler[T]) HandleStart(*ValuedContext[T], time.Duration, T)
func (NopValuedHandler[T]) HandleStop ¶
func (NopValuedHandler[T]) HandleStop(*Valued[T], StopCause, T)
type Option ¶
type Option = func(cd *CoolDown)
Option is the option implementation for default CoolDown.
func OptionHandler ¶
func OptionHandler(h Handler) Option
type Valued ¶
type Valued[T any] struct { // contains filtered or unexported fields }
Valued represents cooldown with renew ability and values. Point of values is to use them as transaction or context value.
func NewValued ¶
func NewValued[T any](opts ...ValuedOption[T]) *Valued[T]
NewValued creates new Valued cooldown.
func (*Valued[T]) ActiveUnsafe ¶
func (*Valued[T]) DurationUnsafe ¶
func (*Valued[T]) PauseUnsafe ¶
func (*Valued[T]) PausedUnsafe ¶
func (*Valued[T]) RemainingUnsafe ¶
func (*Valued[T]) RenewUnsafe ¶
func (cooldown *Valued[T]) RenewUnsafe(val T)
func (*Valued[T]) ResumeUnsafe ¶
func (*Valued[T]) StartUnsafe ¶
func (*Valued[T]) StopUnsafe ¶
func (cooldown *Valued[T]) StopUnsafe(val T)
func (*Valued[T]) TogglePauseUnsafe ¶
type ValuedHandler ¶
type ValuedHandler[T any] interface { // HandleStart handles start of the cooldown allowing user to cancel it via // context. HandleStart(ctx *ValuedContext[T], dur time.Duration, val T) // HandleRenew handles renew allowing user to cancel it via context. HandleRenew(ctx *ValuedContext[T], dur time.Duration, val T) // HandleStop handles stop of the cooldown. You can identify stop cause by // errors.Is method. Example: // // switch cause { // case cooldown.ErrStopCauseExpired: // // ... // case cooldown.ErrStopCauseCancelled: // // ... // } HandleStop(cooldown *Valued[T], cause StopCause, val T) // HandlePause handles user pausing the cooldown allowing to cancel event // via context. HandlePause(ctx *ValuedContext[T], val T) // HandleResume handles user resuming the cooldown allowing to cancel event // via context. HandleResume(ctx *ValuedContext[T], val T) }
ValuedHandler allows to handle actions with Valued, additionally providing a ValuedContext allowing to cancel the event.
Note: you're NOT allowed to call locking Valued methods on handler events, because it is already in lock. Otherwise, it'll cause deadlock. Note, that you're still able to use Unsafe methods.
type ValuedOption ¶
ValuedOption is option implementation for the Valued cooldown.
func ValuedOptionHandler ¶
func ValuedOptionHandler[T any](h ValuedHandler[T]) ValuedOption[T]