@@ -137,7 +137,7 @@ import VRendererCell from './VRendererCell.vue'
137137import VAggregatorCell from ' ./VAggregatorCell.vue'
138138import VDragAndDropCell from ' ./VDragAndDropCell.vue'
139139import VPivottable from ' ../pivottable/VPivottable.vue'
140- import { computed , watch } from ' vue'
140+ import { computed , watch , shallowRef , watchEffect , onUnmounted } from ' vue'
141141import {
142142 usePropsState ,
143143 useMaterializeInput ,
@@ -243,7 +243,38 @@ const unusedAttrs = computed(() => {
243243 .sort (sortAs (pivotUiState .unusedOrder ))
244244})
245245
246- const pivotData = computed (() => new PivotData (state ))
246+ // Use shallowRef instead of computed to prevent creating new PivotData instances on every access
247+ const pivotData = shallowRef (new PivotData (state ))
248+
249+ // Update pivotData when state changes, and clean up the watcher
250+ const stopWatcher = watchEffect (() => {
251+ // Clean up old PivotData if exists
252+ const oldPivotData = pivotData .value
253+ pivotData .value = new PivotData (state )
254+
255+ // Clear old data references
256+ if (oldPivotData ) {
257+ oldPivotData .tree = {}
258+ oldPivotData .rowKeys = []
259+ oldPivotData .colKeys = []
260+ oldPivotData .rowTotals = {}
261+ oldPivotData .colTotals = {}
262+ oldPivotData .filteredData = []
263+ }
264+ })
265+
266+ // Clean up on unmount
267+ onUnmounted (() => {
268+ stopWatcher ()
269+ if (pivotData .value ) {
270+ pivotData .value .tree = {}
271+ pivotData .value .rowKeys = []
272+ pivotData .value .colKeys = []
273+ pivotData .value .rowTotals = {}
274+ pivotData .value .colTotals = {}
275+ pivotData .value .filteredData = []
276+ }
277+ })
247278const pivotProps = computed (() => ({
248279 data: state .data ,
249280 aggregators: state .aggregators ,
@@ -274,6 +305,8 @@ onUpdateUnusedOrder(unusedAttrs.value)
274305
275306provideFilterBox (pivotProps .value )
276307
308+ // Remove deep watch to prevent memory leak
309+ // Deep watch creates thousands of property watchers in Vue 3
277310watch (
278311 () => props .pivotModel ,
279312 (newModel ) => {
@@ -297,14 +330,16 @@ watch(
297330watch (
298331 [allFilters , materializedInput ],
299332 () => {
333+ // Only update the changed properties, not the entire state
300334 updateMultiple ({
301- ... state ,
302335 allFilters: allFilters .value ,
303- materializedInput: materializedInput .value
336+ materializedInput: materializedInput .value ,
337+ data: materializedInput .value // Ensure data is also updated
304338 })
305339 },
306340 {
307- deep: true
341+ immediate: true // Add immediate to ensure initial update
342+ // Removed deep: true - this was causing 80% of memory leak
308343 }
309344)
310345 </script >
0 commit comments