@@ -28,19 +28,46 @@ browser.alarms.onAlarm.addListener(alarm => {
28
28
} )
29
29
} )
30
30
31
+ const onchange = ( localId , details ) => {
32
+ browser . storage . local . get ( 'accounts' )
33
+ . then ( ( d ) => {
34
+ var accounts = d [ 'accounts' ]
35
+ Object . keys ( accounts ) . forEach ( accountId => {
36
+ Account . get ( accountId )
37
+ . then ( ( account ) => {
38
+ return Promise . all ( [
39
+ account . hasBookmark ( localId ) ,
40
+ account . getLocalRoot ( )
41
+ ] )
42
+ } )
43
+ . then ( data => {
44
+ let [ hasBookmark , localRoot ] = data
45
+ if ( ! syncing [ accountId ] && ( hasBookmark || details . parentId === localRoot ) ) syncAccount ( accountId )
46
+ } )
47
+ } )
48
+ } )
49
+ }
50
+ browser . bookmarks . onChanged . addListener ( onchange )
51
+ browser . bookmarks . onMoved . addListener ( onchange )
52
+ browser . bookmarks . onRemoved . addListener ( onchange )
53
+ browser . bookmarks . onCreated . addListener ( onchange )
54
+
31
55
var syncing = { }
56
+ , next = { }
32
57
window . syncAccount = function ( accountId ) {
33
- if ( syncing [ accountId ] ) return syncing [ accountId ] ;
58
+ if ( syncing [ accountId ] ) {
59
+ next [ accountId ] = ( ) => syncAccount ( accountId )
60
+ return
61
+ }
34
62
syncing [ accountId ] = true
35
63
Account . get ( accountId )
36
64
. then ( ( account ) => {
37
65
return account . sync ( )
38
66
} )
39
- . then (
40
- ( ) => { delete syncing [ accountId ] } ,
41
- ( er ) => {
42
- delete syncing [ accountId ]
43
- return Promise . reject ( er )
44
- }
45
- )
67
+ . then ( ( ) => { delete syncing [ accountId ] } )
68
+ . catch ( ( er ) => {
69
+ delete syncing [ accountId ]
70
+ console . error ( er )
71
+ } )
72
+ . then ( ( ) => next [ accountId ] && next [ accountId ] ( ) )
46
73
}
0 commit comments