Skip to content

Commit 0480f80

Browse files
committed
fix: a rare broken initialization with empty storage
caused by race condition: Both sidebar and bg generate default sidebar configs with different panel ids. changed: In sidebar, wait until bg set default sidebar config (or 5sec). In sidebar, setup sidebar listeners after sidebar is loaded.
1 parent cbbb0d2 commit 0480f80

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

src/services/sidebar.actions.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,30 @@ export function registerHorizontalNavBarEl(el: HTMLElement): void {
6464
horizontalNavBarEl = el
6565
}
6666

67+
async function loadSidebarConfig() {
68+
return browser.storage.managed
69+
.get<Stored>('sidebar')
70+
.catch(() => undefined)
71+
.then(storage => (!storage?.sidebar ? browser.storage.local.get<Stored>('sidebar') : storage))
72+
}
73+
6774
export async function loadPanels(): Promise<void> {
6875
const ts = performance.now()
6976
Logs.info('Sidebar.loadPanels')
7077

71-
const gettingActiveId = browser.sessions.getWindowValue<ID>(Windows.id, 'activePanelId')
72-
const gettingHiddenPanels = browser.sessions.getWindowValue<ID[]>(Windows.id, 'hiddenPanels')
73-
const gettingStorage = browser.storage.managed
74-
.get<Stored>('sidebar')
75-
.catch(() => {})
76-
.then(storage => (!storage?.sidebar ? browser.storage.local.get<Stored>('sidebar') : storage))
77-
78-
const [activeId, storage, hiddenPanels] = await Promise.all([
79-
gettingActiveId,
80-
gettingStorage,
81-
gettingHiddenPanels,
78+
const [storage, activeId, hiddenPanels] = await Promise.all([
79+
Utils.pending({
80+
action: loadSidebarConfig,
81+
check: storage => !!storage.sidebar?.nav?.length,
82+
interval: 250,
83+
tryCount: 20,
84+
}),
85+
browser.sessions.getWindowValue<ID>(Windows.id, 'activePanelId').catch(() => undefined),
86+
browser.sessions.getWindowValue<ID[]>(Windows.id, 'hiddenPanels').catch(() => undefined),
8287
])
8388

8489
if (!storage.sidebar?.nav?.length) {
85-
Logs.warn('Sidebar.loadPanels: Creating default sidebar config')
90+
Logs.warn('Sidebar.loadPanels: No sidebar config: Creating default sidebar config')
8691
storage.sidebar = createDefaultSidebarConfig()
8792
}
8893

src/services/tabs.fg.actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ async function restoreTabPanelsContent(tabs: Tab[]) {
425425
}
426426

427427
if (tabsAreUnsorted) {
428-
Logs.warn('Tabs.restoreTabPanelsContent: Tabs are unsorted!')
428+
Logs.warn('Tabs.restoreTabPanelsContent: Tabs are unsorted; unmatchIndex:', unmatchIndex)
429429

430430
// Sort tabs
431431
Tabs.ignoreMoveEvents(true, 'initsort')

src/sidebar/sidebar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ async function main(): Promise<void> {
113113
Permissions.setupListeners()
114114
Windows.setupWindowsListeners()
115115
Containers.setupContainersListeners()
116-
Sidebar.setupListeners()
117116

118117
Styles.loadCustomSidebarCSS()
119118
Styles.initColorScheme()
120119

121120
await Sidebar.loadPanels()
121+
Sidebar.setupListeners()
122122

123123
const actPanel = Sidebar.panelsById[Sidebar.activePanelId]
124124
const initBookmarks = !Settings.state.loadBookmarksOnDemand || Utils.isBookmarksPanel(actPanel)

src/utils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,38 @@ export async function retry(conf: RetryConfig): Promise<void> {
952952
})
953953
}
954954

955+
interface PendingConfig<R> {
956+
action: () => Promise<R>
957+
check: (result: R) => boolean
958+
interval: number
959+
tryCount: number
960+
}
961+
962+
export async function pending<R>(conf: PendingConfig<R>): Promise<R> {
963+
return new Promise<R>(async (ok, meh) => {
964+
let i = 0
965+
966+
while (true) {
967+
let result
968+
try {
969+
result = await conf.action()
970+
} catch (err) {
971+
return meh(err)
972+
}
973+
974+
if (conf.check(result)) {
975+
return ok(result)
976+
}
977+
978+
if (++i >= conf.tryCount) {
979+
return ok(result)
980+
}
981+
982+
await sleep(conf.interval)
983+
}
984+
})
985+
}
986+
955987
/**
956988
* Search back then forth
957989
*/

0 commit comments

Comments
 (0)