Skip to content

Commit

Permalink
memo refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Oct 17, 2024
1 parent 9578c73 commit 401d69b
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 456 deletions.
16 changes: 6 additions & 10 deletions examples/react/filters-faceted/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ import {
flexRender,
sortingFns,
tableFeatures,
processingFns,
useTable,
} from '@tanstack/react-table'
import { makeData } from './makeData'
import type { ProcessingFns } from '../../../../packages/table-core/dist/esm/types/ProcessingFns'
import type {
CellData,
Column,
Expand All @@ -38,11 +36,6 @@ const _features = tableFeatures({
RowSorting,
})

const _processingFns = processingFns(_features, {
filterFns,
sortingFns,
})

declare module '@tanstack/react-table' {
// allows us to define custom properties for our columns
interface ColumnMeta<
Expand Down Expand Up @@ -109,7 +102,10 @@ function App() {

const table = useTable({
_features,
_processingFns,
_processingFns: {
filterFns,
sortingFns,
},
_rowModels: {
Filtered: createFilteredRowModel(), // client-side filtering
Paginated: createPaginatedRowModel(),
Expand Down Expand Up @@ -174,7 +170,7 @@ function App() {
{table.getRowModel().rows.map((row) => {
return (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => {
{row.getAllCells().map((cell) => {
return (
<td key={cell.id}>
{flexRender(
Expand Down Expand Up @@ -269,7 +265,7 @@ function App() {
)
}

function Filter({ column }: { column: Column<any, Person, unknown> }) {
function Filter({ column }: { column: Column<typeof _features, Person> }) {
const { filterVariant } = column.columnDef.meta ?? {}

const columnFilterValue = column.getFilterValue()
Expand Down
6 changes: 5 additions & 1 deletion packages/table-core/src/core/table/Tables.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export interface Table_CoreProperties<
* @link [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
_processingFns: ProcessingFns<TFeatures, TData>
/**
* The row models that are enabled for the table.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/table#_rowmodels)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
_rowModels: CachedRowModels<TFeatures, TData>
/**
* This is the resolved initial state of the table.
Expand All @@ -137,7 +142,6 @@ export interface Table_Table<
TFeatures extends TableFeatures,
TData extends RowData,
> extends Table_CoreProperties<TFeatures, TData> {
_queue: (cb: () => void) => void
/**
* Returns the core row model before any processing has been applied.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/table#getcorerowmodel)
Expand Down
26 changes: 0 additions & 26 deletions packages/table-core/src/core/table/constructTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,6 @@ export function constructTable<

Object.assign(table, coreInstance)

const queued: Array<() => void> = []
let queuedTimeout = false

table._queue = (cb) => {
queued.push(cb)

if (!queuedTimeout) {
queuedTimeout = true

// Schedule a microtask to run the queued callbacks after
// the current call stack (render, etc) has finished.
Promise.resolve()
.then(() => {
while (queued.length) {
queued.shift()!()
}
queuedTimeout = false
})
.catch((error) =>
setTimeout(() => {
throw error
}),
)
}
}

for (const feature of featuresList) {
feature.constructTable?.(table)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/core/table/createCoreRowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function createCoreRowModel<
return (table: Table<TFeatures, TData>) =>
tableMemo({
debug: isDev && (table.options.debugAll ?? table.options.debugTable),
fnName: 'createCoreRowModel',
fnName: 'table.createCoreRowModel',
memoDeps: () => [table.options.data],
fn: (data) => _createCoreRowModel(table, data),
onAfterUpdate: () => table_autoResetPageIndex(table),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function createFacetedMinMaxValues<
return (table, columnId) =>
tableMemo({
debug: isDev && (table.options.debugAll ?? table.options.debugTable),
fnName: 'createFacetedMinMaxValues',
fnName: 'table.createFacetedMinMaxValues',
memoDeps: () => [
column_getFacetedRowModel(table.getColumn(columnId), table)(),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMemoOptions, memo } from '../../utils'
import { isDev, tableMemo } from '../../utils'
import { row_getUniqueValues } from '../../core/rows/Rows.utils'
import { column_getFacetedRowModel } from './ColumnFaceting.utils'
import type { RowData } from '../../types/type-utils'
Expand All @@ -13,34 +13,43 @@ export function createFacetedUniqueValues<
columnId: string,
) => () => Map<any, number> {
return (table, columnId) =>
memo(
() => [column_getFacetedRowModel(table.getColumn(columnId), table)()],
(facetedRowModel) => {
if (!facetedRowModel) return new Map()
tableMemo({
debug: isDev && (table.options.debugAll ?? table.options.debugTable),
fnName: 'table.createFacetedUniqueValues',
memoDeps: () => [
column_getFacetedRowModel(table.getColumn(columnId), table)(),
],
fn: (facetedRowModel) =>
_createFacetedUniqueValues(table, columnId, facetedRowModel),
})
}

function _createFacetedUniqueValues<
TFeatures extends TableFeatures,
TData extends RowData,
>(
table: Table<TFeatures, TData>,
columnId: string,
facetedRowModel?: ReturnType<ReturnType<typeof column_getFacetedRowModel>>,
): Map<any, number> {
if (!facetedRowModel) return new Map()

const facetedUniqueValues = new Map<any, number>()
const facetedUniqueValues = new Map<any, number>()

for (const row of facetedRowModel.flatRows) {
const values = row_getUniqueValues(row, table, columnId)
for (const row of facetedRowModel.flatRows) {
const values = row_getUniqueValues(row, table, columnId)

for (const value of values) {
if (facetedUniqueValues.has(value)) {
facetedUniqueValues.set(
value,
(facetedUniqueValues.get(value) ?? 0) + 1,
)
} else {
facetedUniqueValues.set(value, 1)
}
}
}
for (const value of values) {
if (facetedUniqueValues.has(value)) {
facetedUniqueValues.set(
value,
(facetedUniqueValues.get(value) ?? 0) + 1,
)
} else {
facetedUniqueValues.set(value, 1)
}
}
}

return facetedUniqueValues
},
getMemoOptions(
table.options,
'debugTable',
`getFacetedUniqueValues_${columnId}`,
),
)
return facetedUniqueValues
}
Loading

0 comments on commit 401d69b

Please sign in to comment.