mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 09:27:08 +00:00
remove context.Context from existing RuntimeContext related func params
This commit is contained in:
parent
9710147e94
commit
33e4ad8f50
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user