remove runtimeObj from runtimecontext.New

This commit is contained in:
syumai 2024-04-17 00:56:35 +09:00
parent 627ed95530
commit 52a78e1789
3 changed files with 7 additions and 11 deletions

View File

@ -13,8 +13,8 @@ type Task func(ctx context.Context) error
var scheduledTask Task var scheduledTask Task
func runScheduler(eventObj js.Value, runtimeCtxObj js.Value) error { func runScheduler(eventObj js.Value) error {
ctx := runtimecontext.New(context.Background(), eventObj, runtimeCtxObj) ctx := runtimecontext.New(context.Background(), eventObj)
if err := scheduledTask(ctx); err != nil { if err := scheduledTask(ctx); err != nil {
return err return err
} }
@ -27,13 +27,12 @@ func init() {
panic(fmt.Errorf("invalid number of arguments given to runScheduler: %d", len(args))) panic(fmt.Errorf("invalid number of arguments given to runScheduler: %d", len(args)))
} }
eventObj := args[0] eventObj := args[0]
runtimeCtxObj := jsutil.RuntimeContext
var cb js.Func var cb js.Func
cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any { cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any {
defer cb.Release() defer cb.Release()
resolve := pArgs[0] resolve := pArgs[0]
go func() { go func() {
err := runScheduler(eventObj, runtimeCtxObj) err := runScheduler(eventObj)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -24,13 +24,12 @@ func init() {
panic(fmt.Errorf("too many args given to handleRequest: %d", len(args))) panic(fmt.Errorf("too many args given to handleRequest: %d", len(args)))
} }
reqObj := args[0] reqObj := args[0]
runtimeCtxObj := jsutil.RuntimeContext
var cb js.Func var cb js.Func
cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any { cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any {
defer cb.Release() defer cb.Release()
resolve := pArgs[0] resolve := pArgs[0]
go func() { go func() {
res, err := handleRequest(reqObj, runtimeCtxObj) res, err := handleRequest(reqObj)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -53,7 +52,7 @@ func (c *appCloser) Close() error {
} }
// handleRequest accepts a Request object and returns Response object. // handleRequest accepts a Request object and returns Response object.
func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) { func handleRequest(reqObj js.Value) (js.Value, error) {
if httpHandler == nil { if httpHandler == nil {
return js.Value{}, fmt.Errorf("Serve must be called before handleRequest.") return js.Value{}, fmt.Errorf("Serve must be called before handleRequest.")
} }
@ -61,7 +60,7 @@ func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
ctx := runtimecontext.New(context.Background(), reqObj, runtimeCtxObj) ctx := runtimecontext.New(context.Background(), reqObj)
req = req.WithContext(ctx) req = req.WithContext(ctx)
reader, writer := io.Pipe() reader, writer := io.Pipe()
w := &jshttp.ResponseWriter{ w := &jshttp.ResponseWriter{

View File

@ -7,12 +7,10 @@ import (
type ( type (
contextKeyTriggerObj struct{} contextKeyTriggerObj struct{}
contextKeyRuntimeObj struct{}
) )
func New(ctx context.Context, triggerObj, runtimeObj js.Value) context.Context { func New(ctx context.Context, triggerObj js.Value) context.Context {
ctx = context.WithValue(ctx, contextKeyTriggerObj{}, triggerObj) ctx = context.WithValue(ctx, contextKeyTriggerObj{}, triggerObj)
ctx = context.WithValue(ctx, contextKeyRuntimeObj{}, runtimeObj)
return ctx return ctx
} }