Skip to content

Commit 32334da

Browse files
committed
feat: try to refactor vue adapter
1 parent b24427f commit 32334da

2 files changed

Lines changed: 33 additions & 21 deletions

File tree

packages/vue-table/src/createTableHelper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
export type TableHelper<
1313
TFeatures extends TableFeatures,
1414
TData extends RowData = any,
15-
> = Omit<TableHelper_Core<TFeatures, TData>, 'tableCreator'> & {
15+
> = Omit<TableHelper_Core<TFeatures>, 'tableCreator'> & {
1616
useTable: <TSelected = {}>(
1717
tableOptions: Omit<
1818
TableOptionsWithReactiveData<TFeatures, TData>,
@@ -26,9 +26,9 @@ export function createTableHelper<
2626
TFeatures extends TableFeatures,
2727
TData extends RowData,
2828
>(
29-
tableHelperOptions: TableHelperOptions<TFeatures, TData>,
29+
tableHelperOptions: TableHelperOptions<TFeatures>,
3030
): TableHelper<TFeatures, TData> {
31-
const tableHelper = constructTableHelper(useTable, tableHelperOptions)
31+
const tableHelper = constructTableHelper(useTable as any, tableHelperOptions)
3232
return {
3333
...tableHelper,
3434
useTable: <TSelected = {}>(

packages/vue-table/src/useTable.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
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'
36
import { useStore } from '@tanstack/vue-store'
47
import { mergeProxy } from './merge-proxy'
58
import type {
@@ -74,8 +77,23 @@ export function useTable<
7477
selector: (state: TableState<TFeatures>) => TSelected = () =>
7578
({}) as TSelected,
7679
): VueTable<TFeatures, TData, TSelected> {
80+
const notifier = ref<number>(0)
81+
82+
const vueReactivityFeature = constructReactivityFeature({
83+
stateNotifier: () => notifier.value,
84+
})
7785
const IS_REACTIVE = isRef(tableOptions.data)
7886

87+
tableOptions = {
88+
...tableOptions,
89+
_features: {
90+
...tableOptions._features,
91+
vueReactivityFeature,
92+
},
93+
}
94+
95+
console.log(tableOptions)
96+
7997
const statefulOptions = mergeProxy(
8098
IS_REACTIVE ? getOptionsWithReactiveData(tableOptions) : tableOptions,
8199
{
@@ -125,24 +143,18 @@ export function useTable<
125143
)
126144
}
127145

128-
/**
129-
* Temp force reactivity to all state changes on every table.get* method
130-
*/
131146
const allState = useStore(table.store, (state) => state)
147+
const allOptions = useStore(table.baseOptionsStore, (state) => state)
132148

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+
)
146158

147159
table.Subscribe = function Subscribe<TSelected>(props: {
148160
selector: (state: TableState<TFeatures>) => TSelected

0 commit comments

Comments
 (0)