2021-04-16 11:54:28 +03:00
|
|
|
// This module defines the Context type, which carries deadlines, cancellation signals,
|
|
|
|
// and other request-scoped values across API boundaries and between processes.
|
2021-09-27 17:52:20 +03:00
|
|
|
// Based on: https://github.com/golang/go/tree/master/src/context
|
2021-04-16 11:54:28 +03:00
|
|
|
// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
|
2021-04-12 19:32:51 +03:00
|
|
|
module context
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2023-06-04 19:12:52 +03:00
|
|
|
// An EmptyContext is never canceled, has no values.
|
|
|
|
pub struct EmptyContext {}
|
2021-04-12 19:32:51 +03:00
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &EmptyContext) deadline() ?time.Time {
|
2021-04-12 19:32:51 +03:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &EmptyContext) done() chan int {
|
2021-04-12 19:32:51 +03:00
|
|
|
ch := chan int{}
|
2023-06-04 19:12:52 +03:00
|
|
|
ch.close()
|
2021-04-12 19:32:51 +03:00
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &EmptyContext) err() IError {
|
|
|
|
return none
|
2021-04-12 19:32:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &EmptyContext) value(key Key) ?Any {
|
2021-04-12 19:32:51 +03:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &EmptyContext) str() string {
|
2021-04-12 19:32:51 +03:00
|
|
|
return 'unknown empty Context'
|
|
|
|
}
|
2023-06-04 19:12:52 +03:00
|
|
|
|
|
|
|
// A BackgroundContext is never canceled, has no values.
|
|
|
|
struct BackgroundContext {
|
|
|
|
EmptyContext
|
|
|
|
}
|
|
|
|
|
|
|
|
// str returns a string describing the Context.
|
|
|
|
pub fn (ctx &BackgroundContext) str() string {
|
|
|
|
return 'context.Background'
|
|
|
|
}
|
|
|
|
|
|
|
|
// background returns an empty Context. It is never canceled, has no
|
|
|
|
// values, and has no deadline. It is typically used by the main function,
|
|
|
|
// initialization, and tests, and as the top-level Context for incoming
|
|
|
|
// requests.
|
|
|
|
pub fn background() Context {
|
|
|
|
return &BackgroundContext{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A TodoContext is never canceled, has no values, and has no deadline. It is
|
|
|
|
// never used for real work.
|
|
|
|
struct TodoContext {
|
|
|
|
EmptyContext
|
|
|
|
}
|
|
|
|
|
|
|
|
// str returns a string describing the Context.
|
|
|
|
pub fn (ctx &TodoContext) str() string {
|
|
|
|
return 'context.TODO'
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo returns an empty Context. Code should use todo when
|
|
|
|
// it's unclear which Context to use or it is not yet available (because the
|
|
|
|
// surrounding function has not yet been extended to accept a Context
|
|
|
|
// parameter).
|
|
|
|
pub fn todo() Context {
|
|
|
|
return &TodoContext{}
|
|
|
|
}
|