...

Package timed

import "github.com/Diarkis/diarkis/timed"
Overview
Index

Overview ▾

Index ▾

func Once(wait int64, callback func(interface{}), options interface{})
func SerializeNumber(num *Number) string
func SerializeState(state *State) string
type Event
    func NewEvent(interval int64) (*Event, error)
    func (event *Event) OnError(callback func(error))
    func (event *Event) OnTick(callback func())
    func (event *Event) Start()
    func (event *Event) Stop()
type Number
    func DeserializeNumber(str string) (*Number, error)
    func NewNumber(start, min, max, step int, interval int64) (*Number, error)
    func (num *Number) Decr(val int)
    func (num *Number) Decrementer()
    func (num *Number) GetCurrentValue() int
    func (num *Number) GetProperties() (int, int, int, int, int64)
    func (num *Number) Incr(val int)
    func (num *Number) Incrementer()
    func (num *Number) IsDecrementer() bool
    func (num *Number) IsIncrementer() bool
    func (num *Number) Set(val int)
    func (num *Number) Start() bool
    func (num *Number) ToMax()
    func (num *Number) ToMin()
type Queue
    func NewQueue(interval int64) *Queue
    func (q *Queue) Add(operation func())
    func (q *Queue) Reset()
    func (q *Queue) Start()
    func (q *Queue) Stop()
type State
    func DeserializeState(str string) (*State, error)
    func NewState(states []int, start int, interval int64) (*State, error)
    func (state *State) Back(step int)
    func (state *State) Backward()
    func (state *State) Circular()
    func (state *State) Forward()
    func (state *State) GetCurrentState() int
    func (state *State) GetProperties() ([]int, int64)
    func (state *State) Next(step int)
    func (state *State) NonCircular()
    func (state *State) Start()
    func (state *State) ToEnd()
    func (state *State) ToStart()

func Once

func Once(wait int64, callback func(interface{}), options interface{})

Once executes the given callback function after the given time (in milliseconds) once.

func SerializeNumber

func SerializeNumber(num *Number) string

SerializeNumber serializes an instance of timed.Number into a string

func SerializeState

func SerializeState(state *State) string

SerializeState serializes a state.

type Event

Event represents an event that occurs at certain interval and executes pre-registered operations

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

func NewEvent

func NewEvent(interval int64) (*Event, error)

NewEvent creates an instance of *timed.Event

Parameters

interval - Interval in seconds for the event to be triggered.

func (*Event) OnError

func (event *Event) OnError(callback func(error))

OnError registers a callback function to be executed if callbacks panics at event tick

func (*Event) OnTick

func (event *Event) OnTick(callback func())

OnTick registers a callback function to be executed at every event interval (tick)

func (*Event) Start

func (event *Event) Start()

Start starts the timed event

func (*Event) Stop

func (event *Event) Stop()

Stop stops the timed event

type Number

Number represents a number (int) that changes over time either increment or decrement as time passes

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

func DeserializeNumber

func DeserializeNumber(str string) (*Number, error)

DeserializeNumber deserializes a serialized string of an instance of timed.Number

func NewNumber

func NewNumber(start, min, max, step int, interval int64) (*Number, error)

NewNumber creates an instance of *timed.Number.

Parameters

start    - Initial value to start from.
min      - Minimum allowed value.
max      - Maximum allowed value.
interval - Interval in seconds for the value to be updated.

func (*Number) Decr

func (num *Number) Decr(val int)

Decr decrements the value by the given value

func (*Number) Decrementer

func (num *Number) Decrementer()

Decrementer instance will decrement by step at each interval

func (*Number) GetCurrentValue

func (num *Number) GetCurrentValue() int

GetCurrentValue returns the current value

func (*Number) GetProperties

func (num *Number) GetProperties() (int, int, int, int, int64)

GetProperties returns all properties: start, min, max, step, interval

func (*Number) Incr

func (num *Number) Incr(val int)

Incr increments the value by the given value

func (*Number) Incrementer

func (num *Number) Incrementer()

Incrementer instance will increment by step at each interval

func (*Number) IsDecrementer

func (num *Number) IsDecrementer() bool

IsDecrementer returns true if the instance is decrementer.

func (*Number) IsIncrementer

func (num *Number) IsIncrementer() bool

IsIncrementer returns true if the instance is incrementer.

func (*Number) Set

func (num *Number) Set(val int)

Set sets the current value to be the given value

func (*Number) Start

func (num *Number) Start() bool

Start starts the instance

func (*Number) ToMax

func (num *Number) ToMax()

ToMax sets the current value to be max

func (*Number) ToMin

func (num *Number) ToMin()

ToMin sets the current value to be min

type Queue

Queue represents the data structure of a function queue

type Queue struct {
    sync.RWMutex
    // contains filtered or unexported fields
}

func NewQueue

func NewQueue(interval int64) *Queue

NewQueue creates a new timed queue with interval.

[IMPORTANT] If interval is 0, queue will not start and Add() will execute operation immediately

func (*Queue) Add

func (q *Queue) Add(operation func())

Add adds a new operation into queue

func (*Queue) Reset

func (q *Queue) Reset()

Reset stops queue and discards all operations in the queue

func (*Queue) Start

func (q *Queue) Start()

Start starts the queue

func (*Queue) Stop

func (q *Queue) Stop()

Stop stops queue, but keeps the items in the queue

type State

State represents a state (int) that changes over time either forward or backward as time passes

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

func DeserializeState

func DeserializeState(str string) (*State, error)

DeserializeState deserializes a serialized state.

func NewState

func NewState(states []int, start int, interval int64) (*State, error)

NewState creates an instance of *timed.State.

Parameters

states   - An array of available states.
           The order of the array will be the order to state change.
start    - Starting index of the states array.
interval - Interval in seconds for the state to change.

func (*State) Back

func (state *State) Back(step int)

Back moves the state backwards by the given value

func (*State) Backward

func (state *State) Backward()

Backward the change direction of the state will be backward

func (*State) Circular

func (state *State) Circular()

Circular defines the state to be a circular - meaning the change of state does not stop

func (*State) Forward

func (state *State) Forward()

Forward the change direction of the state will be forward

func (*State) GetCurrentState

func (state *State) GetCurrentState() int

GetCurrentState returns the current state

func (*State) GetProperties

func (state *State) GetProperties() ([]int, int64)

GetProperties returns the properties: states, interval

func (*State) Next

func (state *State) Next(step int)

Next moves the state forward by the given value

func (*State) NonCircular

func (state *State) NonCircular()

NonCircular defines the state to be a non-circular - meaning the change of state stops at the either beginning or the end

func (*State) Start

func (state *State) Start()

Start starts the state instance

func (*State) ToEnd

func (state *State) ToEnd()

ToEnd moves the state to the end state of the states array

func (*State) ToStart

func (state *State) ToStart()

ToStart moves the state to the first state of the states array