Skip to content

Commit

Permalink
Added EventLoop.Terminate() which cancels all active timers and preve…
Browse files Browse the repository at this point in the history
…nts further job submission. Closes #81
  • Loading branch information
dop251 committed Jul 26, 2024
1 parent 5de51f3 commit 5f9c081
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions eventloop/eventloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ func (loop *EventLoop) setImmediate(call goja.FunctionCall) goja.Value {
func (loop *EventLoop) SetTimeout(fn func(*goja.Runtime), timeout time.Duration) *Timer {
t := loop.newTimeout(func() { fn(loop.vm) })
if loop.addAuxJob(func() {
t.start(loop, timeout)
loop.jobCount++
t.idx = len(loop.jobs)
loop.jobs = append(loop.jobs, &t.job)
}) {
t.start(loop, timeout)
return t
}
return nil
Expand All @@ -192,11 +192,11 @@ func (loop *EventLoop) ClearTimeout(t *Timer) {
func (loop *EventLoop) SetInterval(fn func(*goja.Runtime), timeout time.Duration) *Interval {
i := loop.newInterval(func() { fn(loop.vm) })
if loop.addAuxJob(func() {
i.start(loop, timeout)
loop.jobCount++
i.idx = len(loop.jobs)
loop.jobs = append(loop.jobs, &i.job)
}) {
i.start(loop, timeout)
return i
}
return nil
Expand Down
12 changes: 12 additions & 0 deletions eventloop/eventloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,18 @@ func TestNativeClearInterval(t *testing.T) {
}
}

func TestSetAndClearOnStoppedLoop(t *testing.T) {
t.Parallel()
loop := NewEventLoop()
timeout := loop.SetTimeout(func(runtime *goja.Runtime) {
panic("must not run")
}, 1*time.Millisecond)
loop.ClearTimeout(timeout)
loop.Start()
time.Sleep(10 * time.Millisecond)
loop.Terminate()
}

func TestSetTimeoutConcurrent(t *testing.T) {
t.Parallel()
loop := NewEventLoop()
Expand Down

0 comments on commit 5f9c081

Please sign in to comment.