Skip to content

Commit 232c5ea

Browse files
committed
make feature constructor functions that can accept generics
1 parent 963b69c commit 232c5ea

27 files changed

Lines changed: 1036 additions & 822 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { computed, signal } from '@angular/core'
2+
import { toComputed } from './proxy'
3+
import type { Signal } from '@angular/core'
4+
import type {
5+
RowData,
6+
Table,
7+
TableFeature,
8+
TableFeatures,
9+
} from '@tanstack/table-core'
10+
11+
declare module '@tanstack/table-core' {
12+
interface TableOptions_Plugins<
13+
TFeatures extends TableFeatures,
14+
TData extends RowData,
15+
> extends TableOptions_AngularReactivity {}
16+
17+
interface Table_Plugins<
18+
TFeatures extends TableFeatures,
19+
TData extends RowData,
20+
> extends Table_AngularReactivity<TFeatures, TData> {}
21+
}
22+
23+
interface TableOptions_AngularReactivity {
24+
enableExperimentalReactivity?: boolean
25+
}
26+
27+
interface Table_AngularReactivity<
28+
TFeatures extends TableFeatures,
29+
TData extends RowData,
30+
> {
31+
_rootNotifier?: Signal<Table<TFeatures, TData>>
32+
_setRootNotifier?: (signal: Signal<Table<TFeatures, TData>>) => void
33+
}
34+
35+
interface AngularReactivityFeatureConstructors<
36+
TFeatures extends TableFeatures,
37+
TData extends RowData,
38+
> {
39+
TableOptions: TableOptions_AngularReactivity
40+
Table: Table_AngularReactivity<TFeatures, TData>
41+
}
42+
43+
export function constructAngularReactivityFeature<
44+
TFeatures extends TableFeatures,
45+
TData extends RowData,
46+
>(): TableFeature<AngularReactivityFeatureConstructors<TFeatures, TData>> {
47+
return {
48+
getDefaultTableOptions(table) {
49+
return { enableExperimentalReactivity: false }
50+
},
51+
constructTableAPIs: (table) => {
52+
if (!table.options.enableExperimentalReactivity) {
53+
return
54+
}
55+
const rootNotifier = signal<Signal<any> | null>(null)
56+
57+
table._rootNotifier = computed(() => rootNotifier()?.(), {
58+
equal: () => false,
59+
}) as any
60+
61+
table._setRootNotifier = (notifier) => {
62+
rootNotifier.set(notifier)
63+
}
64+
65+
setReactiveProps(table._rootNotifier!, table, {
66+
skipProperty: skipBaseProperties,
67+
})
68+
},
69+
70+
constructCellAPIs(cell) {
71+
if (
72+
!cell._table.options.enableExperimentalReactivity ||
73+
!cell._table._rootNotifier
74+
) {
75+
return
76+
}
77+
setReactiveProps(cell._table._rootNotifier, cell, {
78+
skipProperty: skipBaseProperties,
79+
})
80+
},
81+
82+
constructColumnAPIs(column) {
83+
if (
84+
!column._table.options.enableExperimentalReactivity ||
85+
!column._table._rootNotifier
86+
) {
87+
return
88+
}
89+
setReactiveProps(column._table._rootNotifier, column, {
90+
skipProperty: skipBaseProperties,
91+
})
92+
},
93+
94+
constructHeaderAPIs(header) {
95+
if (
96+
!header._table.options.enableExperimentalReactivity ||
97+
!header._table._rootNotifier
98+
) {
99+
return
100+
}
101+
setReactiveProps(header._table._rootNotifier, header, {
102+
skipProperty: skipBaseProperties,
103+
})
104+
},
105+
106+
constructRowAPIs(row) {
107+
if (
108+
!row._table.options.enableExperimentalReactivity ||
109+
!row._table._rootNotifier
110+
) {
111+
return
112+
}
113+
setReactiveProps(row._table._rootNotifier, row, {
114+
skipProperty: skipBaseProperties,
115+
})
116+
},
117+
}
118+
}
119+
120+
export const angularReactivityFeature = constructAngularReactivityFeature()
121+
122+
function skipBaseProperties(property: string): boolean {
123+
return property.endsWith('Handler') || !property.startsWith('get')
124+
}
125+
126+
export function setReactiveProps(
127+
notifier: Signal<Table<any, any>>,
128+
obj: { [key: string]: any },
129+
options: {
130+
skipProperty: (property: string) => boolean
131+
},
132+
) {
133+
const { skipProperty } = options
134+
for (const property in obj) {
135+
const value = obj[property]
136+
if (typeof value !== 'function') {
137+
continue
138+
}
139+
if (skipProperty(property)) {
140+
continue
141+
}
142+
Object.defineProperty(obj, property, {
143+
enumerable: true,
144+
configurable: false,
145+
value: toComputed(notifier, value, property),
146+
})
147+
}
148+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export * from '@tanstack/table-core'
22

3+
export * from './angularReactivityFeature'
4+
export * from './createTableHelper'
35
export * from './flex-render'
4-
export * from './proxy'
5-
export * from './lazy-signal-initializer'
66
export * from './injectTable'
7-
export * from './createTableHelper'
8-
export * from './reactivity'
7+
export * from './lazy-signal-initializer'
8+
export * from './proxy'

packages/angular-table/src/injectTable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@tanstack/table-core'
88
import { lazyInit } from './lazy-signal-initializer'
99
import { proxifyTable } from './proxy'
10-
import { reactivityFeature } from './reactivity'
10+
import { angularReactivityFeature } from './angularReactivityFeature'
1111
import type {
1212
RowData,
1313
Table,
@@ -28,7 +28,7 @@ export function injectTable<
2828
return {
2929
...coreFeatures,
3030
...options()._features,
31-
reactivityFeature,
31+
...angularReactivityFeature,
3232
}
3333
}
3434

packages/angular-table/src/reactivity.ts

Lines changed: 0 additions & 138 deletions
This file was deleted.

packages/table-core/src/core/cells/coreCellsFeature.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,44 @@ import {
44
cell_getValue,
55
cell_renderValue,
66
} from './coreCellsFeature.utils'
7-
import type { TableFeature } from '../../types/TableFeatures'
7+
import type { RowData } from '../../types/type-utils'
8+
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
9+
// import type { Cell_Cell, TableOptions_Cell } from './coreCellsFeature.types'
810

9-
export const coreCellsFeature: TableFeature<{
10-
// Cell: Cell_Cell<TableFeatures, RowData, CellData>
11+
interface CoreCellsFeatureConstructors<
12+
TFeatures extends TableFeatures,
13+
TData extends RowData,
14+
> {
15+
// Cell: Cell_Cell<TableFeatures, RowData>
1116
// TableOptions: TableOptions_Cell
12-
}> = {
13-
feature: 'coreCellsFeature',
17+
}
1418

15-
constructCellAPIs: (cell) => {
16-
assignAPIs('coreCellsFeature', cell, [
17-
{
18-
fn: () => cell_getValue(cell),
19-
fnName: 'cell_getValue',
20-
},
21-
{
22-
fn: () => cell_renderValue(cell),
23-
fnName: 'cell_renderValue',
24-
},
25-
{
26-
fn: () => cell_getContext(cell),
27-
fnName: 'cell_getContext',
28-
memoDeps: () => [cell],
29-
},
30-
])
31-
},
19+
export function constructCoreCellsFeature<
20+
TFeatures extends TableFeatures,
21+
TData extends RowData,
22+
>(): TableFeature<CoreCellsFeatureConstructors<TFeatures, TData>> {
23+
return {
24+
constructCellAPIs: (cell) => {
25+
assignAPIs('coreCellsFeature', cell, [
26+
{
27+
fn: () => cell_getValue(cell),
28+
fnName: 'cell_getValue',
29+
},
30+
{
31+
fn: () => cell_renderValue(cell),
32+
fnName: 'cell_renderValue',
33+
},
34+
{
35+
fn: () => cell_getContext(cell),
36+
fnName: 'cell_getContext',
37+
memoDeps: () => [cell],
38+
},
39+
])
40+
},
41+
}
3242
}
43+
44+
/**
45+
* The Core Cells feature provides the core cell functionality.
46+
*/
47+
export const coreCellsFeature = constructCoreCellsFeature()

0 commit comments

Comments
 (0)