// watchFew is used if there are only a few watchers as a performance
// optimization.
491367
func watchFew(ctx context.Context, ch []<-chan struct{}) error {
	select {

	case <-ch[0]:
		return nil

	case <-ch[1]:
		return nil

	case <-ch[2]:
		return nil

	case <-ch[3]:
		return nil

	case <-ch[4]:
		return nil

	case <-ch[5]:
		return nil

	case <-ch[6]:
		return nil

	case <-ch[7]:
		return nil

	case <-ch[8]:
		return nil

	case <-ch[9]:
		return nil

	case <-ch[10]:
		return nil

	case <-ch[11]:
		return nil

	case <-ch[12]:
		return nil

	case <-ch[13]:
		return nil

	case <-ch[14]:
		return nil

	case <-ch[15]:
		return nil

	case <-ch[16]:
		return nil

	case <-ch[17]:
		return nil

	case <-ch[18]:
		return nil

	case <-ch[19]:
		return nil

	case <-ch[20]:
		return nil

	case <-ch[21]:
		return nil

	case <-ch[22]:
		return nil

	case <-ch[23]:
		return nil

	case <-ch[24]:
		return nil

	case <-ch[25]:
		return nil

	case <-ch[26]:
		return nil

	case <-ch[27]:
		return nil

	case <-ch[28]:
		return nil

	case <-ch[29]:
		return nil

	case <-ch[30]:
		return nil

	case <-ch[31]:
		return nil

	case <-ctx.Done():
		return ctx.Err()
	}
}
316333
func (manager *configManager) WatchNode(name string, cb func(config *NodeConfig) error) error {
	manager.Lock()
	defer manager.Unlock()

	if _, present := manager.cbNode[name]; present {
		return fmt.Errorf("%s already present", name)
	}
	manager.cbNode[name] = cb
	return nil
}
661574
func (facade *Facade) Watch(entities params.Entities) params.NotifyWatchResults {
	count := len(entities.Entities)
	results := params.NotifyWatchResults{
		Results: make([]params.NotifyWatchResult, count),
	}
	for i, entity := range entities.Entities {
		id, err := facade.oneWatch(entity.Tag)
		results.Results[i].NotifyWatcherId = id
		results.Results[i].Error = common.ServerError(err)
	}
	return results
}
929932
func (api *CleanerAPI) WatchCleanups() (params.NotifyWatchResult, error) {
	watch := api.st.WatchCleanups()
	if _, ok := <-watch.Changes(); ok {
		return params.NotifyWatchResult{
			NotifyWatcherId: api.resources.Register(watch),
		}, nil
	}
	return params.NotifyWatchResult{
		Error: common.ServerError(watcher.EnsureErr(watch)),
	}, nil
}
649895
func (w *machineUnitsWatcher) watchNewUnits(unitNames, pending []string, unitColl mongo.Collection) ([]string, error) {
	if len(unitNames) == 0 {
		return pending, nil
	}
	ids := make([]interface{}, len(unitNames))
	for i := range unitNames {
		ids[i] = w.backend.docID(unitNames[i])
	}
	logger.Tracef("for machine %q watching new units %q", w.machine.doc.DocID, unitNames)
	err := w.watcher.WatchMulti(unitsC, ids, w.in)
	if err != nil {
		return nil, errors.Trace(err)
	}

	if unitColl == nil {
		var closer SessionCloser
		unitColl, closer = w.db.GetCollection(unitsC)
		defer closer()
	}
	var doc unitDoc
	iter := unitColl.Find(bson.M{"_id": bson.M{"$in": unitNames}}).Iter()
	unknownSubs := set.NewStrings()
	notfound := set.NewStrings(unitNames...)
	for iter.Next(&doc) {
		notfound.Remove(doc.Name)
		w.known[doc.Name] = doc.Life
		pending = append(pending, doc.Name)
		// now load subordinates
		for _, subunitName := range doc.Subordinates {
			if _, subknown := w.known[subunitName]; !subknown {
				unknownSubs.Add(subunitName)
			}
		}
	}
	if err := iter.Close(); err != nil {
		return nil, errors.Trace(err)
	}
	for name := range notfound {
		logger.Debugf("unit %q referenced but not found", name)
		w.watcher.Unwatch(unitsC, w.backend.docID(name), w.in)
	}
	if !unknownSubs.IsEmpty() {
		pending, err = w.watchNewUnits(unknownSubs.Values(), pending, unitColl)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	return pending, nil
}
1154329
func (p *PushEventRepository) GetWatchersCount() int {
	if p == nil || p.WatchersCount == nil {
		return 0
	}
	return *p.WatchersCount
}
998759
func (a *Application) WatchCharmConfig() (NotifyWatcher, error) {
	configKey := a.charmConfigKey()
	return newEntityWatcher(a.st, settingsC, a.st.docID(configKey)), nil
}
967789
func (s *StorageProvisionerAPIv3) WatchBlockDevices(args params.Entities) (params.NotifyWatchResults, error) {
	canAccess, err := s.getBlockDevicesAuthFunc()
	if err != nil {
		return params.NotifyWatchResults{}, common.ServerError(common.ErrPerm)
	}
	results := params.NotifyWatchResults{
		Results: make([]params.NotifyWatchResult, len(args.Entities)),
	}
	one := func(arg params.Entity) (string, error) {
		machineTag, err := names.ParseMachineTag(arg.Tag)
		if err != nil {
			return "", err
		}
		if !canAccess(machineTag) {
			return "", common.ErrPerm
		}
		w := s.sb.WatchBlockDevices(machineTag)
		if _, ok := <-w.Changes(); ok {
			return s.resources.Register(w), nil
		}
		return "", watcher.EnsureErr(w)
	}
	for i, arg := range args.Entities {
		var result params.NotifyWatchResult
		id, err := one(arg)
		if err != nil {
			result.Error = common.ServerError(err)
		} else {
			result.NotifyWatcherId = id
		}
		results.Results[i] = result
	}
	return results, nil
}
835075
func NewWatcher() (*RWatcher, error) {
	fsWatch, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}

	m := &RWatcher{}
	m.fsnotify = fsWatch
	m.Events = make(chan fsnotify.Event)
	m.Errors = make(chan error)
	m.done = make(chan struct{})

	go m.start()

	return m, nil
}
1167062
func (f *Facade) WatchUnits(args params.Entities) (params.StringsWatchResults, error) {
	results := params.StringsWatchResults{
		Results: make([]params.StringsWatchResult, len(args.Entities)),
	}
	for i, arg := range args.Entities {
		id, changes, err := f.watchUnits(arg.Tag)
		if err != nil {
			results.Results[i].Error = common.ServerError(err)
			continue
		}
		results.Results[i].StringsWatcherId = id
		results.Results[i].Changes = changes
	}
	return results, nil
}
982488
func (c *Client) WatchAllModels() (*api.AllWatcher, error) {
	var info params.AllWatcherId
	if err := c.facade.FacadeCall("WatchAllModels", nil, &info); err != nil {
		return nil, err
	}
	return api.NewAllModelWatcher(c.facade.RawAPICaller(), &info.AllWatcherId), nil
}
879347
func (f *FirewallerAPIV3) WatchOpenedPorts(args params.Entities) (params.StringsWatchResults, error) {
	result := params.StringsWatchResults{
		Results: make([]params.StringsWatchResult, len(args.Entities)),
	}
	if len(args.Entities) == 0 {
		return result, nil
	}
	canWatch, err := f.accessModel()
	if err != nil {
		return params.StringsWatchResults{}, errors.Trace(err)
	}
	for i, entity := range args.Entities {
		tag, err := names.ParseTag(entity.Tag)
		if err != nil {
			result.Results[i].Error = common.ServerError(common.ErrPerm)
			continue
		}
		if !canWatch(tag) {
			result.Results[i].Error = common.ServerError(common.ErrPerm)
			continue
		}
		watcherId, initial, err := f.watchOneModelOpenedPorts(tag)
		if err != nil {
			result.Results[i].Error = common.ServerError(err)
			continue
		}
		result.Results[i].StringsWatcherId = watcherId
		result.Results[i].Changes = initial
	}
	return result, nil
}
54560
func (c *Int) Watch(watch IntWatch) {
	c.mutex.Lock()
	defer c.mutex.Unlock()
	c.watches = append(c.watches, watch)
}
1221986
func (c *FakeExamples) Watch(opts v1.ListOptions) (watch.Interface, error) {
	return c.Fake.
		InvokesWatch(testing.NewWatchAction(examplesResource, c.ns, opts))

}
382832
func (ec *externalControllers) WatchController(controllerUUID string) NotifyWatcher {
	return newEntityWatcher(ec.st, externalControllersC, controllerUUID)
}
210490
func (m *Model) WatchConfig(keys ...string) *ConfigWatcher {
	return newConfigWatcher(keys, m.hashCache, m.hub, m.topic(modelConfigChange), m.Resident)
}
710944
func (c *Client) WatchUnits(application string) (watcher.StringsWatcher, error) {
	applicationTag, err := applicationTag(application)
	if err != nil {
		return nil, errors.Trace(err)
	}
	args := entities(applicationTag)

	var results params.StringsWatchResults
	if err := c.facade.FacadeCall("WatchUnits", args, &results); err != nil {
		return nil, err
	}
	if n := len(results.Results); n != 1 {
		return nil, errors.Errorf("expected 1 result, got %d", n)
	}
	if err := results.Results[0].Error; err != nil {
		return nil, errors.Trace(err)
	}
	w := apiwatcher.NewStringsWatcher(c.facade.RawAPICaller(), results.Results[0])
	return w, nil
}