github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/context/wrap.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package context
    19  
    20  import (
    21  	"context"
    22  	"time"
    23  )
    24  
    25  var (
    26  	Canceled         = context.Canceled
    27  	DeadlineExceeded = context.DeadlineExceeded
    28  )
    29  
    30  func Wrap(ctx context.Context) Context {
    31  	v, ok := ctx.(Context)
    32  	if ok {
    33  		return v
    34  	}
    35  	return &context_{
    36  		Context: ctx,
    37  		users:   make(Entries, 0, 1),
    38  		locals:  make(Entries, 0, 1),
    39  	}
    40  }
    41  
    42  func Fork(ctx context.Context) Context {
    43  	return &context_{
    44  		Context: ctx,
    45  		users:   make(Entries, 0, 1),
    46  		locals:  make(Entries, 0, 1),
    47  	}
    48  }
    49  
    50  func TODO() Context {
    51  	return Wrap(context.TODO())
    52  }
    53  
    54  func WithValue(parent context.Context, key any, val any) Context {
    55  	return &valueContext{
    56  		Context: Wrap(parent),
    57  		key:     key,
    58  		val:     val,
    59  	}
    60  }
    61  
    62  type CancelFunc context.CancelFunc
    63  
    64  func WithCancel(parent Context) (Context, CancelFunc) {
    65  	ctx, cancel := context.WithCancel(parent)
    66  	return Wrap(ctx), CancelFunc(cancel)
    67  }
    68  
    69  func WithTimeout(parent Context, ttl time.Duration) (Context, CancelFunc) {
    70  	ctx, cancel := context.WithTimeout(parent, ttl)
    71  	return Wrap(ctx), CancelFunc(cancel)
    72  }
    73  
    74  func WithTimeoutCause(parent Context, ttl time.Duration, cause error) (Context, CancelFunc) {
    75  	ctx, cancel := context.WithTimeoutCause(parent, ttl, cause)
    76  	return Wrap(ctx), CancelFunc(cancel)
    77  }
    78  
    79  func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
    80  	ctx, cancel := context.WithDeadline(parent, deadline)
    81  	return Wrap(ctx), CancelFunc(cancel)
    82  }
    83  
    84  func WithDeadlineCause(parent Context, deadline time.Time, cause error) (Context, CancelFunc) {
    85  	ctx, cancel := context.WithDeadlineCause(parent, deadline, cause)
    86  	return Wrap(ctx), CancelFunc(cancel)
    87  }
    88  
    89  func WithoutCancel(parent Context) Context {
    90  	return Wrap(context.WithoutCancel(parent))
    91  }
    92  
    93  func AfterFunc(ctx Context, f func()) (stop func() bool) {
    94  	stop = context.AfterFunc(ctx, f)
    95  	return
    96  }