Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Unified Diff: chrome/browser/resources/md_bookmarks/util.js

Issue 2752223004: MD Bookmarks: Remove deleted nodes from state tree (Closed)
Patch Set: Rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/md_bookmarks/util.js
diff --git a/chrome/browser/resources/md_bookmarks/util.js b/chrome/browser/resources/md_bookmarks/util.js
index bf20a151c1246529fcb4ca5c327e84df876066ce..2678e798f81730592fbbec5ead2badfd4542896a 100644
--- a/chrome/browser/resources/md_bookmarks/util.js
+++ b/chrome/browser/resources/md_bookmarks/util.js
@@ -67,9 +67,56 @@ cr.define('bookmarks.util', function() {
};
}
+ /**
+ * Get all descendants of a node, including the node itself.
+ * @param {NodeList} nodes
+ * @param {string} baseId
+ * @return {!Set<string>}
+ */
+ function getDescendants(nodes, baseId) {
+ var descendants = new Set();
+ var stack = [];
+ stack.push(baseId);
+
+ while (stack.length > 0) {
+ var id = stack.pop();
+ var node = nodes[id];
+
+ if (!node)
+ continue;
+
+ descendants.add(id);
+
+ if (!node.children)
+ continue;
+
+ node.children.forEach(function(childId) {
+ stack.push(childId);
+ });
+ }
+
+ return descendants;
+ }
+
+ /**
+ * @param {!Object<string, T>} map
+ * @param {!Set<string>} nodes
+ * @return {!Object<string, T>}
+ * @template T
+ */
+ function removeNodesFromMap(map, nodes) {
calamity 2017/04/03 06:54:24 Maybe genericize further into removeKeysFromMap? A
tsergeant 2017/04/04 04:42:28 Done, with removeIdsFromMap (and removeIdsFromSet)
+ var newMap = Object.assign({}, map);
+ nodes.forEach(function(id) {
+ delete newMap[id];
+ });
+ return newMap;
+ };
+
return {
createEmptyState: createEmptyState,
+ getDescendants: getDescendants,
getDisplayedList: getDisplayedList,
normalizeNodes: normalizeNodes,
+ removeNodesFromMap: removeNodesFromMap,
};
});

Powered by Google App Engine
This is Rietveld 408576698