mirror of
https://github.com/MontFerret/ferret.git
synced 2025-11-23 21:54:45 +02:00
allow context cancellation in WAIT (#524)
* allow context cancellation in WAIT * cancel context before running the program
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
// Wait pauses the execution for a given period.
|
||||
// @param timeout (Float|Int) - Number value which indicates for how long to stop an execution.
|
||||
func Wait(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
func Wait(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 1, 1)
|
||||
|
||||
if err != nil {
|
||||
@@ -19,7 +19,13 @@ func Wait(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
|
||||
arg := values.ToInt(args[0])
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(arg))
|
||||
timer := time.NewTimer(time.Millisecond * time.Duration(arg))
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return values.None, ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
|
||||
return values.None, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user