Repository: syncthing/syncthing
File: gui/default/syncthing/core/syncthingController.js (JavaScript)

Code before:
                    $scope.discovery = [];
                    for (var id in registry) {
                        if ($scope.discovery.length === 5) {
                            break;
                        }
                        if (id in $scope.devices) {
                            continue
                        }
                        $scope.discovery.push(id);
                    }
                })
                .then(function () {
                    $http.get(urlbase + '/config/defaults/device').then(function (p) {
                        $scope.currentDevice = p.data;
                        $scope.currentDevice.name = name;
                        $scope.currentDevice.deviceID = deviceID;
                        if (deviceID) {
                            $scope.currentDevice._editing = "pending";
                        } else {
                            $scope.currentDevice._editing = "add";
                        };
                        initShareEditing('device');
                        $scope.currentSharing.unrelated = $scope.folderList();
                        editDeviceModal();
                    }, $scope.emitHTTPError);
                });
        };

        $scope.deleteDevice = function () {
            $('#editDevice').modal('hide');
            if ($scope.currentDevice._editing != "existing") {
                return;
            }

            var id = $scope.currentDevice.deviceID
            delete $scope.devices[id];
            $scope.config.devices = $scope.deviceList();

            for (var id in $scope.folders) {
                $scope.folders[id].devices = $scope.folders[id].devices.filter(function (n) {
                    return n.deviceID !== $scope.currentDevice.deviceID;
                });
            }

            $scope.saveConfig();
        };

        $scope.saveDevice = function () {
            $('#editDevice').modal('hide');
            $scope.currentDevice.addresses = $scope.currentDevice._addressesStr.split(',').map(function (x) {
                return x.trim();

Review comment (refactor):
When adding another value to this "kind of enum" variable, you must make sure that every other use of it will not change semantics with the new value.  Did you check that?

And I think this function should be refactored to completely avoid retrieving the discovery list at all for the case when deviceID is already provided.

Code after:
        };

        $scope.addDevice = function (deviceID, name) {
            return $http.get(urlbase + '/system/discovery')
                .success(function (registry) {
                    $scope.discovery = [];
                    for (var id in registry) {
                        if ($scope.discovery.length === 5) {
                            break;
                        }
                        if (id in $scope.devices) {
                            continue
                        }
                        $scope.discovery.push(id);
                    }
                })
                .then(function () {
                    $http.get(urlbase + '/config/defaults/device').then(function (p) {
                        $scope.currentDevice = p.data;
                        $scope.currentDevice.name = name;
                        $scope.currentDevice.deviceID = deviceID;
                        if (deviceID) {
                            $scope.currentDevice._editing = "new-pending";
                        } else {
                            $scope.currentDevice._editing = "new";
                        }
                        initShareEditing('device');
                        $scope.currentSharing.unrelated = $scope.folderList();
                        editDeviceModal();
                    }, $scope.emitHTTPError);
                });
        };

        $scope.deleteDevice = function () {
            $('#editDevice').modal('hide');
            if ($scope.currentDevice._editing != "existing") {
                return;
            }

            var id = $scope.currentDevice.deviceID
            delete $scope.devices[id];
            $scope.config.devices = $scope.deviceList();

            for (var id in $scope.folders) {
                $scope.folders[id].devices = $scope.folders[id].devices.filter(function (n) {
                    return n.deviceID !== $scope.currentDevice.deviceID;
                });
            }

            $scope.saveConfig();
        };