refactor(breadcrumb): replace scrollBreadcrumbsStore singleton with lazy accessor

Convert the eager scrollBreadcrumbsStore singleton to getScrollBreadcrumbsStore()
(+ __resetScrollBreadcrumbsStore for tests) and add a public destroy() that
disconnects the IntersectionObserver and scroll listener. Update the feature
barrel and consumers (BreadcrumbHeader, BreadcrumbHeaderSeeded, NavigationWrapper).
This commit is contained in:
Ilia Mashkov
2026-06-01 18:46:10 +03:00
parent 6877807aaf
commit 839460726e
5 changed files with 28 additions and 6 deletions
@@ -167,6 +167,13 @@ class ScrollBreadcrumbsStore {
this.#detachScrollListener();
}
/**
* Tears down the observer and scroll listener. Call on store disposal.
*/
destroy(): void {
this.#disconnect();
}
/**
* All tracked items sorted by index
*/
@@ -272,7 +279,17 @@ export function createScrollBreadcrumbsStore(): ScrollBreadcrumbsStore {
return new ScrollBreadcrumbsStore();
}
let _scrollBreadcrumbsStore: ScrollBreadcrumbsStore | undefined;
/**
* Singleton scroll breadcrumbs store instance
* App-wide scroll breadcrumbs store, created on first access.
*/
export const scrollBreadcrumbsStore = createScrollBreadcrumbsStore();
export function getScrollBreadcrumbsStore(): ScrollBreadcrumbsStore {
return (_scrollBreadcrumbsStore ??= createScrollBreadcrumbsStore());
}
// test-only reset, so specs don't share observer/scroll state
export function __resetScrollBreadcrumbsStore() {
_scrollBreadcrumbsStore?.destroy();
_scrollBreadcrumbsStore = undefined;
}