49 lines
781 B
Go
49 lines
781 B
Go
package reactive
|
|
|
|
type NotificationKind int
|
|
|
|
const (
|
|
NextNotification NotificationKind = iota
|
|
ErrorNotification
|
|
)
|
|
|
|
type Notification[T any] interface {
|
|
Value() T
|
|
Error() error
|
|
|
|
Kind() NotificationKind
|
|
}
|
|
|
|
func MakeNext[T any](v T) Notification[T] {
|
|
return val[T]{v}
|
|
}
|
|
func MakeError[T any](e error) Notification[T] {
|
|
return err[T]{e}
|
|
}
|
|
|
|
type val[T any] struct {
|
|
value T
|
|
}
|
|
type err[T any] struct {
|
|
err error
|
|
}
|
|
|
|
func (v val[T]) Value() T {
|
|
return v.value
|
|
}
|
|
func (v val[T]) Error() error {
|
|
return nil
|
|
}
|
|
func (v val[T]) Kind() NotificationKind {
|
|
return NextNotification
|
|
}
|
|
|
|
func (e err[T]) Value() T {
|
|
panic("tried to .Value() on an Error notification")
|
|
}
|
|
func (e err[T]) Error() error {
|
|
return e.err
|
|
}
|
|
func (e err[T]) Kind() NotificationKind {
|
|
return ErrorNotification
|
|
}
|