Repository: syncthing/syncthing
File: lib/model/model.go (Go)

Code before:
					delete(changedFcfg, cfg.Folders[i].ID)
				}
			}
			for _, fcfg := range changedFcfg {
				cfg.Folders = append(cfg.Folders, fcfg)
			}
		})
		// Need to wait for the waiter, as this calls CommitConfiguration,
		// which sets up the folder and as we return from this call,
		// ClusterConfig starts poking at m.folderFiles and other things
		// that might not exist until the config is committed.
		w.Wait()
	}

	tempIndexFolders, states, err := m.ccHandleFolders(cm.Folders, deviceCfg, ccDeviceInfos, indexHandlerRegistry)
	if err != nil {
		return err
	}

	m.pmut.Lock()
	m.remoteFolderStates[deviceID] = states
	m.pmut.Unlock()

	m.evLogger.Log(events.RemoteClusterConfigReceived, map[string]string{
		"id": deviceID.String(),
	})

	if len(tempIndexFolders) > 0 {
		m.pmut.RLock()
		conn, ok := m.conn[deviceID]
		m.pmut.RUnlock()
		// In case we've got ClusterConfig, and the connection disappeared
		// from infront of our nose.
		if ok {
			m.progressEmitter.temporaryIndexSubscribe(conn, tempIndexFolders)
		}
	}

	if deviceCfg.Introducer {
		m.cfg.Modify(func(cfg *config.Configuration) {
			folders, devices, foldersDevices, introduced := m.handleIntroductions(deviceCfg, cm, cfg.FolderMap(), cfg.DeviceMap())
			folders, devices, deintroduced := m.handleDeintroductions(deviceCfg, foldersDevices, folders, devices)
			if !introduced && !deintroduced {
				return
			}
			cfg.Folders = make([]config.FolderConfiguration, 0, len(folders))
			for _, fcfg := range folders {
				cfg.Folders = append(cfg.Folders, fcfg)
			}
			cfg.Devices = make([]config.DeviceConfiguration, len(devices))
			for _, dcfg := range devices {

Review comment (suggestion):
If/when more info is added, the values likely won't be all strings anymore. Creating a type for the event data is much easier to deal with than these `map[string]interface{}` constructs we've been using.

Code after:
			}
			for i := range cfg.Folders {
				if fcfg, ok := changedFcfg[cfg.Folders[i].ID]; ok {
					cfg.Folders[i] = fcfg
					delete(changedFcfg, cfg.Folders[i].ID)
				}
			}
			for _, fcfg := range changedFcfg {
				cfg.Folders = append(cfg.Folders, fcfg)
			}
		})
		// Need to wait for the waiter, as this calls CommitConfiguration,
		// which sets up the folder and as we return from this call,
		// ClusterConfig starts poking at m.folderFiles and other things
		// that might not exist until the config is committed.
		w.Wait()
	}

	tempIndexFolders, states, err := m.ccHandleFolders(cm.Folders, deviceCfg, ccDeviceInfos, indexHandlerRegistry)
	if err != nil {
		return err
	}

	m.pmut.Lock()
	m.remoteFolderStates[deviceID] = states
	m.pmut.Unlock()

	m.evLogger.Log(events.ClusterConfigReceived, ClusterConfigReceivedEventData{
		Device: deviceID,
	})

	if len(tempIndexFolders) > 0 {
		m.pmut.RLock()
		conn, ok := m.conn[deviceID]
		m.pmut.RUnlock()
		// In case we've got ClusterConfig, and the connection disappeared
		// from infront of our nose.
		if ok {
			m.progressEmitter.temporaryIndexSubscribe(conn, tempIndexFolders)
		}
	}

	if deviceCfg.Introducer {
		m.cfg.Modify(func(cfg *config.Configuration) {
			folders, devices, foldersDevices, introduced := m.handleIntroductions(deviceCfg, cm, cfg.FolderMap(), cfg.DeviceMap())
			folders, devices, deintroduced := m.handleDeintroductions(deviceCfg, foldersDevices, folders, devices)
			if !introduced && !deintroduced {
				return
			}
			cfg.Folders = make([]config.FolderConfiguration, 0, len(folders))
			for _, fcfg := range folders {