2021-05-26 04:17:53 +00:00
|
|
|
package js
|
|
|
|
|
|
|
|
import (
|
2021-09-20 23:34:25 +00:00
|
|
|
"fmt"
|
2021-05-26 04:17:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sleepFunc(call otto.FunctionCall) otto.Value {
|
|
|
|
arg := call.Argument(0)
|
|
|
|
ms, _ := arg.ToInteger()
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * time.Duration(ms))
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
2021-09-20 23:34:25 +00:00
|
|
|
func AddUtilAPI(vm *otto.Otto) error {
|
2021-05-26 04:17:53 +00:00
|
|
|
util, _ := vm.Object("({})")
|
2021-09-20 23:34:25 +00:00
|
|
|
if err := util.Set("Sleep", sleepFunc); err != nil {
|
|
|
|
return fmt.Errorf("unable to set sleep func: %w", err)
|
|
|
|
}
|
2021-05-26 04:17:53 +00:00
|
|
|
|
2021-09-20 23:34:25 +00:00
|
|
|
if err := vm.Set("util", util); err != nil {
|
|
|
|
return fmt.Errorf("unable to set util: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-05-26 04:17:53 +00:00
|
|
|
}
|