Documentation
¶
Overview ¶
Package task is a asynchronous utility inspired by JS Promises & C# Tasks.
Index ¶
- type ErrStoppingTaskTimeout
- type Internal
- type Task
- func (t *Task) IsCompleted() bool
- func (t *Task) MustResult() interface{}
- func (t *Task) MustResultWithTimeout(runtime, stoptime time.Duration) interface{}
- func (t *Task) MustWait()
- func (t *Task) Result() (interface{}, error)
- func (t *Task) ResultWithTimeout(runtime, stoptime time.Duration) (interface{}, error)
- func (t *Task) Stop()
- func (t *Task) StopWithTimeout(timeout time.Duration) error
- func (t *Task) Then(fn interface{}) *Task
- func (t *Task) Wait() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrStoppingTaskTimeout ¶
type ErrStoppingTaskTimeout struct { }
ErrStoppingTaskTimeout is returned by StopWithTimeout when the given duration has passed and the task has still not stopped.
func (*ErrStoppingTaskTimeout) Error ¶
func (e *ErrStoppingTaskTimeout) Error() string
type Internal ¶
type Internal struct { // Every task has a resolver channel that ultimately represents a // value to be returned sometime in the future. As a task implementor // you should only ever send a single value to this channel. Resolver chan<- interface{} // Every task has a rejector channel that represents a possible error // to be returned sometime in the future. As a task implementor // you should only ever send a single value to this channel. Rejector chan<- error // Every task has a stopper channel that allows the task to be // stopped cooperatively from the outside. If this channel is closed // you should stop your task. Stopper *chan struct{} // contains filtered or unexported fields }
Internal is used by the task implementor.
func (*Internal) CancelableCtx ¶
CancelableCtx returns a context object that will be canceled if this task is told to stop, this is useful for integrating with more traditional go code.
func (*Internal) Reject ¶
Reject is a simple function that sends the provided error to the rejector channel.
func (*Internal) Resolve ¶
func (i *Internal) Resolve(v interface{})
Resolve is a simple function that sends the provided value to the resolver channel.
func (*Internal) ShouldStop ¶
ShouldStop is a non blocking method that informs your task if it should stop.
type Task ¶
type Task struct { // Every task has a resolver channel that ultimately represents a // value to be returned sometime in the future. Keep in mind that // once a value is read from this channel it can not be read from again. Resolver <-chan interface{} // Every task has a rejector channel that represents a possible error // to be returned sometime in the future. Keep in mind that once a value // is read from this channel it can not be read from again. Rejector <-chan error // Every task has a stopper channel that allows the task to be // stopped cooperatively from the outside. Simply close this // channel to stop the task. Stopper *chan struct{} // Used to track when the task has actually finished regardless of what // has or hasn't been resolved/rejected. Tasks can just perform some action // they don't necessarily have to resolve something. Done *chan struct{} // contains filtered or unexported fields }
Task represents an asynchronous operation, create new instances with New.
func New ¶
func New(fn interface{}) *Task
New creates new instances of Task. Accepts `func()` or `func(t *Internal)`
func (*Task) IsCompleted ¶ added in v2.1.0
IsCompleted indicates if the task has finished or not in a non-blocking manner
func (*Task) MustResult ¶
func (t *Task) MustResult() interface{}
MustResult does the same as Result() but panics if an error was rejected.
func (*Task) MustResultWithTimeout ¶
MustResultWithTimeout does the same as ResultWithTimeout() but panics if an error was encountered.
func (*Task) MustWait ¶ added in v2.1.0
func (t *Task) MustWait()
MustWait does the same as Wait but panics if the task rejected an error
func (*Task) Result ¶
Result waits for the task to complete and then returns any resolved (or rejected) values. This can be called many times over and the same values will be returned.
func (*Task) ResultWithTimeout ¶
ResultWithTimeout takes 2 duration values, the first is the amount of time we will wait for the task to complete, if that time passes we will then call `StopWithTimeout` which will wait for the second duration for the given task to cooperatively stop.
func (*Task) Stop ¶
func (t *Task) Stop()
Stop the task cooperatively, this will block until the task has returned.
func (*Task) StopWithTimeout ¶
StopWithTimeout will stop the task cooperatively but return an error if a timeout is reached. Use this to ensure your application does not hang indefinitely.