1515package dbus
1616
1717import (
18+ "context"
1819 "errors"
1920 "log"
2021 "time"
@@ -94,16 +95,26 @@ func (c *Conn) dispatch() {
9495 }()
9596}
9697
97- // SubscribeUnits returns two unbuffered channels which will receive all changed units every
98- // interval. Deleted units are sent as nil.
98+ // Deprecated: use SubscribeUnitsContext instead.
9999func (c * Conn ) SubscribeUnits (interval time.Duration ) (<- chan map [string ]* UnitStatus , <- chan error ) {
100- return c .SubscribeUnitsCustom (interval , 0 , func (u1 , u2 * UnitStatus ) bool { return * u1 != * u2 }, nil )
100+ return c .SubscribeUnitsContext (context .Background (), interval )
101+ }
102+
103+ // SubscribeUnitsContext returns two unbuffered channels which will receive all changed units every
104+ // interval. Deleted units are sent as nil.
105+ func (c * Conn ) SubscribeUnitsContext (ctx context.Context , interval time.Duration ) (<- chan map [string ]* UnitStatus , <- chan error ) {
106+ return c .SubscribeUnitsCustomContext (ctx , interval , 0 , func (u1 , u2 * UnitStatus ) bool { return * u1 != * u2 }, nil )
101107}
102108
103- // SubscribeUnitsCustom is like SubscribeUnits but lets you specify the buffer
109+ // Deprecated: use SubscribeUnitsCustomContext instead.
110+ func (c * Conn ) SubscribeUnitsCustom (interval time.Duration , buffer int , isChanged func (* UnitStatus , * UnitStatus ) bool , filterUnit func (string ) bool ) (<- chan map [string ]* UnitStatus , <- chan error ) {
111+ return c .SubscribeUnitsCustomContext (context .Background (), interval , buffer , isChanged , filterUnit )
112+ }
113+
114+ // SubscribeUnitsCustomContext is like SubscribeUnits but lets you specify the buffer
104115// size of the channels, the comparison function for detecting changes and a filter
105116// function for cutting down on the noise that your channel receives.
106- func (c * Conn ) SubscribeUnitsCustom ( interval time.Duration , buffer int , isChanged func (* UnitStatus , * UnitStatus ) bool , filterUnit func (string ) bool ) (<- chan map [string ]* UnitStatus , <- chan error ) {
117+ func (c * Conn ) SubscribeUnitsCustomContext ( ctx context. Context , interval time.Duration , buffer int , isChanged func (* UnitStatus , * UnitStatus ) bool , filterUnit func (string ) bool ) (<- chan map [string ]* UnitStatus , <- chan error ) {
107118 old := make (map [string ]* UnitStatus )
108119 statusChan := make (chan map [string ]* UnitStatus , buffer )
109120 errChan := make (chan error , buffer )
@@ -112,7 +123,7 @@ func (c *Conn) SubscribeUnitsCustom(interval time.Duration, buffer int, isChange
112123 for {
113124 timerChan := time .After (interval )
114125
115- units , err := c .ListUnits ( )
126+ units , err := c .ListUnitsContext ( ctx )
116127 if err == nil {
117128 cur := make (map [string ]* UnitStatus )
118129 for i := range units {
@@ -145,7 +156,14 @@ func (c *Conn) SubscribeUnitsCustom(interval time.Duration, buffer int, isChange
145156 errChan <- err
146157 }
147158
148- <- timerChan
159+ select {
160+ case <- timerChan :
161+ close (statusChan )
162+ close (errChan )
163+ continue
164+ case <- ctx .Done ():
165+ return
166+ }
149167 }
150168 }()
151169
0 commit comments