-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathinjectTanStackTableDevtools.ts
More file actions
77 lines (70 loc) · 1.94 KB
/
injectTanStackTableDevtools.ts
File metadata and controls
77 lines (70 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
removeTableDevtoolsTarget,
upsertTableDevtoolsTarget,
} from '@tanstack/table-devtools'
import {
APP_ID,
DestroyRef,
Injectable,
Injector,
assertInInjectionContext,
effect,
inject,
} from '@angular/core'
import type { RowData, Table, TableFeatures } from '@tanstack/table-core'
export interface InjectTanStackTableDevtoolsOptions {
enabled?: () => boolean
injector?: Injector
}
function normalizeName(name?: string) {
const trimmedName = name?.trim()
return trimmedName ? trimmedName : undefined
}
let autoId = 0
function generateId(): string {
const appId = inject(APP_ID)
return `tanstacktable-${appId}_${autoId++}${Date.now().toString(36)}`
}
export function injectTanStackTableDevtools<
TFeatures extends TableFeatures = TableFeatures,
TData extends RowData = RowData,
>(
table: () => Table<TFeatures, TData> | undefined,
name?: string,
options?: InjectTanStackTableDevtoolsOptions,
): void {
const registrationId = generateId()
const enabled = () => options?.enabled?.() ?? true
assertInInjectionContext(injectTanStackTableDevtools)
const injector = options?.injector ?? inject(Injector)
const destroyRef = inject(DestroyRef)
effect(
(onCleanup) => {
const enabledValue = enabled()
const tableValue = table()
if (!enabledValue || !tableValue) {
removeTableDevtoolsTarget(registrationId)
}
upsertTableDevtoolsTarget({
id: registrationId,
table: tableValue,
name: normalizeName(name),
})
onCleanup(() => {
removeTableDevtoolsTarget(registrationId)
})
},
{ injector },
)
destroyRef.onDestroy(() => {
removeTableDevtoolsTarget(registrationId)
})
}
export function injectTanStackTableDevtoolsNoOp<
TFeatures extends TableFeatures = TableFeatures,
TData extends RowData = RowData,
>(
_table: Table<TFeatures, TData> | undefined,
_name?: string,
_options?: InjectTanStackTableDevtoolsOptions,
): void {}