Skip to content

Commit 7168077

Browse files
committed
remove table proxy implementation, improve angular reactivity feature
1 parent 56ba513 commit 7168077

File tree

5 files changed

+123
-294
lines changed

5 files changed

+123
-294
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { computed, signal } from '@angular/core'
2-
import { toComputed } from './proxy'
3-
import type { Signal } from '@angular/core'
1+
import { computed, isSignal, type Signal, signal } from '@angular/core'
2+
import { defineLazyComputedProperty } from './proxy'
43
import type {
54
RowData,
65
Table,
@@ -20,20 +19,25 @@ declare module '@tanstack/table-core' {
2019
> extends Table_AngularReactivity<TFeatures, TData> {}
2120
}
2221

22+
export interface AngularReactivityFlags {
23+
header: boolean
24+
column: boolean
25+
row: boolean
26+
cell: boolean
27+
table: boolean
28+
}
29+
2330
interface TableOptions_AngularReactivity {
24-
enableExperimentalReactivity?: boolean
25-
enableCellAutoReactivity?: boolean
26-
enableRowAutoReactivity?: boolean
27-
enableColumnAutoReactivity?: boolean
28-
enableHeaderAutoReactivity?: boolean
31+
reactivity?: Partial<AngularReactivityFlags>
2932
}
3033

3134
interface Table_AngularReactivity<
3235
TFeatures extends TableFeatures,
3336
TData extends RowData,
3437
> {
35-
_rootNotifier?: Signal<Table<TFeatures, TData>>
36-
_setRootNotifier?: (signal: Signal<Table<TFeatures, TData>>) => void
38+
get: Signal<Table<TFeatures, TData>>
39+
_rootNotifier: Signal<Table<TFeatures, TData>>
40+
_setRootNotifier: (signal: Signal<Table<TFeatures, TData>>) => void
3741
}
3842

3943
interface AngularReactivityFeatureConstructors<
@@ -51,17 +55,16 @@ export function constructAngularReactivityFeature<
5155
return {
5256
getDefaultTableOptions(table) {
5357
return {
54-
enableExperimentalReactivity: false,
55-
enableHeaderAutoReactivity: true,
56-
enableColumnAutoReactivity: true,
57-
enableRowAutoReactivity: true,
58-
enableCellAutoReactivity: false,
58+
reactivity: {
59+
header: true,
60+
column: true,
61+
row: true,
62+
cell: true,
63+
table: true,
64+
},
5965
}
6066
},
6167
constructTableAPIs: (table) => {
62-
if (!table.options.enableExperimentalReactivity) {
63-
return
64-
}
6568
const rootNotifier = signal<Signal<any> | null>(null)
6669

6770
table._rootNotifier = computed(() => rootNotifier()?.(), {
@@ -72,29 +75,26 @@ export function constructAngularReactivityFeature<
7275
rootNotifier.set(notifier)
7376
}
7477

75-
setReactiveProps(table._rootNotifier!, table, {
78+
table.get = computed(() => rootNotifier()?.(), {
79+
equal: () => false,
80+
}) as any
81+
82+
setReactiveProps(table._rootNotifier, table, {
7683
skipProperty: skipBaseProperties,
7784
})
7885
},
7986

8087
constructCellAPIs(cell) {
81-
if (
82-
!cell._table.options.enableCellAutoReactivity ||
83-
!cell._table._rootNotifier
84-
) {
88+
if (cell._table.options.reactivity?.cell === false) {
8589
return
8690
}
87-
8891
setReactiveProps(cell._table._rootNotifier, cell, {
8992
skipProperty: skipBaseProperties,
9093
})
9194
},
9295

9396
constructColumnAPIs(column) {
94-
if (
95-
!column._table.options.enableColumnAutoReactivity ||
96-
!column._table._rootNotifier
97-
) {
97+
if (column._table.options.reactivity?.column === false) {
9898
return
9999
}
100100
setReactiveProps(column._table._rootNotifier, column, {
@@ -103,10 +103,7 @@ export function constructAngularReactivityFeature<
103103
},
104104

105105
constructHeaderAPIs(header) {
106-
if (
107-
!header._table.options.enableHeaderAutoReactivity ||
108-
!header._table._rootNotifier
109-
) {
106+
if (header._table.options.reactivity?.header === false) {
110107
return
111108
}
112109
setReactiveProps(header._table._rootNotifier, header, {
@@ -115,10 +112,7 @@ export function constructAngularReactivityFeature<
115112
},
116113

117114
constructRowAPIs(row) {
118-
if (
119-
!row._table.options.enableRowAutoReactivity ||
120-
!row._table._rootNotifier
121-
) {
115+
if (row._table.options.reactivity?.row === false) {
122116
return
123117
}
124118
setReactiveProps(row._table._rootNotifier, row, {
@@ -131,7 +125,23 @@ export function constructAngularReactivityFeature<
131125
export const angularReactivityFeature = constructAngularReactivityFeature()
132126

133127
function skipBaseProperties(property: string): boolean {
134-
return property.endsWith('Handler') || !property.startsWith('get')
128+
return (
129+
// equal `getContext`
130+
property === 'getContext' ||
131+
// start with `_`
132+
property[0] === '_' ||
133+
// start with `get`
134+
!(property[0] === 'g' && property[1] === 'e' && property[2] === 't') ||
135+
// ends with `Handler`
136+
(property.length >= 7 &&
137+
property[property.length - 7] === 'H' &&
138+
property[property.length - 6] === 'a' &&
139+
property[property.length - 5] === 'n' &&
140+
property[property.length - 4] === 'd' &&
141+
property[property.length - 3] === 'l' &&
142+
property[property.length - 2] === 'e' &&
143+
property[property.length - 1] === 'r')
144+
)
135145
}
136146

137147
export function setReactiveProps(
@@ -144,39 +154,17 @@ export function setReactiveProps(
144154
const { skipProperty } = options
145155
for (const property in obj) {
146156
const value = obj[property]
147-
if (typeof value !== 'function') {
148-
continue
149-
}
150-
if (skipProperty(property)) {
157+
if (
158+
isSignal(value) ||
159+
typeof value !== 'function' ||
160+
skipProperty(property)
161+
) {
151162
continue
152163
}
153-
Object.defineProperty(obj, property, {
154-
enumerable: true,
155-
configurable: false,
156-
value: toComputed(notifier, value, property),
164+
defineLazyComputedProperty(notifier, {
165+
valueFn: value,
166+
property,
167+
originalObject: obj,
157168
})
158-
// return;
159-
//
160-
// let _computed: any
161-
// Object.defineProperty(obj, property, {
162-
// enumerable: true,
163-
// configurable: true,
164-
// get(): any {
165-
// if (_computed) {
166-
// Object.defineProperty(this, property, {
167-
// value: _computed,
168-
// writable: false, // Make it immutable (optional)
169-
// configurable: false,
170-
// })
171-
// return _computed
172-
// }
173-
// _computed = toComputed(notifier, value, property)
174-
// Object.defineProperty(this, property, {
175-
// value: _computed,
176-
// configurable: false,
177-
// })
178-
// return _computed
179-
// },
180-
// })
181169
}
182170
}

packages/angular-table/src/injectTable.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
import { computed, signal } from '@angular/core'
2+
import type {
3+
RowData,
4+
Table,
5+
TableFeatures,
6+
TableOptions,
7+
TableState,
8+
} from '@tanstack/table-core'
29
import {
310
constructTable,
411
coreFeatures,
512
getInitialTableState,
613
isFunction,
714
} from '@tanstack/table-core'
815
import { lazyInit } from './lazy-signal-initializer'
9-
import { proxifyTable } from './proxy'
1016
import { angularReactivityFeature } from './angularReactivityFeature'
11-
import type {
12-
RowData,
13-
Table,
14-
TableFeatures,
15-
TableOptions,
16-
TableState,
17-
} from '@tanstack/table-core'
18-
import type { Signal } from '@angular/core'
1917

2018
export function injectTable<
2119
TFeatures extends TableFeatures,
2220
TData extends RowData,
23-
>(
24-
options: () => TableOptions<TFeatures, TData>,
25-
): Table<TFeatures, TData> & Signal<Table<TFeatures, TData>> {
21+
>(options: () => TableOptions<TFeatures, TData>): Table<TFeatures, TData> {
2622
return lazyInit(() => {
2723
const features = () => {
2824
return {
@@ -78,9 +74,9 @@ export function injectTable<
7874
},
7975
)
8076

81-
table._setRootNotifier?.(tableSignal as any)
77+
table._setRootNotifier(tableSignal as any)
8278

8379
// proxify Table instance to provide ability for consumer to listen to any table state changes
84-
return proxifyTable(tableSignal)
80+
return table as any
8581
})
8682
}

0 commit comments

Comments
 (0)