Skip to content

Commit

Permalink
add bind option to ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
RyougiNevermore committed Nov 19, 2024
1 parent dd03fa3 commit cd60bbf
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions async/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ func WithWait() Option {
}
}

// Make
// 构建一个许诺
func Make[R any](ctx context.Context, options ...Option) (p Promise[R], err error) {
type optionsCtxKey struct{}

// WithOptions
// 把 Make 的 Options 绑定到 context.Context。常用于设置上下文中的默认选项。
func WithOptions(ctx context.Context, options ...Option) context.Context {
opt := Options{
Stream: false,
Mode: Normal,
Expand All @@ -77,6 +79,29 @@ func Make[R any](ctx context.Context, options ...Option) (p Promise[R], err erro
for _, o := range options {
o(&opt)
}
return context.WithValue(ctx, optionsCtxKey{}, opt)
}

func getOptions(ctx context.Context) Options {
value := ctx.Value(optionsCtxKey{})
if value == nil {
return Options{
Stream: false,
Mode: Normal,
WaitTimeout: -1,
}
}
opt := value.(Options)
return opt
}

// Make
// 构建一个许诺
func Make[R any](ctx context.Context, options ...Option) (p Promise[R], err error) {
opt := getOptions(ctx)
for _, o := range options {
o(&opt)
}
var submitter rxp.TaskSubmitter
switch opt.Mode {
case Unlimited:
Expand Down

0 comments on commit cd60bbf

Please sign in to comment.