From 33e4ad8f50d5e2f7c7b5bbb4dce55cc2ac15b16d Mon Sep 17 00:00:00 2001 From: syumai Date: Wed, 17 Apr 2024 00:41:39 +0900 Subject: [PATCH] remove context.Context from existing RuntimeContext related func params --- .../cfruntimecontext/cfruntimecontext.go | 19 +++++++++---------- internal/runtimecontext/context.go | 10 ---------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go index b36d9cc..345dc4b 100644 --- a/cloudflare/internal/cfruntimecontext/cfruntimecontext.go +++ b/cloudflare/internal/cfruntimecontext/cfruntimecontext.go @@ -1,11 +1,10 @@ package cfruntimecontext import ( - "context" "errors" "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. // - see: https://github.com/cloudflare/workers-types/blob/c8d9533caa4415c2156d2cf1daca75289d01ae70/index.d.ts#L566 -func MustGetRuntimeContextEnv(ctx context.Context) js.Value { - return MustGetRuntimeContextValue(ctx, "env") +func MustGetRuntimeContextEnv() js.Value { + return MustGetRuntimeContextValue("env") } // MustGetExecutionContext gets ExecutionContext object from context. // - 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 -func MustGetExecutionContext(ctx context.Context) js.Value { - return MustGetRuntimeContextValue(ctx, "ctx") +func MustGetExecutionContext() js.Value { + return MustGetRuntimeContextValue("ctx") } // MustGetRuntimeContextValue gets value for specified key from RuntimeContext. // - if the value is undefined, this function panics. -func MustGetRuntimeContextValue(ctx context.Context, key string) js.Value { - val, err := GetRuntimeContextValue(ctx, key) +func MustGetRuntimeContextValue(key string) js.Value { + val, err := GetRuntimeContextValue(key) if err != nil { 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. // - if the value is undefined, return error. -func GetRuntimeContextValue(ctx context.Context, key string) (js.Value, error) { - runtimeObj := runtimecontext.MustExtractRuntimeObj(ctx) +func GetRuntimeContextValue(key string) (js.Value, error) { + runtimeObj := jsutil.RuntimeContext v := runtimeObj.Get(key) if v.IsUndefined() { return js.Value{}, ErrValueNotFound diff --git a/internal/runtimecontext/context.go b/internal/runtimecontext/context.go index 288e0cd..4f5f668 100644 --- a/internal/runtimecontext/context.go +++ b/internal/runtimecontext/context.go @@ -25,13 +25,3 @@ func MustExtractTriggerObj(ctx context.Context) js.Value { } 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 -}