2022-04-12 23:28:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-07-21 15:37:08 +00:00
|
|
|
// "fmt"
|
|
|
|
// "os"
|
2022-04-12 23:28:25 +00:00
|
|
|
"time"
|
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
// "hilbish/moonlight"
|
|
|
|
|
2022-04-12 23:28:25 +00:00
|
|
|
rt "github.com/arnodel/golua/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type timerType int64
|
|
|
|
const (
|
|
|
|
timerInterval timerType = iota
|
|
|
|
timerTimeout
|
|
|
|
)
|
|
|
|
|
2023-02-07 19:39:22 +00:00
|
|
|
// #type
|
2023-01-18 10:39:26 +00:00
|
|
|
// #interface timers
|
|
|
|
// #property type What type of timer it is
|
|
|
|
// #property running If the timer is running
|
|
|
|
// #property duration The duration in milliseconds that the timer will run
|
|
|
|
// The Job type describes a Hilbish timer.
|
2022-04-12 23:28:25 +00:00
|
|
|
type timer struct{
|
|
|
|
id int
|
|
|
|
typ timerType
|
|
|
|
running bool
|
|
|
|
dur time.Duration
|
|
|
|
fun *rt.Closure
|
2022-12-15 04:00:54 +00:00
|
|
|
th *timersModule
|
2022-04-12 23:28:25 +00:00
|
|
|
ticker *time.Ticker
|
2022-05-28 23:03:44 +00:00
|
|
|
ud *rt.UserData
|
2022-05-22 01:39:14 +00:00
|
|
|
channel chan struct{}
|
2022-04-12 23:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *timer) start() error {
|
|
|
|
if t.running {
|
|
|
|
return errors.New("timer is already running")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.running = true
|
|
|
|
t.th.running++
|
2022-05-22 01:58:58 +00:00
|
|
|
t.th.wg.Add(1)
|
2022-04-12 23:28:25 +00:00
|
|
|
t.ticker = time.NewTicker(t.dur)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.ticker.C:
|
2024-07-21 15:37:08 +00:00
|
|
|
/*
|
|
|
|
_, err := l.Call1(moonlight.FunctionValue(t.fun))
|
2022-04-12 23:28:25 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error in function:\n", err)
|
|
|
|
t.stop()
|
|
|
|
}
|
|
|
|
// only run one for timeout
|
|
|
|
if t.typ == timerTimeout {
|
|
|
|
t.stop()
|
|
|
|
}
|
2024-07-21 15:37:08 +00:00
|
|
|
*/
|
2022-04-12 23:28:25 +00:00
|
|
|
case <-t.channel:
|
|
|
|
t.ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *timer) stop() error {
|
|
|
|
if !t.running {
|
|
|
|
return errors.New("timer not running")
|
|
|
|
}
|
|
|
|
|
2022-05-22 01:39:14 +00:00
|
|
|
t.channel <- struct{}{}
|
2022-04-12 23:28:25 +00:00
|
|
|
t.running = false
|
|
|
|
t.th.running--
|
2022-05-22 01:58:58 +00:00
|
|
|
t.th.wg.Done()
|
2022-04-12 23:28:25 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface timers
|
|
|
|
// #member
|
|
|
|
// start()
|
|
|
|
// Starts a timer.
|
2022-05-28 23:03:44 +00:00
|
|
|
func timerStart(thr *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := timerArg(c, 0)
|
2022-04-12 23:28:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-28 23:03:44 +00:00
|
|
|
err = t.start()
|
2022-04-12 23:28:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Next(), nil
|
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface timers
|
|
|
|
// #member
|
|
|
|
// stop()
|
|
|
|
// Stops a timer.
|
2022-05-28 23:03:44 +00:00
|
|
|
func timerStop(thr *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := timerArg(c, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-04-12 23:28:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 23:03:44 +00:00
|
|
|
err = t.stop()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-12 23:28:25 +00:00
|
|
|
|
2022-05-28 23:03:44 +00:00
|
|
|
return c.Next(), nil
|
2022-04-12 23:28:25 +00:00
|
|
|
}
|