@@ -93,14 +93,15 @@ func (c *Conn) initDispatch() {
9393}
9494
9595// Returns two unbuffered channels which will receive all changed units every
96- // @ interval@ seconds . Deleted units are sent as nil.
96+ // interval. Deleted units are sent as nil.
9797func (c * Conn ) SubscribeUnits (interval time.Duration ) (<- chan map [string ]* UnitStatus , <- chan error ) {
98- return c .SubscribeUnitsCustom (interval , 0 , func (u1 , u2 * UnitStatus ) bool { return * u1 != * u2 })
98+ return c .SubscribeUnitsCustom (interval , 0 , func (u1 , u2 * UnitStatus ) bool { return * u1 != * u2 }, nil )
9999}
100100
101101// SubscribeUnitsCustom is like SubscribeUnits but lets you specify the buffer
102- // size of the channels and the comparison function for detecting changes.
103- func (c * Conn ) SubscribeUnitsCustom (interval time.Duration , buffer int , isChanged func (* UnitStatus , * UnitStatus ) bool ) (<- chan map [string ]* UnitStatus , <- chan error ) {
102+ // size of the channels, the comparison function for detecting changes and a filter
103+ // function for cutting down on the noise that your channel receives.
104+ func (c * Conn ) SubscribeUnitsCustom (interval time.Duration , buffer int , isChanged func (* UnitStatus , * UnitStatus ) bool , filterUnit func (string ) bool ) (<- chan map [string ]* UnitStatus , <- chan error ) {
104105 old := make (map [string ]* UnitStatus )
105106 statusChan := make (chan map [string ]* UnitStatus , buffer )
106107 errChan := make (chan error , buffer )
@@ -113,6 +114,9 @@ func (c *Conn) SubscribeUnitsCustom(interval time.Duration, buffer int, isChange
113114 if err == nil {
114115 cur := make (map [string ]* UnitStatus )
115116 for i := range units {
117+ if filterUnit != nil && filterUnit (units [i ].Name ) {
118+ continue
119+ }
116120 cur [units [i ].Name ] = & units [i ]
117121 }
118122
@@ -132,7 +136,9 @@ func (c *Conn) SubscribeUnitsCustom(interval time.Duration, buffer int, isChange
132136
133137 old = cur
134138
135- statusChan <- changed
139+ if len (changed ) != 0 {
140+ statusChan <- changed
141+ }
136142 } else {
137143 errChan <- err
138144 }
0 commit comments