Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

take into account ctx.Done() when running timers #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions timers/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
package timers

import (
"context"
"fmt"

"rogchap.com/v8go"
)

func InjectTo(iso *v8go.Isolate, global *v8go.ObjectTemplate) error {
t := NewTimers()
func InjectTo(ctx context.Context, iso *v8go.Isolate, global *v8go.ObjectTemplate) error {
t := NewTimers(ctx)

for _, f := range []struct {
Name string
Expand Down
28 changes: 17 additions & 11 deletions timers/internal/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package internal

import (
"context"
"time"
)

Expand Down Expand Up @@ -53,25 +54,30 @@ func (t *Item) Clear() {
t.Done = true
}

func (t *Item) Start() {
func (t *Item) Start(ctx context.Context) {
go func() {
defer t.Clear() // self clear

ticker := time.NewTicker(time.Duration(t.Delay) * time.Millisecond)
defer ticker.Stop()

for range ticker.C {
if t.Done {
break
}
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if t.Done {
return
}

if t.FunctionCB != nil {
t.FunctionCB()
}
if t.FunctionCB != nil {
t.FunctionCB()
}

if !t.Interval {
t.Done = true
break
if !t.Interval {
t.Done = true
return
}
}
}
}()
Expand Down
13 changes: 8 additions & 5 deletions timers/timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package timers

import (
"context"
"errors"

"go.kuoruan.net/v8go-polyfills/timers/internal"
Expand All @@ -40,22 +41,24 @@ type Timers interface {
type timers struct {
Items map[int32]*internal.Item
NextItemID int32
ctx context.Context
}

const initNextItemID = 1

func NewTimers() Timers {
func NewTimers(ctx context.Context) Timers {
return &timers{
Items: make(map[int32]*internal.Item),
NextItemID: initNextItemID,
ctx: ctx,
}
}

func (t *timers) GetSetTimeoutFunctionCallback() v8go.FunctionCallback {
return func(info *v8go.FunctionCallbackInfo) *v8go.Value {
ctx := info.Context()

id, err := t.startNewTimer(info.This(), info.Args(), false)
id, err := t.startNewTimer(t.ctx, info.This(), info.Args(), false)
if err != nil {
return newInt32Value(ctx, 0)
}
Expand All @@ -68,7 +71,7 @@ func (t *timers) GetSetIntervalFunctionCallback() v8go.FunctionCallback {
return func(info *v8go.FunctionCallbackInfo) *v8go.Value {
ctx := info.Context()

id, err := t.startNewTimer(info.This(), info.Args(), true)
id, err := t.startNewTimer(t.ctx, info.This(), info.Args(), true)
if err != nil {
return newInt32Value(ctx, 0)
}
Expand Down Expand Up @@ -109,7 +112,7 @@ func (t *timers) clear(id int32, interval bool) {
}
}

func (t *timers) startNewTimer(this v8go.Valuer, args []*v8go.Value, interval bool) (int32, error) {
func (t *timers) startNewTimer(ctx context.Context, this v8go.Valuer, args []*v8go.Value, interval bool) (int32, error) {
if len(args) <= 0 {
return 0, errors.New("1 argument required, but only 0 present")
}
Expand Down Expand Up @@ -152,7 +155,7 @@ func (t *timers) startNewTimer(this v8go.Valuer, args []*v8go.Value, interval bo
t.NextItemID++
t.Items[item.ID] = item

item.Start()
item.Start(ctx)

return item.ID, nil
}
Expand Down