Skip to content

Commit e8ebb4a

Browse files
committed
fix(angular): revisit adapter to support lazy initialization
1 parent c4bb4a5 commit e8ebb4a

3 files changed

Lines changed: 35 additions & 32 deletions

File tree

‎packages/angular-table/src/injectTable.ts‎

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -98,42 +98,47 @@ export function injectTable<
9898
const injector = inject(Injector)
9999
const ngZone = inject(NgZone)
100100
const options = computed(() => _options())
101+
const coreReactivityFeature = angularReactivity(injector)
102+
103+
let table: Table<TFeatures, TData>
104+
injector.get(DestroyRef).onDestroy(() => {
105+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
106+
if (table) {
107+
table._reactivity.unmount?.()
108+
}
109+
})
110+
111+
let previousOptions: TableOptions<TFeatures, TData>
112+
effect(
113+
() => {
114+
const currentOptions = options()
115+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
116+
if (!table) {
117+
return
118+
}
119+
if (previousOptions === currentOptions) {
120+
return
121+
}
122+
untracked(() =>
123+
table.setOptions((previous) => ({
124+
...previous,
125+
...currentOptions,
126+
})),
127+
)
128+
previousOptions = currentOptions
129+
},
130+
{ injector, debugName: 'tableOptionsUpdate' },
131+
)
101132

102133
return ngZone.runOutsideAngular(() =>
103134
lazyInit(() => {
104-
// Explicit type arguments skip generic inference from the spread object
105-
// (a type-check hot spot); the spread only adds the angular reactivity
106-
// binding to `features`.
107-
const table = constructTable<TFeatures, TData>({
135+
table = constructTable<TFeatures, TData>({
108136
...options(),
109137
features: {
110-
coreReactivityFeature: angularReactivity(injector),
138+
coreReactivityFeature,
111139
...options().features,
112140
},
113141
})
114-
115-
injector.get(DestroyRef).onDestroy(() => {
116-
table._reactivity.unmount?.()
117-
})
118-
119-
let previousOptions = options()
120-
effect(
121-
() => {
122-
const currentOptions = options()
123-
if (previousOptions === currentOptions) {
124-
return
125-
}
126-
untracked(() =>
127-
table.setOptions((previous) => ({
128-
...previous,
129-
...currentOptions,
130-
})),
131-
)
132-
previousOptions = currentOptions
133-
},
134-
{ injector, debugName: 'tableOptionsUpdate' },
135-
)
136-
137142
return table
138143
}),
139144
)

‎packages/angular-table/src/lazySignalInitializer.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ export function lazyInit<T extends object>(initializer: () => T): T {
1313
}
1414
}
1515

16-
queueMicrotask(() => initializeObject())
17-
1816
const table = () => {}
1917

2018
return new Proxy<T>(table as T, {

‎packages/angular-table/tests/lazy-init.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { flushQueue, setFixtureSignalInputs } from './test-utils'
1313
import type { WritableSignal } from '@angular/core'
1414

1515
describe('lazyInit', () => {
16-
test('should init lazily in next tick when not accessing manually', async () => {
16+
test('should not init lazily in next tick when not accessing manually', async () => {
1717
const mockFn = vi.fn()
1818

1919
TestBed.runInInjectionContext(() => {
@@ -29,7 +29,7 @@ describe('lazyInit', () => {
2929

3030
await new Promise(setImmediate)
3131

32-
expect(mockFn).toHaveBeenCalled()
32+
expect(mockFn).not.toHaveBeenCalled()
3333
})
3434

3535
test('should init eagerly accessing manually', () => {

0 commit comments

Comments
 (0)