Skip to content

Commit 047dd88

Browse files
committed
fix(composables): handle dependency changes in composables
1 parent fd3cc64 commit 047dd88

21 files changed

+58
-2
lines changed

packages/public/vue-tinybase/scripts/generate/composables/astHandlers/handleStoreInterfaceDeclaration.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@ export async function handleStoreInterfaceDeclaration(path: NodePath<t.TSInterfa
5555
)
5656

5757
const file = t.file(
58-
addImports(path, statements, parameterNames.length > 0, false, true, true, `on${fieldName}Change`),
58+
addImports(
59+
path,
60+
statements,
61+
parameterNames.length > 0,
62+
false,
63+
true,
64+
true,
65+
parameterNames.length > 0,
66+
`on${fieldName}Change`,
67+
),
5968
)
6069
for (const rule of rulesToDisable) {
6170
t.addComment(file, 'leading', ` eslint-disable ${rule} `, false)

packages/public/vue-tinybase/scripts/generate/composables/generators/generateComposableFunction.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ export function generateComposableFunction(
7474
),
7575
])
7676

77+
const watchStatement = t.expressionStatement(
78+
t.callExpression(t.identifier('watch'), [
79+
t.arrayExpression(parameterNames.map(parameterName => t.identifier(`${parameterName}Ref`))),
80+
t.identifier('getDataFromStore'),
81+
]),
82+
)
83+
7784
const returnStatement = t.returnStatement(
7885
t.objectExpression([t.objectProperty(t.identifier('data'), t.identifier('data'))]),
7986
)
@@ -84,8 +91,11 @@ export function generateComposableFunction(
8491
getDataFromStoreStatement,
8592
eventListenerCallStatement,
8693
dataComputedStatement,
87-
returnStatement,
8894
)
95+
if (parameterNames.length > 0) {
96+
statements.push(watchStatement)
97+
}
98+
statements.push(returnStatement)
8999

90100
const functionDeclaration = t.functionDeclaration(
91101
t.identifier(`use${fieldName}`),

packages/public/vue-tinybase/scripts/generate/utils/importsHandler.ts

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function addImports(
2424
isUseListenerUsed = true,
2525
isShallowRefUsed = false,
2626
isComputedUsed = false,
27+
isWatchUsed = false,
2728
eventListenerToImport?: string,
2829
) {
2930
const program = t.program([...statements])
@@ -41,6 +42,15 @@ export function addImports(
4142
)
4243
}
4344

45+
if (isWatchUsed) {
46+
program.body.unshift(
47+
t.importDeclaration(
48+
[t.importSpecifier(t.identifier('watch'), t.identifier('watch'))],
49+
t.stringLiteral('@vue/runtime-core'),
50+
),
51+
)
52+
}
53+
4454
if (isUseListenerUsed) {
4555
program.body.unshift(
4656
t.importDeclaration(

packages/public/vue-tinybase/src/generated/composables/custom-store/useCell.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { onCellChange } from '../../events/custom-store/onCellChange.js'
910

@@ -48,6 +49,7 @@ export function useCell<
4849
}
4950
return localRef.value
5051
})
52+
watch([tableIdRef, rowIdRef, cellIdRef], getDataFromStore)
5153
return {
5254
data: data,
5355
}

packages/public/vue-tinybase/src/generated/composables/custom-store/useCellIds.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { onCellIdsChange } from '../../events/custom-store/onCellIdsChange.js'
910

@@ -39,6 +40,7 @@ export function useCellIds<
3940
}
4041
return localRef.value
4142
})
43+
watch([tableIdRef, rowIdRef], getDataFromStore)
4244
return {
4345
data: data,
4446
}

packages/public/vue-tinybase/src/generated/composables/custom-store/useRow.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { onRowChange } from '../../events/custom-store/onRowChange.js'
910

@@ -41,6 +42,7 @@ export function useRow<Store extends AnyStore, TableId extends TableIdFromSchema
4142
}
4243
return localRef.value
4344
})
45+
watch([tableIdRef, rowIdRef], getDataFromStore)
4446
return {
4547
data: data,
4648
}

packages/public/vue-tinybase/src/generated/composables/custom-store/useRowCount.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { onRowCountChange } from '../../events/custom-store/onRowCountChange.js'
910

@@ -34,6 +35,7 @@ export function useRowCount<Store extends AnyStore>(
3435
}
3536
return localRef.value
3637
})
38+
watch([tableIdRef], getDataFromStore)
3739
return {
3840
data: data,
3941
}

