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) { |
| 64 return nodes && nodes[0]; |
| 65 }, function(error) { |
| 66 console.error(error.message); |
| 67 }); |
| 68 loadingPromises[id].then(function() { |
58 delete loadingPromises[id]; | 69 delete loadingPromises[id]; |
59 return nodes && nodes[0]; | |
60 }); | 70 }); |
61 } | 71 } |
62 return loadingPromises[id]; | 72 return loadingPromises[id]; |
63 } | 73 } |
64 | 74 |
65 /** | 75 /** |
66 * Loads the entire bookmark tree and returns a {@code Promise} that will | 76 * 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 | 77 * 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. | 78 * the same tree more than once at the same time. |
69 * @return {!Promise<!BookmarkTreeNode>} The future promise for the load. | 79 * @return {!Promise<!BookmarkTreeNode>} The future promise for the load. |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 }; | 252 }; |
243 | 253 |
244 return { | 254 return { |
245 contains: contains, | 255 contains: contains, |
246 isFolder: isFolder, | 256 isFolder: isFolder, |
247 loadSubtree: loadSubtree, | 257 loadSubtree: loadSubtree, |
248 loadTree: loadTree, | 258 loadTree: loadTree, |
249 addBookmarkModelListeners: addBookmarkModelListeners | 259 addBookmarkModelListeners: addBookmarkModelListeners |
250 }; | 260 }; |
251 }); | 261 }); |
OLD | NEW |