task

package
v2.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2021 License: MIT Imports: 3 Imported by: 18

Documentation

Overview

Package task is a asynchronous utility inspired by JS Promises & C# Tasks.

Index

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

func (i *Internal) CancelableCtx() context.Context

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

func (i *Internal) Reject(err interface{}, messages ...string)

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

func (i *Internal) ShouldStop() bool

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 Rejected

func Rejected(e error) *Task

Rejected returns a pre-rejected task

func Resolved

func Resolved(v interface{}) *Task

Resolved returns a pre-resolved task

func (*Task) IsCompleted added in v2.1.0

func (t *Task) IsCompleted() bool

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

func (t *Task) MustResultWithTimeout(runtime, stoptime time.Duration) interface{}

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

func (t *Task) Result() (interface{}, error)

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

func (t *Task) ResultWithTimeout(runtime, stoptime time.Duration) (interface{}, error)

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

func (t *Task) StopWithTimeout(timeout time.Duration) error

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.

func (*Task) Then added in v2.1.0

func (t *Task) Then(fn interface{}) *Task

Then registers a callback to be called when this Task completes. Accepts `func()`, `func(t *Internal)` or `func(result interface{}, t *Internal)`

func (*Task) Wait added in v2.1.0

func (t *Task) Wait() error

Wait will block until the task is complete, if the task rejected an error it will be returned

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL