11import { tick } from 'svelte' ;
22// eslint-disable-next-line @typescript-eslint/no-unused-vars
33import type { Action , ActionReturn } from 'svelte/action' ;
4- import { writable } from 'svelte/store' ;
54
65import { ATTRIBUTES , EVENTS , cache , intersectionObserverCallback } from './toc.internal' ;
7- import type { ActiveTocItemStore } from './toc.internal' ;
86import { compare , resolve } from './toc.parameters' ;
97import type {
108 ResolvedTocParameters ,
@@ -97,13 +95,12 @@ export const toc: Action<HTMLElement, UserTocParameters, TocAttributes> = functi
9795 let resolved = resolve ( parameters ) ;
9896
9997 let items : TocCacheItem [ 'items' ] = { } ;
100- const activeTocItemStore = writable < string | undefined > ( undefined ) satisfies ActiveTocItemStore ;
10198
10299 // stay minimal by reusing as few `IntersectionObserver` as possible
103100 // only create new `IntersectionObserver` for each new `threshold`
104101 const observers : Record < number , IntersectionObserver > = { } ;
105102
106- const activeTocItemStoreUnsubscribe = activeTocItemStore . subscribe ( ( activeTocItemId ) => {
103+ function updateActiveTocItem ( activeTocItemId ?: string ) {
107104 if ( activeTocItemId ) {
108105 const detail : TocChangeEventDetails = {
109106 id : resolved . id ,
@@ -112,7 +109,7 @@ export const toc: Action<HTMLElement, UserTocParameters, TocAttributes> = functi
112109 node . dispatchEvent ( new CustomEvent ( EVENTS . change , { detail } ) ) ;
113110 resolved . store ?. set ( { ...detail , items } ) ;
114111 }
115- } ) ;
112+ }
116113
117114 tick ( ) . then ( async ( ) => {
118115 const { id, selector, anchor, observe, scrollMarginTop } = resolved ;
@@ -228,7 +225,7 @@ export const toc: Action<HTMLElement, UserTocParameters, TocAttributes> = functi
228225 if ( observers [ threshold ] ) {
229226 observer = observers [ threshold ] ;
230227 } else {
231- observer = new IntersectionObserver ( intersectionObserverCallback ( activeTocItemStore ) , {
228+ observer = new IntersectionObserver ( intersectionObserverCallback ( updateActiveTocItem ) , {
232229 threshold,
233230 rootMargin,
234231 root,
@@ -271,7 +268,6 @@ export const toc: Action<HTMLElement, UserTocParameters, TocAttributes> = functi
271268 // - re-run operations
272269 } ,
273270 destroy ( ) {
274- activeTocItemStoreUnsubscribe ( ) ;
275271 for ( const observer of Object . values ( observers ) ) {
276272 observer . disconnect ( ) ;
277273 }
0 commit comments