Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-ends-wish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/angular-virtual': patch
---

Fixes writing to signal error in angular effect
90 changes: 61 additions & 29 deletions packages/angular-virtual/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
effect,
inject,
signal,
untracked,
} from '@angular/core'
import {
Virtualizer,
Expand All @@ -16,7 +15,6 @@ import {
observeWindowRect,
windowScroll,
} from '@tanstack/virtual-core'
import { proxyVirtualizer } from './proxy'
import type { ElementRef, Signal } from '@angular/core'
import type { PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core'
import type { AngularVirtualizer } from './types'
Expand All @@ -30,52 +28,86 @@ function createVirtualizerBase<
>(
options: Signal<VirtualizerOptions<TScrollElement, TItemElement>>,
): AngularVirtualizer<TScrollElement, TItemElement> {
let virtualizer: Virtualizer<TScrollElement, TItemElement>
function lazyInit() {
virtualizer ??= new Virtualizer(options())
return virtualizer
const instance = new Virtualizer<TScrollElement, TItemElement>(options())

const virtualItems = signal(instance.getVirtualItems(), {
equal: () => false,
})
const totalSize = signal(instance.getTotalSize())
const isScrolling = signal(instance.isScrolling)
const range = signal(instance.range, { equal: () => false })
const scrollDirection = signal(instance.scrollDirection)
const scrollElement = signal(instance.scrollElement)
const scrollOffset = signal(instance.scrollOffset)
const scrollRect = signal(instance.scrollRect)

const handler = {
get(
target: Virtualizer<TScrollElement, TItemElement>,
prop: keyof Virtualizer<TScrollElement, TItemElement>,
) {
switch (prop) {
case 'getVirtualItems':
return virtualItems
case 'getTotalSize':
return totalSize
case 'isScrolling':
return isScrolling
case 'options':
return options
Comment on lines +56 to +57
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options property on the returned AngularVirtualizer proxy is currently mapped to the input options signal rather than instance.options (the fully-normalized options object that Virtualizer actually uses). This changes runtime semantics and also diverges from AngularVirtualizer['options'] (which is typed as Signal<Virtualizer['options']>). Consider exposing a dedicated options signal that is updated from instance.options (e.g. in onChange and/or after setOptions) instead of returning the adapter’s options input signal here.

Copilot uses AI. Check for mistakes.
case 'range':
return range
case 'scrollDirection':
return scrollDirection
case 'scrollElement':
return scrollElement
case 'scrollOffset':
return scrollOffset
case 'scrollRect':
return scrollRect
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the adapter wrapped several argument-taking Virtualizer methods (e.g. getOffsetForIndex, getVirtualItemForOffset, indexFromElement) so their results became reactive to virtualizer updates when used from signal-driven templates/computeds. With the new proxy, these methods fall through to Reflect.get and no longer read any Angular signal, so their return values won’t automatically update when the virtualizer state changes. If this reactivity was intentional (it was explicitly implemented in the removed proxy.ts), consider reintroducing a reactive wrapper for these methods or documenting the behavior change as it can break existing consumers.

Suggested change
return scrollRect
return scrollRect
case 'getOffsetForIndex':
case 'getVirtualItemForOffset':
case 'indexFromElement':
// Wrap argument-taking query methods so they read a signal and stay reactive
return (...args: unknown[]) => {
// Access a signal that updates on virtualizer changes to register dependency
virtualItems()
// Delegate to the underlying Virtualizer method
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (target as any)[prop](...args)
}

Copilot uses AI. Check for mistakes.
default:
return Reflect.get(target, prop)
}
},
}

const virtualizerSignal = signal(virtualizer!, { equal: () => false })
const virtualizer = new Proxy(
instance,
handler,
) as unknown as AngularVirtualizer<TScrollElement, TItemElement>

// two-way sync options
effect(
() => {
const _options = options()
lazyInit()
virtualizerSignal.set(virtualizer)
virtualizer.setOptions({
instance.setOptions({
..._options,
onChange: (instance, sync) => {
// update virtualizerSignal so that dependent computeds recompute.
virtualizerSignal.set(instance)
virtualItems.set(instance.getVirtualItems())
totalSize.set(instance.getTotalSize())
isScrolling.set(instance.isScrolling)
range.set(instance.range)
scrollDirection.set(instance.scrollDirection)
scrollElement.set(instance.scrollElement)
scrollOffset.set(instance.scrollOffset)
scrollRect.set(instance.scrollRect)
_options.onChange?.(instance, sync)
},
})
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The adapter signals (virtualItems, totalSize, range, etc.) are only updated inside the onChange callback. However, Virtualizer.setOptions() does not itself trigger notify(), and _willUpdate() only notifies when the scroll element changes (or via subsequent scroll/resize observers). This means option-only changes (e.g. count changes from a signal, which is a documented use case) can leave these signals stale until an unrelated scroll/resize happens. To keep the Angular signals consistent, update them once after applying options (or otherwise force a notify) within this effect run.

Suggested change
})
})
// Ensure adapter signals stay in sync even when only options change
virtualItems.set(instance.getVirtualItems())
totalSize.set(instance.getTotalSize())
isScrolling.set(instance.isScrolling)
range.set(instance.range)
scrollDirection.set(instance.scrollDirection)
scrollElement.set(instance.scrollElement)
scrollOffset.set(instance.scrollOffset)
scrollRect.set(instance.scrollRect)

Copilot uses AI. Check for mistakes.
// update virtualizerSignal so that dependent computeds recompute.
virtualizerSignal.set(virtualizer)
instance._willUpdate()
},
{ allowSignalWrites: true },
)

const scrollElement = computed(() => options().getScrollElement())
// let the virtualizer know when the scroll element is changed
effect(
() => {
const el = scrollElement()
if (el) {
untracked(virtualizerSignal)._willUpdate()
}
let cleanup: (() => void) | undefined
afterNextRender({
read: () => {
cleanup = instance._didMount()
},
{ allowSignalWrites: true },
)

let cleanup: () => void | undefined
afterNextRender({ read: () => (virtualizer ?? lazyInit())._didMount() })
})

inject(DestroyRef).onDestroy(() => cleanup?.())

return proxyVirtualizer(virtualizerSignal, lazyInit)
return virtualizer
}

export function injectVirtualizer<
Expand Down
117 changes: 0 additions & 117 deletions packages/angular-virtual/src/proxy.ts

This file was deleted.

Loading