|
1 | | -import { isRef, unref, watch } from 'vue' |
2 | | -import { constructTable } from '@tanstack/table-core' |
| 1 | +import { isRef, ref, unref, watch } from 'vue' |
| 2 | +import { |
| 3 | + constructReactivityFeature, |
| 4 | + constructTable, |
| 5 | +} from '@tanstack/table-core' |
3 | 6 | import { useStore } from '@tanstack/vue-store' |
4 | 7 | import { mergeProxy } from './merge-proxy' |
5 | 8 | import type { |
@@ -74,8 +77,23 @@ export function useTable< |
74 | 77 | selector: (state: TableState<TFeatures>) => TSelected = () => |
75 | 78 | ({}) as TSelected, |
76 | 79 | ): VueTable<TFeatures, TData, TSelected> { |
| 80 | + const notifier = ref<number>(0) |
| 81 | + |
| 82 | + const vueReactivityFeature = constructReactivityFeature({ |
| 83 | + stateNotifier: () => notifier.value, |
| 84 | + }) |
77 | 85 | const IS_REACTIVE = isRef(tableOptions.data) |
78 | 86 |
|
| 87 | + tableOptions = { |
| 88 | + ...tableOptions, |
| 89 | + _features: { |
| 90 | + ...tableOptions._features, |
| 91 | + vueReactivityFeature, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + console.log(tableOptions) |
| 96 | + |
79 | 97 | const statefulOptions = mergeProxy( |
80 | 98 | IS_REACTIVE ? getOptionsWithReactiveData(tableOptions) : tableOptions, |
81 | 99 | { |
@@ -125,24 +143,18 @@ export function useTable< |
125 | 143 | ) |
126 | 144 | } |
127 | 145 |
|
128 | | - /** |
129 | | - * Temp force reactivity to all state changes on every table.get* method |
130 | | - */ |
131 | 146 | const allState = useStore(table.store, (state) => state) |
| 147 | + const allOptions = useStore(table.baseOptionsStore, (state) => state) |
132 | 148 |
|
133 | | - // Wrap all "get*" methods to make them reactive |
134 | | - // Access allState.value directly to create reactive dependency |
135 | | - Object.keys(table).forEach((key) => { |
136 | | - const value = (table as any)[key] |
137 | | - if (typeof value === 'function' && key.startsWith('get')) { |
138 | | - const originalMethod = value.bind(table) |
139 | | - ;(table as any)[key] = (...args: Array<any>) => { |
140 | | - // Access state to create reactive dependency |
141 | | - allState.value |
142 | | - return originalMethod(...args) |
143 | | - } |
144 | | - } |
145 | | - }) |
| 149 | + watch( |
| 150 | + () => { |
| 151 | + return { state: allState.value, options: allOptions.value } |
| 152 | + }, |
| 153 | + () => { |
| 154 | + notifier.value++ |
| 155 | + }, |
| 156 | + { immediate: true }, |
| 157 | + ) |
146 | 158 |
|
147 | 159 | table.Subscribe = function Subscribe<TSelected>(props: { |
148 | 160 | selector: (state: TableState<TFeatures>) => TSelected |
|
0 commit comments