Open History to see the last commit
1import { computed, untracked } from '@angular/core'
2import type { Signal, WritableSignal } from '@angular/core'
3import type { Virtualizer } from '@tanstack/virtual-core'
4import type { AngularVirtualizer } from './types'
5β
6export function proxyVirtualizer<
7 V extends Virtualizer<any, any>,
8 S extends Element | Window = V extends Virtualizer<infer U, any> ? U : never,
9 I extends Element = V extends Virtualizer<any, infer U> ? U : never,
10>(
11 virtualizerSignal: WritableSignal<V>,
12 lazyInit: () => V,
13): AngularVirtualizer<S, I> {
14 return new Proxy(virtualizerSignal, {
15 apply() {
16 return virtualizerSignal()
17 },
18 get(target, property) {
19 const untypedTarget = target as any
20 if (untypedTarget[property]) {
21 return untypedTarget[property]
22 }
23 let virtualizer = untracked(virtualizerSignal)
24 if (virtualizer == null) {
25 virtualizer = lazyInit()
26 untracked(() => virtualizerSignal.set(virtualizer))
27 }
28β
29 // Create computed signals for each property that represents a reactive value
30 if (
31 typeof property === 'string' &&
32 [
33 'getTotalSize',
34 'getVirtualItems',
35 'isScrolling',
36 'options',
37 'range',
38 'scrollDirection',
39 'scrollElement',
40 'scrollOffset',
41 'scrollRect',
42 'measureElementCache',
43 'measurementsCache',
44 ].includes(property)
45 ) {
46 const isFunction =
47 typeof virtualizer[property as keyof V] === 'function'
48 Object.defineProperty(untypedTarget, property, {
49 value: isFunction
50 ? computed(() => (target()[property as keyof V] as Function)())
51 : computed(() => target()[property as keyof V]),
52 configurable: true,
53 enumerable: true,
54 })
55 }
56β
57 // Create plain signals for functions that accept arguments and return reactive values
58 if (
59 typeof property === 'string' &&
60 [
61 'getOffsetForAlignment',
62 'getOffsetForIndex',
63 'getVirtualItemForOffset',
64 'indexFromElement',
65 ].includes(property)
66 ) {
67 const fn = virtualizer[property as keyof V] as Function
68 Object.defineProperty(untypedTarget, property, {
69 value: toComputed(virtualizerSignal, fn),
70 configurable: true,
71 enumerable: true,
72 })
73 }
74β
75 return untypedTarget[property] || virtualizer[property as keyof V]
76 },
77 has(_, property: string) {
78 return !!untracked(virtualizerSignal)[property as keyof V]
79 },
80 ownKeys() {
81 return Reflect.ownKeys(untracked(virtualizerSignal))
82 },
83 getOwnPropertyDescriptor() {
84 return {
85 enumerable: true,
86 configurable: true,
87 }
88 },
89 }) as unknown as AngularVirtualizer<S, I>
90}
91β
92function toComputed<V extends Virtualizer<any, any>>(
93 signal: Signal<V>,
94 fn: Function,
95) {
96 const computedCache: Record<string, Signal<unknown>> = {}
97β
98 return (...args: Array<any>) => {
99 // Cache computeds by their arguments to avoid re-creating the computed on each call
100 const serializedArgs = serializeArgs(...args)
101 if (computedCache.hasOwnProperty(serializedArgs)) {
102 return computedCache[serializedArgs]?.()
103 }
104 const computedSignal = computed(() => {
105 void signal()
106 return fn(...args)
107 })
108β
109 computedCache[serializedArgs] = computedSignal
110β
111 return computedSignal()
112 }
113}
114β
115function serializeArgs(...args: Array<any>) {
116 return JSON.stringify(args)
117}