github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/mobile/context.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 // Contains all the wrappers from the golang.org/x/net/context package to support 13 // client side context management on mobile platforms. 14 15 package geth 16 17 import ( 18 "context" 19 "time" 20 ) 21 22 // Context carries a deadline, a cancelation signal, and other values across API 23 // boundaries. 24 type Context struct { 25 context context.Context 26 cancel context.CancelFunc 27 } 28 29 // NewContext returns a non-nil, empty Context. It is never canceled, has no 30 // values, and has no deadline. It is typically used by the main function, 31 // initialization, and tests, and as the top-level Context for incoming requests. 32 func NewContext() *Context { 33 return &Context{ 34 context: context.Background(), 35 } 36 } 37 38 // WithCancel returns a copy of the original context with cancellation mechanism 39 // included. 40 // 41 // Canceling this context releases resources associated with it, so code should 42 // call cancel as soon as the operations running in this Context complete. 43 func (c *Context) WithCancel() *Context { 44 child, cancel := context.WithCancel(c.context) 45 return &Context{ 46 context: child, 47 cancel: cancel, 48 } 49 } 50 51 // WithDeadline returns a copy of the original context with the deadline adjusted 52 // to be no later than the specified time. 53 // 54 // Canceling this context releases resources associated with it, so code should 55 // call cancel as soon as the operations running in this Context complete. 56 func (c *Context) WithDeadline(sec int64, nsec int64) *Context { 57 child, cancel := context.WithDeadline(c.context, time.Unix(sec, nsec)) 58 return &Context{ 59 context: child, 60 cancel: cancel, 61 } 62 } 63 64 // WithTimeout returns a copy of the original context with the deadline adjusted 65 // to be no later than now + the duration specified. 66 // 67 // Canceling this context releases resources associated with it, so code should 68 // call cancel as soon as the operations running in this Context complete. 69 func (c *Context) WithTimeout(nsec int64) *Context { 70 child, cancel := context.WithTimeout(c.context, time.Duration(nsec)) 71 return &Context{ 72 context: child, 73 cancel: cancel, 74 } 75 }