future

package module
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 License: MIT Imports: 3 Imported by: 0

README

Future

Port of asynchron to go.

Installation

go get -u github.com/Ar37-rs/future

Future Task Example:

package main

import (
	"fmt"
	"time"
	task "github.com/Ar37-rs/future"
)

func main() {
	// Create new future task.
	task1 := task.Future(1, func(it *task.ITask) task.Progress {
		count := 0
		for {
			// // return task.Cancel(..) if canceled.
			// if it.ShouldCancel() {
			// 	cancel := fmt.Sprintf(`the task with id: %d canceled`, it.Id())
			// 	return task.Cancel(cancel)
			// }
			count += 1
			time.Sleep(time.Millisecond * 10)
			// Send some messages from the ITask (Inner Task)
			it.Send(fmt.Sprintf(`hello: %d`, count))
			if count > 5 {
				break
			}
		}
		complete := fmt.Sprintf(`the task with id: %d completed`, it.Id())
		return task.Complete(complete)
		// // return error if need to.
		// _error := fmt.Errorf(`the task with id: %d error`, it.Id())
		// return task.Error(_error)
	})
	println(fmt.Sprintf(`task id: %d`, task1.Id()))
	// Try do the task now.
	task1.TryDo()
	// // Cancel if need to.
	// task1.Cancel()
	quit := false
	for {
		println("this event loop won't be blocked.")
		time.Sleep(time.Millisecond * 100)
		task1.TryResolve(func(p *task.Progress, done bool) {
			p.Current(func(v task.Value) {
				println(v.(string))
			})

			p.OnCancel(func(v task.Value) {
				println(v.(string))
			})

			p.OnComplete(func(v task.Value) {
				println(v.(string))
			})

			p.OnError(func(e error) {
				println(e.Error())
			})

			if done {
				quit = true
			}
		})

		if quit {
			break
		}
	}
}

Runnable Task Example:

package main

import (
	"fmt"
	"time"
	task "github.com/Ar37-rs/future"
)

