@@ -4,11 +4,8 @@ import {
44 observeElementOffset ,
55 elementScroll ,
66} from " @tanstack/virtual-core"
7- import type {
8- VirtualItem ,
9- ScrollToIndexOptions ,
10- ScrollToOffsetOptions ,
11- } from " @tanstack/virtual-core"
7+ import type { VirtualItem , ScrollToOptions } from " @tanstack/virtual-core"
8+ import { setInstance , getInstance , deleteInstance } from " ./store"
129
1310export interface Input {
1411 count: number
@@ -23,32 +20,26 @@ export interface Input {
2320 gap? : number
2421 lanes? : number
2522 initialOffset? : number | (() => number )
26- // Tag parameters — the body content receives virtualItems, totalSize, etc.
2723 content: Marko .Body <[{
2824 virtualItems: VirtualItem []
2925 totalSize: number
3026 measureElement: ((el : Element | null ) => void ) | undefined
31- scrollToIndex: (index : number , options ? : ScrollToIndexOptions ) => void
32- scrollToOffset: (offset : number , options ? : ScrollToOffsetOptions ) => void
27+ scrollToIndex: (index : number , options ? : ScrollToOptions ) => void
28+ scrollToOffset: (offset : number , options ? : ScrollToOptions ) => void
3329 }]>
3430}
3531
36- // Module-level Map — class instances never stored in reactive signals
37- static const _instances = new Map <string , Virtualizer <Element , Element >>()
38- static const _cleanups = new Map <string , () => void >()
39- static let _nextId = 0
40-
41- <let /id = (` v${++ _nextId } ` ) />
32+ <let /id = (Math .random ().toString (36 ).slice (2 )) />
4233<let /items = ([] as VirtualItem []) />
4334<let /size = 0 />
4435
4536<lifecycle <{}>
4637 onMount () {
4738 const notify = () => {
48- const v = _instances . get (id )
49- if (! v ) return
50- items = v .getVirtualItems ()
51- size = v .getTotalSize ()
39+ const entry = getInstance (id )
40+ if (! entry ) return
41+ items = entry . v .getVirtualItems ()
42+ size = entry . v .getTotalSize ()
5243 }
5344
5445 const v = new Virtualizer <Element , Element >({
@@ -70,14 +61,14 @@ static let _nextId = 0
7061 onChange: notify ,
7162 })
7263
73- _instances .set (id , v )
74- _cleanups .set (id , v ._didMount ())
64+ setInstance (id , { v , cleanup: v ._didMount () })
7565 v ._willUpdate ()
7666 }
7767
7868 onUpdate () {
79- const v = _instances .get (id )
80- if (! v ) return
69+ const entry = getInstance (id )
70+ if (! entry ) return
71+ const { v } = entry
8172
8273 const notify = () => {
8374 items = v .getVirtualItems ()
@@ -102,21 +93,22 @@ static let _nextId = 0
10293 })
10394
10495 v ._willUpdate ()
96+ notify ()
10597 }
10698
10799 onDestroy () {
108- _cleanups .get (id )?.()
109- _instances .delete (id )
110- _cleanups .delete (id )
100+ const entry = getInstance (id )
101+ if (entry ) {
102+ entry .cleanup ()
103+ deleteInstance (id )
104+ }
111105 }
112106/>
113107
114- // Render the body content with virtual items passed as tag parameters.
115- // When items/size signals change, this re-renders with updated values.
116108<${input .content }
117109 virtualItems = items
118110 totalSize = size
119- measureElement = _instances . get (id )?.measureElement
120- scrollToIndex = (index : number , options ? : ScrollToIndexOptions ) => _instances . get (id )?.scrollToIndex (index , options )
121- scrollToOffset = (offset : number , options ? : ScrollToOffsetOptions ) => _instances . get (id )?.scrollToOffset (offset , options )
111+ measureElement = getInstance (id )?. v ?.measureElement
112+ scrollToIndex = (index : number , options ? : ScrollToOptions ) => getInstance (id )?. v ?.scrollToIndex (index , options )
113+ scrollToOffset = (offset : number , options ? : ScrollToOptions ) => getInstance (id )?. v ?.scrollToOffset (offset , options )
122114/>
0 commit comments