2018-06-10 14:50:03 +00:00
|
|
|
package observable
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
type Observable[T any] struct {
|
|
|
|
iterable Iterable[T]
|
|
|
|
listener map[Subscription[T]]*Subscriber[T]
|
2019-10-30 07:43:55 +00:00
|
|
|
mux sync.Mutex
|
2018-06-10 14:50:03 +00:00
|
|
|
done bool
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
func (o *Observable[T]) process() {
|
2018-06-10 14:50:03 +00:00
|
|
|
for item := range o.iterable {
|
2019-10-30 07:43:55 +00:00
|
|
|
o.mux.Lock()
|
|
|
|
for _, sub := range o.listener {
|
|
|
|
sub.Emit(item)
|
|
|
|
}
|
|
|
|
o.mux.Unlock()
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
o.close()
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
func (o *Observable[T]) close() {
|
2019-10-30 07:43:55 +00:00
|
|
|
o.mux.Lock()
|
|
|
|
defer o.mux.Unlock()
|
2018-06-10 14:50:03 +00:00
|
|
|
|
2019-10-30 07:43:55 +00:00
|
|
|
o.done = true
|
|
|
|
for _, sub := range o.listener {
|
|
|
|
sub.Close()
|
|
|
|
}
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
func (o *Observable[T]) Subscribe() (Subscription[T], error) {
|
2019-10-30 07:43:55 +00:00
|
|
|
o.mux.Lock()
|
|
|
|
defer o.mux.Unlock()
|
|
|
|
if o.done {
|
2022-04-23 18:07:57 +00:00
|
|
|
return nil, errors.New("observable is closed")
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2022-04-23 18:07:57 +00:00
|
|
|
subscriber := newSubscriber[T]()
|
2019-10-30 07:43:55 +00:00
|
|
|
o.listener[subscriber.Out()] = subscriber
|
2018-06-10 14:50:03 +00:00
|
|
|
return subscriber.Out(), nil
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
func (o *Observable[T]) UnSubscribe(sub Subscription[T]) {
|
2019-10-30 07:43:55 +00:00
|
|
|
o.mux.Lock()
|
|
|
|
defer o.mux.Unlock()
|
|
|
|
subscriber, exist := o.listener[sub]
|
2018-06-10 14:50:03 +00:00
|
|
|
if !exist {
|
|
|
|
return
|
|
|
|
}
|
2019-10-30 07:43:55 +00:00
|
|
|
delete(o.listener, sub)
|
2018-06-10 14:50:03 +00:00
|
|
|
subscriber.Close()
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
func NewObservable[T any](iter Iterable[T]) *Observable[T] {
|
|
|
|
observable := &Observable[T]{
|
|
|
|
iterable: iter,
|
|
|
|
listener: map[Subscription[T]]*Subscriber[T]{},
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
go observable.process()
|
|
|
|
return observable
|
|
|
|
}
|