func main() {
	counter := 0
	// Create new runnable task.
	runnable_task := task.Runnable(8, func(it *task.IRTask) {
		for {
			// Make sure the loop not spin too fast, sleep a few millis if need to be canceled.
			time.Sleep(time.Millisecond * 10)
			if it.ShouldCancel() || counter > 5 {
				break
			}
			counter += 1
		}
	})
	// Try do the task now.
	runnable_task.TryDo()
	println(fmt.Sprintf(`task id: %d`, runnable_task.Id()))
	// // Cancel if need to.
	// runnable_task.Cancel()
	for {
		if runnable_task.IsNotRunning() {
			break
		}
	}
	if runnable_task.IsCanceled() {
		println("runnable task canceled.")
	}
	println(fmt.Sprintf(`counter value: %d`, counter))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IRTask added in v1.1.5

type IRTask struct {
	// contains filtered or unexported fields
}

Inner Runnable Task

func (*IRTask) Id added in v1.1.5

func (it *IRTask) Id() uint

Get id of the task

func (*IRTask) ShouldCancel added in v1.1.5

func (it *IRTask) ShouldCancel() bool

Check if the runnable task should be canceled.

func (*IRTask) ShouldSuspend added in v1.1.5

func (it *IRTask) ShouldSuspend() bool

Check if the runnable task should be suspended,

usually applied for a specific task with event loop in it.

do other things (switch) while the task is suspended or Wait() the task immediately.

func (*IRTask) Wait added in v1.5.0

func (it *IRTask) Wait()

Wait immediately if the runnable task should be suspended and will do nothing if it should not.

type ITask

type ITask struct {
	// contains filtered or unexported fields
}

Inner Task

func (*ITask) Id added in v0.1.1

func (it *ITask) Id() uint

Get id of the task

func (*ITask) Recv added in v1.1.0

func (it *ITask) Recv() Value

Receive value from the outer task (if any, and will return nil if nothing).

func (*ITask) Send

func (it *ITask) Send(value interface{})

Send current inner task progress

func (*ITask) ShouldCancel added in v0.5.0

func (it *ITask) ShouldCancel() bool

Check if progress of the task should be canceled.

func (*ITask) ShouldSuspend added in v0.5.0

func (it *ITask) ShouldSuspend() bool

Check if the task should be suspended,

usually applied for a specific task with event loop in it.

do other things (switch) while the task is suspended or Wait() the task immediately.

func (*ITask) Wait added in v1.5.0

func (it *ITask) Wait()

Wait immediately if the task should be suspended and will do nothing if it should not.

type Progress

type Progress struct {
	// contains filtered or unexported fields
}

Progress of the task

func Cancel added in v0.5.0

func Cancel(val Value) Progress

func Complete added in v0.5.0

func Complete(val Value) Progress

func Error

func Error(e error) Progress

func (*Progress) Current

func (p *Progress) Current(fn func(Value))

Current progress of the task with sender Value (if any)

func (*Progress) OnCancel added in v0.5.0

func (p *Progress) OnCancel(fn func(Value))

Indicates the task is canceled

func (*Progress) OnComplete added in v0.5.0

func (p *Progress) OnComplete(fn func(Value))

Indicates the task is completed

func (*Progress) OnError

func (p *Progress) OnError(fn func(error))

Indicates the task is error

type RTask added in v1.1.5

type RTask struct {
	// contains filtered or unexported fields
}

Runnable task

func Runnable added in v1.1.5

func Runnable(id uint, fn func(f *IRTask)) *RTask

Create new runnable task

func (*RTask) Cancel added in v1.1.5

func (f *RTask) Cancel()

Send signal to the inner runnable handle that the task should be canceled.

this won't do anything if not explicitly configured inside the task.

func (*RTask) ChangeTask added in v1.1.5

func (f *RTask) ChangeTask(fn func(f *IRTask))

Change the runnable task, make sure the task isn't running.

func (*RTask) Id added in v1.1.5

func (f *RTask) Id() uint

Get id of the runnable task

func (*RTask) IsCanceled added in v1.1.5

func (f *RTask) IsCanceled() bool

Check if the runnable task is canceled

func (*RTask) IsChanged added in v1.1.5

func (f *RTask) IsChanged() bool

Check if the task is changed

func (*RTask) IsNotRunning added in v1.1.5

func (f *RTask) IsNotRunning() bool

Check if the runnable task is not running anymore (or done)

func (*RTask) IsRunning added in v1.1.5

func (f *RTask) IsRunning() bool

Check if the task is running

func (*RTask) IsSuspended added in v1.1.5

func (f *RTask) IsSuspended() bool

Check if the task is suspended

func (*RTask) Resume added in v1.1.5

func (f *RTask) Resume()

Resume the suspended runnable task.

this won't do anything if not explicitly configured inside the task.

func (*RTask) Suspend added in v1.1.5

func (f *RTask) Suspend()

Send signal to the inner runnable task handle that the task should be suspended.

this won't do anything if not explicitly configured inside the task.

func (*RTask) TryDo added in v1.1.5

func (f *RTask) TryDo()

Try do the runnable task now (it won't block the current thread)

type Task

type Task struct {
	// contains filtered or unexported fields
}

Future task

func Future

func Future(id uint, fn func(f *ITask) Progress) *Task

Create new future task

func (*Task) Cancel

func (f *Task) Cancel()

Send signal to the inner task that the task should be canceled.

this won't do anything if not explicitly configured inside the task.

func (*Task) ChangeTask added in v0.5.0

func (f *Task) ChangeTask(fn func(f *ITask) Progress)

Change task, make sure the task isn't in progress.

func (*Task) Id

func (f *Task) Id() uint

Get id of the task

func (*Task) IsCanceled

func (f *Task) IsCanceled() bool

Check if the task is canceled

func (*Task) IsChanged added in v0.5.0

func (f *Task) IsChanged() bool

Check if the task is changed

func (*Task) IsDone

func (f *Task) IsDone() bool

Check if the task is done (not in progress anymore)

func (*Task) IsInProgress

func (f *Task) IsInProgress() bool

Check if the task is in progress

func (*Task) IsSuspended added in v0.5.0

func (f *Task) IsSuspended() bool

Check if the task is suspended

func (*Task) Resume added in v0.5.0

func (f *Task) Resume()

Resume the suspended task.

this won't do anything if not explicitly configured inside the task.

func (*Task) Send added in v1.1.0

func (it *Task) Send(value interface{})

Send value to the inner task

func (*Task) Suspend added in v0.5.0

func (f *Task) Suspend()

Send signal to the inner task that the task should be suspended.

this won't do anything if not explicitly configured inside the task.

func (*Task) TryDo

func (f *Task) TryDo()

Try do the task now (it won't block the current thread)

and then TryResolve later.

func (*Task) TryResolve

func (f *Task) TryResolve(fn func(*Progress, bool))

Try resolve progress of the future task (non-blocking)

func (*Task) WaitResolve added in v1.0.0

func (f *Task) WaitResolve(spin_delay uint, fn func(*Progress, bool))

Careful! this is blocking operation, resolve progress of the future task (will "block" until the task is completed).

if spin_delay < 3 milliseconds, spin_delay value will be 3 milliseconds, set it large than that or accordingly.

due to blocking operation nature this fn only return whether the task is canceled, completed or error. current progress (sender) will be ignored.

type Value

type Value = interface{}

Future value

Jump to

Keyboard shortcuts

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