Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 cr.define('bmm', function() { | 7 cr.define('bmm', function() { |
| 8 /** | 8 /** |
| 9 * Whether a node contains another node. | 9 * Whether a node contains another node. |
| 10 * TODO(yosin): Once JavaScript style guide is updated and linter follows | 10 * TODO(yosin): Once JavaScript style guide is updated and linter follows |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 | 34 |
| 35 var loadingPromises = {}; | 35 var loadingPromises = {}; |
| 36 | 36 |
| 37 /** | 37 /** |
| 38 * Promise version of chrome.bookmarkManagerPrivate.getSubtree. | 38 * Promise version of chrome.bookmarkManagerPrivate.getSubtree. |
| 39 * @param {string} id . | 39 * @param {string} id . |
| 40 * @param {boolean} foldersOnly . | 40 * @param {boolean} foldersOnly . |
| 41 * @return {!Promise<!Array<!BookmarkTreeNode>>} . | 41 * @return {!Promise<!Array<!BookmarkTreeNode>>} . |
| 42 */ | 42 */ |
| 43 function getSubtreePromise(id, foldersOnly) { | 43 function getSubtreePromise(id, foldersOnly) { |
| 44 return new Promise(function(resolve) { | 44 return new Promise(function(resolve, reject) { |
| 45 chrome.bookmarkManagerPrivate.getSubtree(id, foldersOnly, resolve); | 45 chrome.bookmarkManagerPrivate.getSubtree(id, foldersOnly, function(node) { |
| 46 if (chrome.runtime.lastError) { | |
| 47 reject(new Error(chrome.runtime.lastError.message)); | |
| 48 return; | |
| 49 } | |
| 50 resolve(node); | |
| 51 }); | |
| 46 }); | 52 }); |
| 47 } | 53 } |
| 48 | 54 |
| 49 /** | 55 /** |
| 50 * Loads a subtree of the bookmark tree and returns a {@code Promise} that | 56 * Loads a subtree of the bookmark tree and returns a {@code Promise} that |
| 51 * will be fulfilled when done. This reuses multiple loads so that we do not | 57 * will be fulfilled when done. This reuses multiple loads so that we do not |
| 52 * load the same subtree more than once at the same time. | 58 * load the same subtree more than once at the same time. |
| 53 * @return {!Promise<!BookmarkTreeNode>} The future promise for the load. | 59 * @return {!Promise<!BookmarkTreeNode>} The future promise for the load. |
| 54 */ | 60 */ |
| 55 function loadSubtree(id) { | 61 function loadSubtree(id) { |
| 56 if (!loadingPromises[id]) { | 62 if (!loadingPromises[id]) { |
| 57 loadingPromises[id] = getSubtreePromise(id, false).then(function(nodes) { | 63 loadingPromises[id] = getSubtreePromise(id, false).then(function(nodes) { |
| 58 delete loadingPromises[id]; | 64 loadingPromises[id].then(function() { |
|
Bernhard Bauer
2015/04/30 15:08:07
You can pull this out into a single callback on lo
Deepak
2015/04/30 16:11:19
Done.
| |
| 65 delete loadingPromises[id]; | |
| 66 }); | |
| 59 return nodes && nodes[0]; | 67 return nodes && nodes[0]; |
| 68 }, function(error) { | |
| 69 loadingPromises[id].then(function() { | |
| 70 delete loadingPromises[id]; | |
| 71 }); | |
| 72 console.error(error.message); | |
| 73 return; | |
| 60 }); | 74 }); |
| 61 } | 75 } |
| 62 return loadingPromises[id]; | 76 return loadingPromises[id]; |
| 63 } | 77 } |
| 64 | 78 |
| 65 /** | 79 /** |
| 66 * Loads the entire bookmark tree and returns a {@code Promise} that will | 80 * Loads the entire bookmark tree and returns a {@code Promise} that will |
| 67 * be fulfilled when done. This reuses multiple loads so that we do not load | 81 * be fulfilled when done. This reuses multiple loads so that we do not load |
| 68 * the same tree more than once at the same time. | 82 * the same tree more than once at the same time. |
| 69 * @return {!Promise<!BookmarkTreeNode>} The future promise for the load. | 83 * @return {!Promise<!BookmarkTreeNode>} The future promise for the load. |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 }; | 256 }; |
| 243 | 257 |
| 244 return { | 258 return { |
| 245 contains: contains, | 259 contains: contains, |
| 246 isFolder: isFolder, | 260 isFolder: isFolder, |
| 247 loadSubtree: loadSubtree, | 261 loadSubtree: loadSubtree, |
| 248 loadTree: loadTree, | 262 loadTree: loadTree, |
| 249 addBookmarkModelListeners: addBookmarkModelListeners | 263 addBookmarkModelListeners: addBookmarkModelListeners |
| 250 }; | 264 }; |
| 251 }); | 265 }); |
| OLD | NEW |