state/signal.go

Functions

Functions

func NewSignal

NewSignal creates a new writable signal with the given initial value.

 

The signal uses default behavior: no equality checks and default panic

handling (log and continue).

 

Example:

 

count := state.NewSignal(0)

count.Set(5)

fmt.Println(count.Get()) // 5

func NewSignal[T any](initial T) Signal[T] {
	return signals.New(initial)
}

func NewSignalWithOptions

NewSignalWithOptions creates a new writable signal with custom options.

 

Use this when you need custom equality checks or custom panic handling.

 

Example:

 

count := state.NewSignalWithOptions(0, state.Options[int]{

Equal: func(a, b int) bool { return a == b },

})

func NewSignalWithOptions[T any](initial T, opts Options[T]) Signal[T] {
	return signals.NewWithOptions(initial, opts)
}

func Subscribe

Subscribe registers a callback on a readable signal that is automatically

canceled when the context is done. Returns an Unsubscribe function for

manual cleanup.

 

This is a convenience wrapper so callers do not need to import coregx/signals.

func Subscribe[T any](sig ReadonlySignal[T], ctx context.Context, fn func(T)) Unsubscribe {
	return sig.Subscribe(ctx, fn)
}

func SubscribeForever

SubscribeForever registers a callback on a readable signal that is never

automatically canceled. The caller must call the returned Unsubscribe to

prevent memory leaks.

func SubscribeForever[T any](sig ReadonlySignal[T], fn func(T)) Unsubscribe {
	return sig.SubscribeForever(fn)
}