OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 /** | 5 /** |
6 * @fileoverview Listener functions which translate events from the | 6 * @fileoverview Listener functions which translate events from the |
7 * chrome.bookmarks API into actions to modify the local page state. | 7 * chrome.bookmarks API into actions to modify the local page state. |
8 */ | 8 */ |
9 | 9 |
10 cr.define('bookmarks.ApiListener', function() { | 10 cr.define('bookmarks.ApiListener', function() { |
| 11 |
| 12 /** @type {?number} */ |
| 13 var timerHandle; |
| 14 |
| 15 /** |
| 16 * Batches UI updates so that no changes will be made to UI until the next |
| 17 * task after the last call to this method. This is useful for listeners which |
| 18 * can be called in a tight loop by UI actions. |
| 19 */ |
| 20 function batchUIUpdates() { |
| 21 if (timerHandle) |
| 22 clearTimeout(timerHandle); |
| 23 else |
| 24 bookmarks.Store.getInstance().beginBatchUpdate(); |
| 25 |
| 26 timerHandle = setTimeout(function() { |
| 27 bookmarks.Store.getInstance().endBatchUpdate(); |
| 28 timerHandle = null; |
| 29 }); |
| 30 } |
| 31 |
11 /** @param {Action} action */ | 32 /** @param {Action} action */ |
12 function dispatch(action) { | 33 function dispatch(action) { |
13 bookmarks.Store.getInstance().dispatch(action); | 34 bookmarks.Store.getInstance().dispatch(action); |
14 } | 35 } |
15 | 36 |
16 /** | 37 /** |
17 * @param {string} id | 38 * @param {string} id |
18 * @param {{title: string, url: (string|undefined)}} changeInfo | 39 * @param {{title: string, url: (string|undefined)}} changeInfo |
19 */ | 40 */ |
20 function onBookmarkChanged(id, changeInfo) { | 41 function onBookmarkChanged(id, changeInfo) { |
21 dispatch(bookmarks.actions.editBookmark(id, changeInfo)); | 42 dispatch(bookmarks.actions.editBookmark(id, changeInfo)); |
22 } | 43 } |
23 | 44 |
24 /** | 45 /** |
25 * @param {string} id | 46 * @param {string} id |
26 * @param {BookmarkTreeNode} treeNode | 47 * @param {BookmarkTreeNode} treeNode |
27 */ | 48 */ |
28 function onBookmarkCreated(id, treeNode) { | 49 function onBookmarkCreated(id, treeNode) { |
| 50 batchUIUpdates(); |
29 dispatch(bookmarks.actions.createBookmark(id, treeNode)); | 51 dispatch(bookmarks.actions.createBookmark(id, treeNode)); |
30 } | 52 } |
31 | 53 |
32 /** | 54 /** |
33 * @param {string} id | 55 * @param {string} id |
34 * @param {{parentId: string, index: number}} removeInfo | 56 * @param {{parentId: string, index: number}} removeInfo |
35 */ | 57 */ |
36 function onBookmarkRemoved(id, removeInfo) { | 58 function onBookmarkRemoved(id, removeInfo) { |
| 59 batchUIUpdates(); |
37 var nodes = bookmarks.Store.getInstance().data.nodes; | 60 var nodes = bookmarks.Store.getInstance().data.nodes; |
38 dispatch(bookmarks.actions.removeBookmark( | 61 dispatch(bookmarks.actions.removeBookmark( |
39 id, removeInfo.parentId, removeInfo.index, nodes)); | 62 id, removeInfo.parentId, removeInfo.index, nodes)); |
40 } | 63 } |
41 | 64 |
42 /** | 65 /** |
43 * @param {string} id | 66 * @param {string} id |
44 * @param {{ | 67 * @param {{ |
45 * parentId: string, | 68 * parentId: string, |
46 * index: number, | 69 * index: number, |
47 * oldParentId: string, | 70 * oldParentId: string, |
48 * oldIndex: number | 71 * oldIndex: number |
49 * }} moveInfo | 72 * }} moveInfo |
50 */ | 73 */ |
51 function onBookmarkMoved(id, moveInfo) { | 74 function onBookmarkMoved(id, moveInfo) { |
| 75 batchUIUpdates(); |
52 dispatch(bookmarks.actions.moveBookmark( | 76 dispatch(bookmarks.actions.moveBookmark( |
53 id, moveInfo.parentId, moveInfo.index, moveInfo.oldParentId, | 77 id, moveInfo.parentId, moveInfo.index, moveInfo.oldParentId, |
54 moveInfo.oldIndex)); | 78 moveInfo.oldIndex)); |
55 } | 79 } |
56 | 80 |
57 /** | 81 /** |
58 * @param {string} id | 82 * @param {string} id |
59 * @param {{childIds: !Array<string>}} reorderInfo | 83 * @param {{childIds: !Array<string>}} reorderInfo |
60 */ | 84 */ |
61 function onChildrenReordered(id, reorderInfo) { | 85 function onChildrenReordered(id, reorderInfo) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 132 |
109 cr.sendWithPromise('getCanEditBookmarks').then(onCanEditBookmarksChanged); | 133 cr.sendWithPromise('getCanEditBookmarks').then(onCanEditBookmarksChanged); |
110 cr.addWebUIListener( | 134 cr.addWebUIListener( |
111 'can-edit-bookmarks-changed', onCanEditBookmarksChanged); | 135 'can-edit-bookmarks-changed', onCanEditBookmarksChanged); |
112 } | 136 } |
113 | 137 |
114 return { | 138 return { |
115 init: init, | 139 init: init, |
116 }; | 140 }; |
117 }); | 141 }); |
OLD | NEW |