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'
4
3
import type {
5
4
RowData ,
6
5
Table ,
@@ -20,20 +19,25 @@ declare module '@tanstack/table-core' {
20
19
> extends Table_AngularReactivity < TFeatures , TData > { }
21
20
}
22
21
22
+ export interface AngularReactivityFlags {
23
+ header : boolean
24
+ column : boolean
25
+ row : boolean
26
+ cell : boolean
27
+ table : boolean
28
+ }
29
+
23
30
interface TableOptions_AngularReactivity {
24
- enableExperimentalReactivity ?: boolean
25
- enableCellAutoReactivity ?: boolean
26
- enableRowAutoReactivity ?: boolean
27
- enableColumnAutoReactivity ?: boolean
28
- enableHeaderAutoReactivity ?: boolean
31
+ reactivity ?: Partial < AngularReactivityFlags >
29
32
}
30
33
31
34
interface Table_AngularReactivity <
32
35
TFeatures extends TableFeatures ,
33
36
TData extends RowData ,
34
37
> {
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
37
41
}
38
42
39
43
interface AngularReactivityFeatureConstructors <
@@ -51,17 +55,16 @@ export function constructAngularReactivityFeature<
51
55
return {
52
56
getDefaultTableOptions ( table ) {
53
57
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
+ } ,
59
65
}
60
66
} ,
61
67
constructTableAPIs : ( table ) => {
62
- if ( ! table . options . enableExperimentalReactivity ) {
63
- return
64
- }
65
68
const rootNotifier = signal < Signal < any > | null > ( null )
66
69
67
70
table . _rootNotifier = computed ( ( ) => rootNotifier ( ) ?.( ) , {
@@ -72,29 +75,26 @@ export function constructAngularReactivityFeature<
72
75
rootNotifier . set ( notifier )
73
76
}
74
77
75
- setReactiveProps ( table . _rootNotifier ! , table , {
78
+ table . get = computed ( ( ) => rootNotifier ( ) ?.( ) , {
79
+ equal : ( ) => false ,
80
+ } ) as any
81
+
82
+ setReactiveProps ( table . _rootNotifier , table , {
76
83
skipProperty : skipBaseProperties ,
77
84
} )
78
85
} ,
79
86
80
87
constructCellAPIs ( cell ) {
81
- if (
82
- ! cell . _table . options . enableCellAutoReactivity ||
83
- ! cell . _table . _rootNotifier
84
- ) {
88
+ if ( cell . _table . options . reactivity ?. cell === false ) {
85
89
return
86
90
}
87
-
88
91
setReactiveProps ( cell . _table . _rootNotifier , cell , {
89
92
skipProperty : skipBaseProperties ,
90
93
} )
91
94
} ,
92
95
93
96
constructColumnAPIs ( column ) {
94
- if (
95
- ! column . _table . options . enableColumnAutoReactivity ||
96
- ! column . _table . _rootNotifier
97
- ) {
97
+ if ( column . _table . options . reactivity ?. column === false ) {
98
98
return
99
99
}
100
100
setReactiveProps ( column . _table . _rootNotifier , column , {
@@ -103,10 +103,7 @@ export function constructAngularReactivityFeature<
103
103
} ,
104
104
105
105
constructHeaderAPIs ( header ) {
106
- if (
107
- ! header . _table . options . enableHeaderAutoReactivity ||
108
- ! header . _table . _rootNotifier
109
- ) {
106
+ if ( header . _table . options . reactivity ?. header === false ) {
110
107
return
111
108
}
112
109
setReactiveProps ( header . _table . _rootNotifier , header , {
@@ -115,10 +112,7 @@ export function constructAngularReactivityFeature<
115
112
} ,
116
113
117
114
constructRowAPIs ( row ) {
118
- if (
119
- ! row . _table . options . enableRowAutoReactivity ||
120
- ! row . _table . _rootNotifier
121
- ) {
115
+ if ( row . _table . options . reactivity ?. row === false ) {
122
116
return
123
117
}
124
118
setReactiveProps ( row . _table . _rootNotifier , row , {
@@ -131,7 +125,23 @@ export function constructAngularReactivityFeature<
131
125
export const angularReactivityFeature = constructAngularReactivityFeature ( )
132
126
133
127
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
+ )
135
145
}
136
146
137
147
export function setReactiveProps (
@@ -144,39 +154,17 @@ export function setReactiveProps(
144
154
const { skipProperty } = options
145
155
for ( const property in obj ) {
146
156
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
+ ) {
151
162
continue
152
163
}
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 ,
157
168
} )
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
- // })
181
169
}
182
170
}
0 commit comments