remove context.Context from existing RuntimeContext related func params

This commit is contained in:
syumai 2024-04-17 00:41:39 +09:00
parent 9710147e94
commit 33e4ad8f50
2 changed files with 9 additions and 20 deletions

View File

@ -1,11 +1,10 @@
package cfruntimecontext package cfruntimecontext
import ( import (
"context"
"errors" "errors"
"syscall/js" "syscall/js"
"github.com/syumai/workers/internal/runtimecontext" "github.com/syumai/workers/internal/jsutil"
) )
/** /**
@ -23,21 +22,21 @@ import (
// MustGetRuntimeContextEnv gets object which holds environment variables bound to Cloudflare worker. // MustGetRuntimeContextEnv gets object which holds environment variables bound to Cloudflare worker.
// - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L566 // - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L566
func MustGetRuntimeContextEnv(ctx context.Context) js.Value { func MustGetRuntimeContextEnv() js.Value {
return MustGetRuntimeContextValue(ctx, "env") return MustGetRuntimeContextValue("env")
} }
// MustGetExecutionContext gets ExecutionContext object from context. // MustGetExecutionContext gets ExecutionContext object from context.
// - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L567 // - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L567
// - see also: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L554 // - see also: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L554
func MustGetExecutionContext(ctx context.Context) js.Value { func MustGetExecutionContext() js.Value {
return MustGetRuntimeContextValue(ctx, "ctx") return MustGetRuntimeContextValue("ctx")
} }
// MustGetRuntimeContextValue gets value for specified key from RuntimeContext. // MustGetRuntimeContextValue gets value for specified key from RuntimeContext.
// - if the value is undefined, this function panics. // - if the value is undefined, this function panics.
func MustGetRuntimeContextValue(ctx context.Context, key string) js.Value { func MustGetRuntimeContextValue(key string) js.Value {
val, err := GetRuntimeContextValue(ctx, key) val, err := GetRuntimeContextValue(key)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -48,8 +47,8 @@ var ErrValueNotFound = errors.New("execution context value for specified key not
// GetRuntimeContextValue gets value for specified key from RuntimeContext. // GetRuntimeContextValue gets value for specified key from RuntimeContext.
// - if the value is undefined, return error. // - if the value is undefined, return error.
func GetRuntimeContextValue(ctx context.Context, key string) (js.Value, error) { func GetRuntimeContextValue(key string) (js.Value, error) {
runtimeObj := runtimecontext.MustExtractRuntimeObj(ctx) runtimeObj := jsutil.RuntimeContext
v := runtimeObj.Get(key) v := runtimeObj.Get(key)
if v.IsUndefined() { if v.IsUndefined() {
return js.Value{}, ErrValueNotFound return js.Value{}, ErrValueNotFound

View File

@ -25,13 +25,3 @@ func MustExtractTriggerObj(ctx context.Context) js.Value {
} }
return v return v
} }
// MustExtractRuntimeObj extracts runtime object from context.
// This function panics when runtime object was not found.
func MustExtractRuntimeObj(ctx context.Context) js.Value {
v, ok := ctx.Value(contextKeyRuntimeObj{}).(js.Value)
if !ok {
panic("runtime object was not found")
}
return v
}