Documentation
¶
Overview ¶
Package task is a asynchronous utility inspired by JS Promises & C# Tasks.
Find main reference documentation at https://godoc.org/github.com/brad-jones/goasync
Index ¶
- type ErrStoppingTaskTimeout
- type Internal
- type Task
- func (t *Task) MustResult() interface{}
- func (t *Task) MustResultWithTimeout(runtime, stoptime time.Duration) interface{}
- 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
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{}
// contains filtered or unexported fields
}
Task represents an asynchronous operation, create new instances with New.
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) 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.