Refactor bridge functions in request.go

This commit is contained in:
Max 2023-12-26 03:07:30 +08:00
parent f0b82721c0
commit 7c3fdbe096

View file

@ -376,20 +376,18 @@ func (req *Request) runScript(id string, args []interface{}, cb func(msg *messag
} }
defer v8ctx.Close() defer v8ctx.Close()
// make a new bridge function ssWrite v8ctx.WithFunction("ssWrite", func(info *v8go.FunctionCallbackInfo) *v8go.Value {
ssWriteT := v8go.NewFunctionTemplate(v8ctx.Isolate(), func(info *v8go.FunctionCallbackInfo) *v8go.Value {
args := info.Args() args := info.Args()
if len(args) != 1 { if len(args) != 1 {
return v8go.Null(v8ctx.Isolate()) return v8go.Null(info.Context().Isolate())
} }
text := args[0].String() text := args[0].String()
cb(req.msg().Text(text)) cb(req.msg().Text(text))
return v8go.Null(v8ctx.Isolate()) return v8go.Null(info.Context().Isolate())
}) })
// make a new bridge function done v8ctx.WithFunction("done", func(info *v8go.FunctionCallbackInfo) *v8go.Value {
doneT := v8go.NewFunctionTemplate(v8ctx.Isolate(), func(info *v8go.FunctionCallbackInfo) *v8go.Value {
args := info.Args() args := info.Args()
if len(args) == 1 { if len(args) == 1 {
text := args[0].String() text := args[0].String()
@ -398,12 +396,9 @@ func (req *Request) runScript(id string, args []interface{}, cb func(msg *messag
cb(req.msg().Done()) cb(req.msg().Done())
// req.Done() // req.Done()
return v8go.Null(v8ctx.Isolate()) return v8go.Null(v8ctx.Context.Isolate())
}) })
v8ctx.Global().Set("ssWrite", ssWriteT.GetFunction(v8ctx.Context))
v8ctx.Global().Set("done", doneT.GetFunction(v8ctx.Context))
res, err := v8ctx.CallWith(req.ctx, method, args...) res, err := v8ctx.CallWith(req.ctx, method, args...)
if err != nil { if err != nil {
return nil, err return nil, err