Browse Source

fix 超时bug

master
3136352472 5 years ago
parent
commit
73bf82e3b5
  1. 12
      jsruntime/runtime.go
  2. 5
      jsruntime/timeout.go
  3. 2
      static/index.html

12
jsruntime/runtime.go

@ -26,6 +26,7 @@ type JsRuntime struct {
mutex sync.Mutex mutex sync.Mutex
RenderCallBackFun func(string) RenderCallBackFun func(string)
IsDebugModel bool IsDebugModel bool
isRenderIng bool
} }
type Rely struct { type Rely struct {
@ -217,6 +218,7 @@ func (jr *JsRuntime) RunCode(code string) error {
} }
func (jr *JsRuntime) Render(filePath, href, tplSrc string, GoExtData interface{}, cb func(data string)) (err error) { func (jr *JsRuntime) Render(filePath, href, tplSrc string, GoExtData interface{}, cb func(data string)) (err error) {
jr.RenderCallBackFun = cb jr.RenderCallBackFun = cb
jr.mutex.Lock() jr.mutex.Lock()
defer jr.mutex.Unlock() defer jr.mutex.Unlock()
@ -224,9 +226,11 @@ func (jr *JsRuntime) Render(filePath, href, tplSrc string, GoExtData interface{}
runtime := jr.runtime runtime := jr.runtime
mainSrc := jr.MainSrc mainSrc := jr.MainSrc
isLock := true isLock := true
jr.isRenderIng = true
//timeoutSec := jr.TimeoutSec //timeoutSec := jr.TimeoutSec
defer func() { defer func() {
jr.isRenderIng = false
isLock = false isLock = false
jr.runtime.ClearInterrupt() jr.runtime.ClearInterrupt()
}() }()
@ -266,9 +270,11 @@ func (jr *JsRuntime) Render(filePath, href, tplSrc string, GoExtData interface{}
jr.RenderCallBackFun(data) jr.RenderCallBackFun(data)
}) })
t := time.AfterFunc(time.Duration(jr.TimeoutSec), func() {
jr.runtime.Interrupt("time out")
jr.runtime.ClearInterrupt()
t := time.AfterFunc(time.Duration(jr.TimeoutSec)*time.Second, func() {
if jr.isRenderIng {
jr.runtime.Interrupt("time out")
jr.runtime.ClearInterrupt()
}
}) })
defer t.Stop() defer t.Stop()

5
jsruntime/timeout.go

@ -3,6 +3,7 @@ package jsruntime
import ( import (
"github.com/dop251/goja" "github.com/dop251/goja"
"log" "log"
"time"
) )
func (jr *JsRuntime) EnableTimeoutFunc() { func (jr *JsRuntime) EnableTimeoutFunc() {
@ -10,13 +11,13 @@ func (jr *JsRuntime) EnableTimeoutFunc() {
jr.runtime.Set("setTimeout", func(call goja.FunctionCall) goja.Value { jr.runtime.Set("setTimeout", func(call goja.FunctionCall) goja.Value {
if fn, ok := goja.AssertFunction(call.Argument(0)); ok { if fn, ok := goja.AssertFunction(call.Argument(0)); ok {
//delay := call.Argument(1).ToInteger()
delay := call.Argument(1).ToInteger()
var args []goja.Value var args []goja.Value
if len(call.Arguments) > 2 { if len(call.Arguments) > 2 {
args = call.Arguments[2:] args = call.Arguments[2:]
} }
// //
//time.Sleep(time.Duration(delay) * time.Millisecond)
time.Sleep(time.Duration(delay) * time.Millisecond)
fn(nil, args...) fn(nil, args...)
} }

2
static/index.html

@ -13,7 +13,7 @@
<my-title></my-title> <my-title></my-title>
<template v-for="desc in descs"> <template v-for="desc in descs">
<li > <li >
<a v-on:click="click" gv-ignore>{{desc}}</a>
<a @click="click" gv-ignore="@click">{{desc}}</a>
</li> </li>
</template> </template>
<br> <br>

Loading…
Cancel
Save