Skip to content

Commit 628456a

Browse files
committed
feat(checkpoints): add useCheckpointIds composable
1 parent ec0ce29 commit 628456a

File tree

11 files changed

+181
-20
lines changed

11 files changed

+181
-20
lines changed

Diff for: packages/private/docs/api/checkpoints/composables.md

+79-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Composables {#composables}
22

3-
Checkpoinnts composables.
3+
Checkpoints composables.
44

55
## useCheckpoint {#use-checkpoint}
66

@@ -20,15 +20,15 @@ When first accessed, this composable will create a listener so that changes to t
2020

2121
### Returns
2222

23-
- `ComputedRef<string | undefined>`: A **readonly** reference to the string label for the requested checkpoint, an empty string if it was never set, or `undefined` if the checkpoint does not exist..
23+
- `ComputedRef<string | undefined>`: A **readonly** reference to the string label for the requested checkpoint, an empty string if it was never set, or `undefined` if the checkpoint does not exist.
2424

2525
### Example
2626

2727
<div class="hide-default-store">
2828

2929
```vue
3030
<script setup lang="ts">
31-
import { useCell, injectStore, useCheckpoint } from 'vue-tinybase/custom-store'
31+
import { injectStore, injectCheckpoints, useCheckpoint } from 'vue-tinybase/custom-store'
3232
3333
import { Store1Key, Checkpoints1Key } from './store'
3434
@@ -54,7 +54,7 @@ checkpoints.addCheckpoint('sale')
5454

5555
```vue
5656
<script setup lang="ts">
57-
import { useCell, injectStore, useCheckpoint, injectCheckpoints } from 'vue-tinybase'
57+
import { injectStore, injectCheckpoints, useCheckpoint } from 'vue-tinybase'
5858
5959
const store = injectStore()
6060
@@ -74,3 +74,78 @@ checkpoints.addCheckpoint('sale')
7474
```
7575

7676
</div>
77+
78+
## useCheckpointIds {#use-checkpoint-ids}
79+
80+
The `useCheckpointIds` composable returns an array of the checkpoint [Ids](https://tinybase.org/api/common/type-aliases/identity/ids/) being managed by this [Checkpoints](https://tinybase.org/api/checkpoints/interfaces/checkpoints/checkpoints/) object, and registers a listener so that any changes to that result will cause a re-render.
81+
82+
When first accessed, this composable will create a listener so that changes to the checkpoint [Ids](https://tinybase.org/api/common/type-aliases/identity/ids/) will cause a re-render. When the component containing this composable is unmounted, the listener will be automatically removed.
83+
84+
<div class="hide-default-store">
85+
86+
### Parameters
87+
88+
- `checkpoints` ([`Checkpoints`](https://tinybase.org/api/checkpoints/interfaces/checkpoints/checkpoints/)): The [`Checkpoints`](https://tinybase.org/api/checkpoints/interfaces/checkpoints/checkpoints/) object to be accessed.
89+
90+
</div>
91+
92+
### Returns
93+
94+
- `ComputedRef<`[`CheckpointIds`](https://tinybase.org/api/checkpoints/type-aliases/identity/checkpointids/)`>`: A **readonly** reference to the [CheckpointIds](https://tinybase.org/api/checkpoints/type-aliases/identity/checkpointids/) array, containing the checkpoint [Ids](https://tinybase.org/api/common/type-aliases/identity/ids/) managed by this [Checkpoints](https://tinybase.org/api/checkpoints/interfaces/checkpoints/checkpoints/) object.
95+
96+
### Example
97+
98+
<div class="hide-default-store">
99+
100+
```vue
101+
<script setup lang="ts">
102+
import { injectStore, injectCheckpoints, useCheckpointIds } from 'vue-tinybase/custom-store'
103+
104+
import { Store1Key, Checkpoints1Key } from './store'
105+
106+
const store = injectStore(Store1Key)
107+
const checkpoints = injectCheckpoints(Checkpoints1Key)
108+
109+
const checkpointIds = useCheckpointIds(checkpoints)
110+
// UI will show: [[],"0",[]]
111+
112+
store.setCell('pets', 'fido', 'sold', true)
113+
// UI will show: [["0"],null,[]]
114+
115+
checkpoints.addCheckpoint('sale')
116+
// UI will show: [["0"],"1",[]]
117+
</script>
118+
119+
<template>
120+
<div>{{ checkpointIds }}</div>
121+
</template>
122+
```
123+
124+
</div>
125+
126+
<div class="hide-custom-store">
127+
128+
```vue
129+
<script setup lang="ts">
130+
import { injectStore, injectCheckpoints, useCheckpointIds } from 'vue-tinybase'
131+
132+
const store = injectStore()
133+
134+
const checkpoints = injectCheckpoints()
135+
136+
const checkpointIds = useCheckpointIds()
137+
// UI will show: [[],"0",[]]
138+
139+
store.setCell('pets', 'fido', 'sold', true)
140+
// UI will show: [["0"],null,[]]
141+
142+
checkpoints.addCheckpoint('sale')
143+
// UI will show: [["0"],"1",[]]
144+
</script>
145+
146+
<template>
147+
<div>{{ checkpointIds }}</div>
148+
</template>
149+
```
150+
151+
</div>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
import type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js'
2-
import type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js'
1+
import type {
2+
UseCheckpointFunction as UseCheckpointFunctionWithSchemas,
3+
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithSchemas,
4+
} from './with-schemas/composables.js'
5+
import type {
6+
UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas,
7+
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithoutSchemas,
8+
} from './without-schemas/composables.js'
39

410
export type UseCheckpointFunction = UseCheckpointFunctionWithSchemas & UseCheckpointFunctionWithoutSchemas
11+
export type UseCheckpointIdsFunction = UseCheckpointIdsFunctionWithSchemas & UseCheckpointIdsFunctionWithoutSchemas
512

6-
export type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js'
7-
export type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js'
13+
export type {
14+
UseCheckpointFunction as UseCheckpointFunctionWithSchemas,
15+
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithSchemas,
16+
} from './with-schemas/composables.js'
17+
export type {
18+
UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas,
19+
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithoutSchemas,
20+
} from './without-schemas/composables.js'
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity'
2-
import type { Checkpoints, Id } from 'tinybase/with-schemas'
2+
import type { Checkpoints, Id, CheckpointIds } from 'tinybase/with-schemas'
33

44
export type UseCheckpointFunction = (
55
checkpoints: Checkpoints<any>,
66
checkpointId: MaybeRefOrGetter<Id>,
77
) => ComputedRef<string | undefined>
8+
9+
export type UseCheckpointIdsFunction = (checkpoints: Checkpoints<any>) => ComputedRef<CheckpointIds>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity'
2-
import type { Checkpoints, Id } from 'tinybase'
2+
import type { CheckpointIds, Checkpoints, Id } from 'tinybase'
33

44
export type UseCheckpointFunction = (
55
checkpoints: Checkpoints,
66
checkpointId: MaybeRefOrGetter<Id>,
77
) => ComputedRef<string | undefined>
8+
9+
export type UseCheckpointIdsFunction = (checkpoints: Checkpoints) => ComputedRef<CheckpointIds>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
import type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js'
2-
import type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js'
1+
import type {
2+
UseCheckpointFunction as UseCheckpointFunctionWithSchemas,
3+
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithSchemas,
4+
} from './with-schemas/composables.js'
5+
import type {
6+
UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas,
7+
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithoutSchemas,
8+
} from './without-schemas/composables.js'
39

410
export type UseCheckpointFunction = UseCheckpointFunctionWithSchemas & UseCheckpointFunctionWithoutSchemas
11+
export type UseCheckpointIdsFunction = UseCheckpointIdsFunctionWithSchemas & UseCheckpointIdsFunctionWithoutSchemas
512

6-
export type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js'
7-
export type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js'
13+
export type {
14+
UseCheckpointFunction as UseCheckpointFunctionWithSchemas,
15+
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithSchemas,
16+
} from './with-schemas/composables.js'
17+
export type {
18+
UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas,
19+
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithoutSchemas,
20+
} from './without-schemas/composables.js'
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity'
2-
import type { Id } from 'tinybase/with-schemas'
2+
import type { Id, CheckpointIds } from 'tinybase/with-schemas'
33

44
export type UseCheckpointFunction = (checkpointId: MaybeRefOrGetter<Id>) => ComputedRef<string | undefined>
5+
6+
export type UseCheckpointIdsFunction = () => ComputedRef<CheckpointIds>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity'
2-
import type { Id } from 'tinybase'
2+
import type { Id, CheckpointIds } from 'tinybase'
33

44
export type UseCheckpointFunction = (checkpointId: MaybeRefOrGetter<Id>) => ComputedRef<string | undefined>
5+
6+
export type UseCheckpointIdsFunction = () => ComputedRef<CheckpointIds>

Diff for: packages/public/vue-tinybase/src/custom-store/checkpoints/composables.test.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createStore, createCheckpoints } from 'tinybase'
33
import { expect, test } from 'vitest'
44
import { defineComponent, h, nextTick } from 'vue'
55

6-
import { useCheckpoint } from './composables.js'
6+
import { useCheckpoint, useCheckpointIds } from './composables.js'
77

88
test('[custom-store/checkpoints/composables] useCheckpoint', async () => {
99
const store = createStore()
@@ -27,3 +27,26 @@ test('[custom-store/checkpoints/composables] useCheckpoint', async () => {
2727

2828
expect(wrapper.text()).toBe('sale')
2929
})
30+
31+
test('[custom-store/checkpoints/composables] useCheckpointIds', async () => {
32+
const store = createStore()
33+
const checkpoints = createCheckpoints(store)
34+
35+
const Component = defineComponent({
36+
setup() {
37+
const checkpoint = useCheckpointIds(checkpoints)
38+
39+
return () => h('div', [JSON.stringify(checkpoint.value)])
40+
},
41+
})
42+
43+
const wrapper = mount(Component)
44+
expect(wrapper.text()).toBe('[[],"0",[]]')
45+
46+
store.setCell('pets', 'fido', 'sold', true)
47+
checkpoints.addCheckpoint('sale')
48+
49+
await nextTick()
50+
51+
expect(wrapper.text()).toBe('[["0"],"1",[]]')
52+
})
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { ReturnType, useListenable } from '../../common/useListenable.js'
22

3-
import type { UseCheckpointFunction } from '../../@types/custom-store/index.js'
3+
import type { UseCheckpointFunction, UseCheckpointIdsFunction } from '../../@types/custom-store/index.js'
44
import type { MaybeRefOrGetter } from '@vue/reactivity'
55
import type { Checkpoints, Id } from 'tinybase'
66

77
export const useCheckpoint = ((checkpoints: Checkpoints, checkpointId: MaybeRefOrGetter<Id>) =>
88
useListenable(checkpoints, 'Checkpoint', ReturnType.CellOrValue, [checkpointId])) as UseCheckpointFunction
9+
10+
export const useCheckpointIds = ((checkpoints: Checkpoints) =>
11+
useListenable(checkpoints, 'CheckpointIds', ReturnType.Checkpoints)) as UseCheckpointIdsFunction

Diff for: packages/public/vue-tinybase/src/default-store/checkpoints/composables.test.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { defineComponent, h, nextTick } from 'vue'
55

66
import { providerWrapper } from '../../test-utils/store.js'
77

8-
import { useCheckpoint } from './composables.js'
8+
import { useCheckpoint, useCheckpointIds } from './composables.js'
99

1010
test('[default-store/checkpoints/composables] useCheckpoint', async () => {
1111
const store = createStore()
@@ -29,3 +29,26 @@ test('[default-store/checkpoints/composables] useCheckpoint', async () => {
2929

3030
expect(wrapper.text()).toBe('sale')
3131
})
32+
33+
test('[default-store/checkpoints/composables] useCheckpointIds', async () => {
34+
const store = createStore()
35+
const checkpoints = createCheckpoints(store)
36+
37+
const Component = defineComponent({
38+
setup() {
39+
const checkpoint = useCheckpointIds()
40+
41+
return () => h('div', [JSON.stringify(checkpoint.value)])
42+
},
43+
})
44+
45+
const wrapper = mount(providerWrapper(Component, undefined, { checkpoints }))
46+
expect(wrapper.text()).toBe('[[],"0",[]]')
47+
48+
store.setCell('pets', 'fido', 'sold', true)
49+
checkpoints.addCheckpoint('sale')
50+
51+
await nextTick()
52+
53+
expect(wrapper.text()).toBe('[["0"],"1",[]]')
54+
})

Diff for: packages/public/vue-tinybase/src/default-store/checkpoints/composables.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { ReturnType, useListenable } from '../../common/useListenable.js'
22

33
import { injectCheckpoints } from './context.js'
44

5-
import type { UseCheckpointFunction } from '../../@types/default-store/index.js'
5+
import type { UseCheckpointFunction, UseCheckpointIdsFunction } from '../../@types/default-store/index.js'
66
import type { MaybeRefOrGetter } from '@vue/reactivity'
77
import type { Id } from 'tinybase'
88

99
export const useCheckpoint = ((checkpointId: MaybeRefOrGetter<Id>) =>
1010
useListenable(injectCheckpoints(), 'Checkpoint', ReturnType.CellOrValue, [checkpointId])) as UseCheckpointFunction
11+
12+
export const useCheckpointIds = (() =>
13+
useListenable(injectCheckpoints(), 'CheckpointIds', ReturnType.Checkpoints)) as UseCheckpointIdsFunction

0 commit comments

Comments
 (0)