takelatest

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

takelatest

takelatest implements redux-saga effects takeLatest. It is the open-source version of a similar concept named lastflight.

Example:

	// The latest Take() call will be executed after previous calls are canceled due to the timeout.
	done := make(chan struct{})
	r := &Runner[int]{
		Func: func(ctx context.Context, i int) {
			time.Sleep(1 * time.Second)
			if ctx.Err() != nil {
				return
			}
			fmt.Print(i)
			close(done)
		},
	}
	defer r.Close()
	r.Take(1)
	r.Take(2)
	r.Take(3)
	r.Take(4)
	r.Take(5)
	<-done
	// Output:
	// 5

Documentation

Overview

Package takelatest implements redux-saga effects takeLatest.

Example (DebouncedTimeout)
// The latest Take() call will be executed after previous calls are canceled due to the timeout.
done := make(chan struct{})
r := &Runner[int]{
	Func: func(ctx context.Context, i int) {
		time.Sleep(1 * time.Second)
		if ctx.Err() != nil {
			return
		}
		fmt.Print(i)
		close(done)
	},
}
defer r.Close()
r.Take(1)
r.Take(2)
r.Take(3)
r.Take(4)
r.Take(5)
<-done
Output:

5

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Runner

type Runner[T any] struct {
	// Func executes the desired work. Observe context cancellation to know
	// when the next (aka latest) call comes in. Func cannot be replaced
	// when the Runner is active, you must Close first.
	Func func(ctx context.Context, params T)
	// contains filtered or unexported fields
}

Runner implements the takeLatest effect from redux-saga. Its zero value consumes the taken message and does nothing with it.

func (*Runner[T]) Close

func (r *Runner[T]) Close()

Close stoppers the runner.

func (*Runner[T]) Take

func (r *Runner[T]) Take(param T)

Take enqueues the execution of Func with the given parameters.

Jump to

Keyboard shortcuts

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