예제 #1
0
        static bool RegisterDirectoriesInTree_NoLock(object id, IEnumerable <FilePath> directories)
        {
            Debug.Assert(Monitor.IsEntered(watchers));

            // Remove paths subscribed for this id.
            // TODO: Only modify those which need to be modified, don't register/unregister with no reason.

            bool modified = false;

            if (monitoredDirectories.TryGetValue(id, out var oldDirectories))
            {
                foreach (var dir in oldDirectories)
                {
                    var node = tree.RemoveNode(dir, id);

                    bool wasRemoved = node != null && !node.IsLive;
                    modified |= wasRemoved;
                }
            }

            // Remove the current registered directories
            monitoredDirectories.Remove(id);
            if (directories == null)
            {
                return(modified);
            }

            // Apply new ones if we have any
            var set = new HashSet <FilePath> (directories.Where(x => !x.IsNullOrEmpty));

            if (set.Count > 0)
            {
                monitoredDirectories [id] = set;
                foreach (var path in set)
                {
                    tree.AddNode(path, id);
                }

                // If we reached here, we have added at least 1 node, and the tree has changed.
                modified = true;
            }
            return(modified);
        }
예제 #2
0
        static bool RegisterDirectoriesInTree_NoLock(object id, HashSet <FilePath> set)
        {
            Debug.Assert(Monitor.IsEntered(watchers));

            // Remove paths subscribed for this id.

            bool modified = false;

            if (monitoredDirectories.TryGetValue(id, out var oldDirectories))
            {
                HashSet <FilePath> toRemove = null;
                if (set != null)
                {
                    toRemove = new HashSet <FilePath> (oldDirectories);
                    // Remove the old ones which are not in the new set.
                    toRemove.ExceptWith(set);
                }
                else
                {
                    toRemove = oldDirectories;
                }

                foreach (var dir in toRemove)
                {
                    var node = tree.RemoveNode(dir, id);

                    bool wasRemoved = node != null && !node.IsLive;
                    modified |= wasRemoved;
                }
            }

            // Remove the current registered directories
            monitoredDirectories.Remove(id);
            if (set == null)
            {
                return(modified);
            }

            HashSet <FilePath> toAdd = null;

            if (oldDirectories != null)
            {
                toAdd = new HashSet <FilePath> (set);
                toAdd.ExceptWith(oldDirectories);
            }
            else
            {
                toAdd = set;
            }

            // Apply new ones if we have any
            if (set.Count > 0)
            {
                monitoredDirectories [id] = set;
                foreach (var path in toAdd)
                {
                    tree.AddNode(path, id, out bool isNew);

                    // We have only modified the tree if there is any new pathtree node item added
                    modified |= isNew;
                }
            }
            return(modified);
        }