-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrefcount.go
More file actions
44 lines (42 loc) · 1.37 KB
/
refcount.go
File metadata and controls
44 lines (42 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package rx
import (
"sync"
"sync/atomic"
)
// RefCount converts a Connectable Observable into a standard Observable that automatically
// connects when the first subscriber subscribes and disconnects when the last subscriber
// unsubscribes.
//
// When the first subscriber subscribes to the resulting Observable, it automatically calls
// Connect() on the source Connectable Observable. The connection is shared among all
// subscribers. When the last subscriber unsubscribes, the connection is automatically
// closed.
//
// This is useful for efficiently sharing expensive resources (like network connections)
// among multiple subscribers.
func (connectable Connectable[T]) RefCount() Observable[T] {
var source struct {
sync.Mutex
refcount int32
subscription *subscription
}
observable := func(observe Observer[T], scheduler Scheduler, subscriber Subscriber) {
source.Lock()
if atomic.AddInt32(&source.refcount, 1) == 1 {
source.subscription = newSubscription(WithScheduler(subscriber.Context(), scheduler))
source.Unlock()
connectable.Connector(scheduler, source.subscription)
source.Lock()
}
source.Unlock()
subscriber.OnUnsubscribe(func() {
source.Lock()
if atomic.AddInt32(&source.refcount, -1) == 0 {
source.subscription.Unsubscribe()
}
source.Unlock()
})
connectable.Observable(observe, scheduler, subscriber)
}
return observable
}