Skip to content

Commit

Permalink
fix(LocalTabs): Wait until tab update is finished
Browse files Browse the repository at this point in the history
before we yield to sync process

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed Nov 27, 2024
1 parent 7211af6 commit 1711129
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/Account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AdapterFactory from './AdapterFactory'
import Logger from './Logger'
import { Folder, ItemLocation, TItemLocation } from './Tree'
import { ItemLocation, TItemLocation } from './Tree'
import UnidirectionalSyncProcess from './strategies/Unidirectional'
import MergeSyncProcess from './strategies/Merge'
import DefaultSyncProcess from './strategies/Default'
Expand Down
18 changes: 17 additions & 1 deletion src/lib/LocalTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
active: false,
})
)
await awaitTabsUpdated()
return node.id
}

Expand All @@ -85,6 +86,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
index: -1 // last
})
)
await awaitTabsUpdated()
}

async removeBookmark(bookmark:Bookmark<typeof ItemLocation.LOCAL>): Promise<void> {
Expand All @@ -95,6 +97,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
return
}
await this.queue.add(() => browser.tabs.remove(bookmarkId))
await awaitTabsUpdated()
}

async createFolder(folder:Folder<typeof ItemLocation.LOCAL>): Promise<number> {
Expand Down Expand Up @@ -131,6 +134,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
throw new Error('Failed to reorder folder ' + id + ': ' + e.message)
}
}
await awaitTabsUpdated()
}

async updateFolder(folder:Folder<typeof ItemLocation.LOCAL>):Promise<void> {
Expand All @@ -140,7 +144,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
async removeFolder(folder:Folder<typeof ItemLocation.LOCAL>):Promise<void> {
const id = folder.id
Logger.log('(tabs)REMOVEFOLDER', id)
await this.queue.add(() => browser.tabs.remove(id))
await this.queue.add(() => browser.window.remove(id))
}

async isAvailable(): Promise<boolean> {
Expand All @@ -150,3 +154,15 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
return Boolean(tabs.length)
}
}

function awaitTabsUpdated() {
return Promise.race([
new Promise<void>(resolve => {
browser.tabs.onUpdated.addListener(() => {
browser.tabs.onUpdated.removeListener(resolve)
setTimeout(() => resolve(), 1000)
})
}),
new Promise(resolve => setTimeout(resolve, 1100))
])
}

0 comments on commit 1711129

Please sign in to comment.