packages/public/vue-tinybase/src/generated/composables/custom-store/useRowIds.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { onRowIdsChange } from '../../events/custom-store/onRowIdsChange.js'
910

@@ -35,6 +36,7 @@ export function useRowIds<Store extends AnyStore>(
3536
}
3637
return localRef.value
3738
})
39+
watch([tableIdRef], getDataFromStore)
3840
return {
3941
data: data,
4042
}

packages/public/vue-tinybase/src/generated/composables/custom-store/useSortedRowIds.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { onSortedRowIdsChange } from '../../events/custom-store/onSortedRowIdsChange.js'
910

@@ -64,6 +65,7 @@ export function useSortedRowIds<
6465
}
6566
return localRef.value
6667
})
68+
watch([tableIdRef, cellIdRef, descendingRef, offsetRef, limitRef], getDataFromStore)
6769
return {
6870
data: data,
6971
}

packages/public/vue-tinybase/src/generated/composables/custom-store/useTable.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { onTableChange } from '../../events/custom-store/onTableChange.js'
910

@@ -38,6 +39,7 @@ export function useTable<Store extends AnyStore, TableId extends TableIdFromSche
3839
}
3940
return localRef.value
4041
})
42+
watch([tableIdRef], getDataFromStore)
4143
return {
4244
data: data,
4345
}

packages/public/vue-tinybase/src/generated/composables/custom-store/useTableCellIds.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { onTableCellIdsChange } from '../../events/custom-store/onTableCellIdsChange.js'
910

@@ -37,6 +38,7 @@ export function useTableCellIds<
3738
}
3839
return localRef.value
3940
})
41+
watch([tableIdRef], getDataFromStore)
4042
return {
4143
data: data,
4244
}

packages/public/vue-tinybase/src/generated/composables/custom-store/useValue.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { onValueChange } from '../../events/custom-store/onValueChange.js'
910

@@ -37,6 +38,7 @@ export function useValue<Store extends AnyStore, ValueId extends ValueIdFromSche
3738
}
3839
return localRef.value
3940
})
41+
watch([valueIdRef], getDataFromStore)
4042
return {
4143
data: data,
4244
}

packages/public/vue-tinybase/src/generated/composables/default-store/useCell.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { useStore } from '../../../composables/useStore.js'
910
import { onCellChange } from '../../events/custom-store/onCellChange.js'

packages/public/vue-tinybase/src/generated/composables/default-store/useCellIds.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { useStore } from '../../../composables/useStore.js'
910
import { onCellIdsChange } from '../../events/custom-store/onCellIdsChange.js'

packages/public/vue-tinybase/src/generated/composables/default-store/useRow.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { useStore } from '../../../composables/useStore.js'
910
import { onRowChange } from '../../events/custom-store/onRowChange.js'

packages/public/vue-tinybase/src/generated/composables/default-store/useRowCount.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { useStore } from '../../../composables/useStore.js'
910
import { onRowCountChange } from '../../events/custom-store/onRowCountChange.js'

packages/public/vue-tinybase/src/generated/composables/default-store/useRowIds.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { useStore } from '../../../composables/useStore.js'
910
import { onRowIdsChange } from '../../events/custom-store/onRowIdsChange.js'

packages/public/vue-tinybase/src/generated/composables/default-store/useSortedRowIds.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { useStore } from '../../../composables/useStore.js'
910
import { onSortedRowIdsChange } from '../../events/custom-store/onSortedRowIdsChange.js'

packages/public/vue-tinybase/src/generated/composables/default-store/useTable.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { useStore } from '../../../composables/useStore.js'
910
import { onTableChange } from '../../events/custom-store/onTableChange.js'

packages/public/vue-tinybase/src/generated/composables/default-store/useTableCellIds.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { useStore } from '../../../composables/useStore.js'
910
import { onTableCellIdsChange } from '../../events/custom-store/onTableCellIdsChange.js'

packages/public/vue-tinybase/src/generated/composables/default-store/useValue.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable @typescript-eslint/no-unsafe-argument */
66
import { toRef, shallowRef, computed } from '@vue/reactivity'
7+
import { watch } from '@vue/runtime-core'
78

89
import { useStore } from '../../../composables/useStore.js'
910
import { onValueChange } from '../../events/custom-store/onValueChange.js'

0 commit comments

Comments
 (0)