-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(angular-table): added injector optional parameter for more flexibility #5525
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
db23cf4
feat(angular-table): improve adapter implementation (TanStack#5524)
merto20 363bac4
feat(angular-table): Added proxifyTable back
merto20 07c8440
feat(angular-table): adding back notifier signal for table changed
merto20 b16a194
feat(angular-table): Improve logic in setting table options
merto20 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,72 @@ | ||
import { computed, signal } from '@angular/core' | ||
import { | ||
computed, | ||
effect, | ||
inject, | ||
Injector, | ||
runInInjectionContext, | ||
type Signal, | ||
signal, | ||
untracked, | ||
} from '@angular/core' | ||
import { | ||
createTable, | ||
RowData, | ||
type Table, | ||
TableOptions, | ||
TableOptionsResolved, | ||
TableState, | ||
createTable, | ||
type Table, | ||
} from '@tanstack/table-core' | ||
import { proxifyTable } from './proxy' | ||
import { lazyInit } from './lazy-signal-initializer' | ||
import { proxifyTable } from './proxy' | ||
|
||
export * from '@tanstack/table-core' | ||
|
||
export { | ||
FlexRenderDirective, | ||
FlexRenderComponent, | ||
FlexRenderDirective, | ||
injectFlexRenderContext, | ||
} from './flex-render' | ||
|
||
export function createAngularTable<TData extends RowData>( | ||
options: () => TableOptions<TData> | ||
): Table<TData> & Signal<Table<TData>> { | ||
const injector = inject(Injector) | ||
): Table<TData> { | ||
return lazyInit(() => { | ||
const resolvedOptions = { | ||
state: {}, | ||
onStateChange: () => {}, | ||
renderFallbackValue: null, | ||
...options(), | ||
} | ||
|
||
return lazyInit(() => | ||
runInInjectionContext(injector, () => { | ||
const resolvedOptionsSignal = computed<TableOptionsResolved<TData>>( | ||
() => { | ||
return { | ||
state: {}, | ||
onStateChange: () => {}, | ||
renderFallbackValue: null, | ||
...options(), | ||
} | ||
} | ||
) | ||
const table = createTable(resolvedOptions) | ||
|
||
const notifier = signal([], { equal: () => false }) | ||
const table = createTable(untracked(resolvedOptionsSignal)) | ||
const state = signal(table.initialState) | ||
// By default, manage table state here using the table's initial state | ||
const state = signal<TableState>(table.initialState) | ||
|
||
function updateOptions() { | ||
const tableState = untracked(state) | ||
const resolvedOptions = untracked(resolvedOptionsSignal) | ||
untracked(() => { | ||
table.setOptions(prev => ({ | ||
...prev, | ||
...resolvedOptions, | ||
state: { ...tableState, ...resolvedOptions.state }, | ||
onStateChange: updater => { | ||
const value = | ||
updater instanceof Function ? updater(tableState) : updater | ||
state.set(value) | ||
resolvedOptions.onStateChange?.(updater) | ||
}, | ||
})) | ||
}) | ||
// Compose table options using computed. | ||
// This is to allow `tableSignal` to listen and set table option | ||
const updatedOptions = computed<TableOptionsResolved<TData>>(() => { | ||
// listen to table state changed | ||
const tableState = state() | ||
// listen to input options changed | ||
const tableOptions = options() | ||
return { | ||
...table.options, | ||
...resolvedOptions, | ||
...tableOptions, | ||
state: { ...tableState, ...tableOptions.state }, | ||
onStateChange: updater => { | ||
const value = | ||
updater instanceof Function ? updater(tableState) : updater | ||
state.set(value) | ||
resolvedOptions.onStateChange?.(updater) | ||
}, | ||
} | ||
}) | ||
|
||
updateOptions() | ||
|
||
let firstRender = true | ||
effect(() => { | ||
void [state(), resolvedOptionsSignal()] | ||
if (firstRender) { | ||
return (firstRender = false) | ||
} | ||
untracked(() => { | ||
updateOptions() | ||
notifier.set([]) | ||
}) | ||
}) | ||
|
||
const tableSignal = computed( | ||
() => { | ||
notifier() | ||
return table | ||
}, | ||
{ | ||
equal: () => false, | ||
} | ||
) | ||
// convert table instance to signal for proxify to listen to any table state and options changes | ||
const tableSignal = computed( | ||
() => { | ||
table.setOptions(updatedOptions()) | ||
return table | ||
}, | ||
{ | ||
equal: () => false, | ||
} | ||
) | ||
|
||
return proxifyTable(tableSignal) | ||
merto20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
) | ||
// proxify Table instance to provide ability for consumer to listen to any table state changes | ||
return proxifyTable(tableSignal) | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d go back with the previous implementation, updating the options inside a signal is theoretically wrong
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree. Doing side effects not related to value being returned by the computed signal is theoritically wrong. But updating the object before it will be return inside a computed is not. IMHO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to get rid of the
effect
from the start because of its high requirements. But was in doubt because I was not so sure if I'm doing it wrong to callupdateOptions
method inside a computed. Adding aupdatedOptions
signal simplifies that and giving more clarity that it is not wrong to update the Table object before it will be returned in the computed signal. CallingupdateOptions
functions inside the computed signal seems wrong because we cannot control if someone modifies that method and do something more than updating the table options.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks for the explaination