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
RenderCallBackFun func(string)
IsDebugModel bool
isRenderIng bool
}
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) {
jr.RenderCallBackFun = cb
jr.mutex.Lock()
defer jr.mutex.Unlock()
@ -224,9 +226,11 @@ func (jr *JsRuntime) Render(filePath, href, tplSrc string, GoExtData interface{}
runtime := jr.runtime
mainSrc := jr.MainSrc
isLock := true
jr.isRenderIng = true
//timeoutSec := jr.TimeoutSec
defer func() {
jr.isRenderIng = false
isLock = false
jr.runtime.ClearInterrupt()
}()
@ -266,9 +270,11 @@ func (jr *JsRuntime) Render(filePath, href, tplSrc string, GoExtData interface{}
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()

5
jsruntime/timeout.go

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

2
static/index.html

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

Loading…
Cancel
Save