observable.behavior.go 579 B

12345678910111213141516171819202122232425262728293031
  1. package api
  2. type BehaviorSubjectStruct struct {
  3. Subscriber
  4. initial interface{}
  5. }
  6. func BehaviorSubject(initial ...interface{}) *BehaviorSubjectStruct {
  7. var (
  8. value interface{}
  9. )
  10. if len(initial) > 0 {
  11. value = initial[0]
  12. }
  13. return &BehaviorSubjectStruct{
  14. initial: value,
  15. Subscriber: *NewSubscriber(),
  16. }
  17. }
  18. func (behavior *BehaviorSubjectStruct) Subscribe(handles ...func(...interface{})) (subscription *Subscription) {
  19. subscription = behavior.Subscriber.Subscribe(handles...)
  20. if behavior.initial != nil {
  21. subscription.Next(behavior.initial)
  22. }
  23. return
  24. }