func()

in plugins/internal/plugin/device_watcher.go [161:205]


func (d *DeviceWatcher) watchDevices() {

	// Destroy the underlying DeviceDiscovery object when the loop completes
	defer d.deviceDiscovery.Destroy()

	// Use a context for waiting between polling operations rather than sleeping, so we remain responsive to shutdown and refresh events
	sleep, cancelSleep := context.WithTimeout(context.Background(), time.Second*0)
	defer cancelSleep()

	// Continue sending device updates until shutdown is requested:
	forceRefresh := false
	for {
		select {

		case <-d.shutdown:
			return

		case <-d.refresh:
			forceRefresh = true
			cancelSleep()

		case <-sleep.Done():

			// Poll for device list changes
			refresh, err := d.deviceDiscovery.IsRefreshRequired()
			if err != nil {
				d.Errors <- err
				return
			}

			// Retrieve the updated device list if one is available or if a forced refresh has been requested
			if refresh || forceRefresh {
				if err := d.refreshDevices(); err != nil {
					d.Errors <- err
					return
				}
			}

			// Wait 10 seconds before polling again
			forceRefresh = false
			sleep, cancelSleep = context.WithTimeout(context.Background(), time.Second*10)
			defer cancelSleep()
		}
	}
}