chanx

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

chanx (ARCHIVED)

This package is archived. Use github.com/xoctopus/concx/pkg/chanx instead.

x/chanx is frozen for compatibility. New features land only in schex.

Documentation

Overview

Package chanx is ARCHIVED.

Canonical location: github.com/xoctopus/concx/pkg/chanx

This package remains for compatibility. Do not add features here. New code should import schex/pkg/chanx. Existing callers should migrate when convenient; APIs here are frozen and may lag behind schex.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// Deprecated: use github.com/xoctopus/concx/pkg/chanx.ErrCompleted.
	ErrCompleted = errors.New("completed")
)

Functions

This section is empty.

Types

type Cancelable deprecated

type Cancelable interface {
	CancelCause(err error)
}

Deprecated: use github.com/xoctopus/concx/pkg/chanx. Cancelable is archived here.

type NotifiableObserver deprecated

type NotifiableObserver[T any] interface {
	Observer[T]
	ValueNotifier[T]
}

Deprecated: use github.com/xoctopus/concx/pkg/chanx.

Example
package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/xoctopus/x/chanx"
)

func main() {
	// Create a NotifiableObserver
	obs := chanx.NewNotifiableObserver[int]()

	var wg sync.WaitGroup

	// Start a producer goroutine
	wg.Go(func() {
		obs.Send(1)
		obs.Send(2)
		obs.Send(3)
		time.Sleep(10 * time.Millisecond)
		// Cancel after sending all data. A nil error will be converted to ErrCompleted.
		obs.CancelCause(nil)
	})

	// Consume data in the main goroutine until the channel is closed
	for v := range obs.Value() {
		fmt.Println("Received:", v)
	}

	wg.Wait()

	// Check the final error state
	fmt.Println("Error:", obs.Err())

}
Output:
Received: 1
Received: 2
Received: 3
Error: completed

func NewNotifiableObserver deprecated

func NewNotifiableObserver[T any]() NotifiableObserver[T]

Deprecated: use github.com/xoctopus/concx/pkg/chanx.NewNotifiableObserver.

type Observable deprecated

type Observable[T any] interface {
	Observe() Observer[T]
}

Deprecated: use github.com/xoctopus/concx/pkg/chanx.

type Observer deprecated

type Observer[T any] interface {
	ValueObserver[T]
	Cancelable

	Done() <-chan struct{}
	Err() error
}

Deprecated: use github.com/xoctopus/concx/pkg/chanx.

type ObserverFunc deprecated

type ObserverFunc[T any] func() Observer[T]

Deprecated: use github.com/xoctopus/concx/pkg/chanx.

func (ObserverFunc[T]) Observe

func (f ObserverFunc[T]) Observe() Observer[T]

type Subject deprecated

type Subject[T any] struct {
	// contains filtered or unexported fields
}

Deprecated: use github.com/xoctopus/concx/pkg/chanx.Subject.

Example
package main

import (
	"fmt"
	"slices"
	"sync"
	"time"

	"github.com/xoctopus/x/chanx"
	"github.com/xoctopus/x/iterx"
)

func main() {
	// Create a Subject
	subject := &chanx.Subject[int]{}

	// Create two observers
	obs1 := subject.Observe()
	obs2 := subject.Observe()

	var (
		wg      sync.WaitGroup
		results = make(chan string, 4)
	)

	consuming := func(name string, observer chanx.Observer[int]) func() {
		return func() {
			for v := range observer.Value() {
				results <- fmt.Sprintf("%s received: %d", name, v)
			}
		}
	}

	// Consumer 1
	wg.Go(consuming("obs1", obs1))
	// Consumer 2
	wg.Go(consuming("obs2", obs2))

	// Producer broadcasts data via the Subject
	subject.Send(1)
	time.Sleep(10 * time.Millisecond)
	subject.Send(2)
	time.Sleep(10 * time.Millisecond)

	// Close the Subject, which cascades the cancellation to all subscribers
	subject.CancelCause(nil)
	// Wait for all consumers to finish
	wg.Wait()
	close(results)

	for _, s := range slices.Sorted(iterx.Recv(results)) {
		fmt.Println(s)
	}

	// Verify the error state of the Subject and its Observers
	fmt.Println("Subject Error:", subject.Err())
	fmt.Println("Obs1 Error:", obs1.Err())
	fmt.Println("Obs2 Error:", obs2.Err())

}
Output:
obs1 received: 1
obs1 received: 2
obs2 received: 1
obs2 received: 2
Subject Error: completed
Obs1 Error: completed
Obs2 Error: completed

func (*Subject[T]) CancelCause

func (s *Subject[T]) CancelCause(err error)

func (*Subject[T]) Done

func (s *Subject[T]) Done() <-chan struct{}

func (*Subject[T]) Err

func (s *Subject[T]) Err() error

func (*Subject[T]) Observe

func (s *Subject[T]) Observe() Observer[T]

func (*Subject[T]) Send

func (s *Subject[T]) Send(x T)

func (*Subject[T]) Subscribe

func (s *Subject[T]) Subscribe(o Subscriber[T])

type Subscriber deprecated

type Subscriber[T any] interface {
	ValueNotifier[T]
	Cancelable

	Done() <-chan struct{}
	Err() error
}

Deprecated: use github.com/xoctopus/concx/pkg/chanx.

type ValueNotifier deprecated

type ValueNotifier[T any] interface {
	Send(x T)
}

Deprecated: use github.com/xoctopus/concx/pkg/chanx.

type ValueObserver deprecated

type ValueObserver[T any] interface {
	Value() <-chan T
}

Deprecated: use github.com/xoctopus/concx/pkg/chanx.

Jump to

Keyboard shortcuts

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