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

Code before:
                console.log("refreshConnections", data);

                refreshRemoteGUI(data);
            }).error($scope.emitHTTPError);
        }

        function refreshRemoteGUI(connections) {
            if (!$scope.showRemoteGUI) {
                $scope.idToRemoteGUI = {}
                return
            }
            var newCache = {};
            for (var id in connections) {
                if (!connections.hasOwnProperty(id)) {
                    continue;
                }
                if (!(id in $scope.devices)) {
                    // Avoid errors when called before first updateLocalConfig()
                    continue;
                }
                if (!(id in $scope.idToRemoteGUI)) {
                    $scope.idToRemoteGUI[id] = "";
                }
                var port = $scope.devices[id].remoteGUIPort;
                var isNotRelayConnection = !connections[id].type.includes("relay");
                if (connections[id].address != "" && isNotRelayConnection && port > 0) {
                    var newAddress = "http://" + replaceAddressPort(connections[id].address, port);
                    if (!(newAddress in $scope.remoteGUICache)) {
                        // No cached result, trigger a new port probing asynchronously
                        $scope.probeRemoteGUIAddress(id, newAddress);
                    } else {
                        newCache[newAddress] = $scope.remoteGUICache[newAddress];
                        // Copy cached probing result in the corner case of duplicate GUI
                        // addresses for different devices.  Which is useless, but
                        // possible when behind the same NAT router.
                        if (newCache[newAddress]) {
                            $scope.idToRemoteGUI[id] = newAddress;
                        } else {
                            $scope.idToRemoteGUI[id] = "";
                        }
                    }
                }
            }
            // Replace the cache to discard stale addresses
            $scope.remoteGUICache = newCache;
            console.log("refreshRemoteGUI");
        }

        function refreshErrors() {
            $http.get(urlbase + '/system/error').success(function (data) {
                $scope.errors = data.errors;

Review comment (suggestion):
I'd check and abort on `port == 0` before all of this to avoid having potentially lots of useless empty string entries in `idToRemoteGUI` (potentially deleting the entry for `id` in case it was `port > 0` before).

Code after:
                    }
                }
                $scope.connections = data;
                console.log("refreshConnections", data);

                refreshRemoteGUI(data);
            }).error($scope.emitHTTPError);
        }

        function refreshRemoteGUI(connections) {
            if (!$scope.showRemoteGUI) {
                $scope.idToRemoteGUI = {}
                return
            }
            var newCache = {};
            for (var id in connections) {
                if (!(id in $scope.devices)) {
                    // Avoid errors when called before first updateLocalConfig()
                    continue;
                }
                var port = $scope.devices[id].remoteGUIPort;
                if (port <= 0
                    || !connections[id].address
                    || connections[id].type.includes("relay")) {
                    // Relay connections never work as desired here, nor incomplete addresses
                    $scope.idToRemoteGUI[id] = "";
                    continue;
                }
                var newAddress = "http://" + replaceAddressPort(connections[id].address, port);
                if (!(newAddress in $scope.remoteGUICache)) {
                    // No cached result, trigger a new port probing asynchronously
                    $scope.probeRemoteGUIAddress(id, newAddress);
                } else {
                    newCache[newAddress] = $scope.remoteGUICache[newAddress];
                    // Copy cached probing result in the corner case of duplicate GUI
                    // addresses for different devices.  Which is useless, but
                    // possible when behind the same NAT router.
                    if (newCache[newAddress]) {
                        $scope.idToRemoteGUI[id] = newAddress;
                    } else {
                        $scope.idToRemoteGUI[id] = "";
                    }
                }
            }
            // Replace the cache to discard stale addresses
            $scope.remoteGUICache = newCache;
        }

        function refreshErrors() {
            $http.get(urlbase + '/system/error').success(function (data) {
                $scope.errors = data.errors;