Skip to content

Commit

Permalink
feat(checkpoints): add useCheckpointIds composable
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmessing committed Jul 27, 2024
1 parent ec0ce29 commit 1f11096
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js'
import type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js'
import type {
UseCheckpointFunction as UseCheckpointFunctionWithSchemas,
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithSchemas,
} from './with-schemas/composables.js'
import type {
UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas,
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithoutSchemas,
} from './without-schemas/composables.js'

export type UseCheckpointFunction = UseCheckpointFunctionWithSchemas & UseCheckpointFunctionWithoutSchemas
export type UseCheckpointIdsFunction = UseCheckpointIdsFunctionWithSchemas & UseCheckpointIdsFunctionWithoutSchemas

export type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js'
export type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js'
export type {
UseCheckpointFunction as UseCheckpointFunctionWithSchemas,
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithSchemas,
} from './with-schemas/composables.js'
export type {
UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas,
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithoutSchemas,
} from './without-schemas/composables.js'
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity'
import type { Checkpoints, Id } from 'tinybase/with-schemas'
import type { Checkpoints, Id, CheckpointIds } from 'tinybase/with-schemas'

export type UseCheckpointFunction = (
checkpoints: Checkpoints<any>,
checkpointId: MaybeRefOrGetter<Id>,
) => ComputedRef<string | undefined>

export type UseCheckpointIdsFunction = (checkpoints: Checkpoints<any>) => ComputedRef<CheckpointIds>
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity'
import type { Checkpoints, Id } from 'tinybase'
import type { CheckpointIds, Checkpoints, Id } from 'tinybase'

export type UseCheckpointFunction = (
checkpoints: Checkpoints,
checkpointId: MaybeRefOrGetter<Id>,
) => ComputedRef<string | undefined>

export type UseCheckpointIdsFunction = (checkpoints: Checkpoints) => ComputedRef<CheckpointIds>
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js'
import type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js'
import type {
UseCheckpointFunction as UseCheckpointFunctionWithSchemas,
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithSchemas,
} from './with-schemas/composables.js'
import type {
UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas,
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithoutSchemas,
} from './without-schemas/composables.js'

export type UseCheckpointFunction = UseCheckpointFunctionWithSchemas & UseCheckpointFunctionWithoutSchemas
export type UseCheckpointIdsFunction = UseCheckpointIdsFunctionWithSchemas & UseCheckpointIdsFunctionWithoutSchemas

export type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js'
export type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js'
export type {
UseCheckpointFunction as UseCheckpointFunctionWithSchemas,
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithSchemas,
} from './with-schemas/composables.js'
export type {
UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas,
UseCheckpointIdsFunction as UseCheckpointIdsFunctionWithoutSchemas,
} from './without-schemas/composables.js'
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity'
import type { Id } from 'tinybase/with-schemas'
import type { Id, CheckpointIds } from 'tinybase/with-schemas'

export type UseCheckpointFunction = (checkpointId: MaybeRefOrGetter<Id>) => ComputedRef<string | undefined>

export type UseCheckpointIdsFunction = () => ComputedRef<CheckpointIds>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity'
import type { Id } from 'tinybase'
import type { Id, CheckpointIds } from 'tinybase'

export type UseCheckpointFunction = (checkpointId: MaybeRefOrGetter<Id>) => ComputedRef<string | undefined>

export type UseCheckpointIdsFunction = () => ComputedRef<CheckpointIds>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createStore, createCheckpoints } from 'tinybase'
import { expect, test } from 'vitest'
import { defineComponent, h, nextTick } from 'vue'

import { useCheckpoint } from './composables.js'
import { useCheckpoint, useCheckpointIds } from './composables.js'

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

expect(wrapper.text()).toBe('sale')
})

test('[custom-store/checkpoints/composables] useCheckpointIds', async () => {
const store = createStore()
const checkpoints = createCheckpoints(store)

const Component = defineComponent({
setup() {
const checkpoint = useCheckpointIds(checkpoints)

return () => h('div', [JSON.stringify(checkpoint.value)])
},
})

const wrapper = mount(Component)
expect(wrapper.text()).toBe('[[],"0",[]]')

store.setCell('pets', 'fido', 'sold', true)
checkpoints.addCheckpoint('sale')

await nextTick()

expect(wrapper.text()).toBe('[["0"],"1",[]]')
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { ReturnType, useListenable } from '../../common/useListenable.js'

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

export const useCheckpoint = ((checkpoints: Checkpoints, checkpointId: MaybeRefOrGetter<Id>) =>
useListenable(checkpoints, 'Checkpoint', ReturnType.CellOrValue, [checkpointId])) as UseCheckpointFunction

export const useCheckpointIds = ((checkpoints: Checkpoints) =>
useListenable(checkpoints, 'CheckpointIds', ReturnType.Checkpoints)) as UseCheckpointIdsFunction
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defineComponent, h, nextTick } from 'vue'

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

import { useCheckpoint } from './composables.js'
import { useCheckpoint, useCheckpointIds } from './composables.js'

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

expect(wrapper.text()).toBe('sale')
})

test('[default-store/checkpoints/composables] useCheckpointIds', async () => {
const store = createStore()
const checkpoints = createCheckpoints(store)

const Component = defineComponent({
setup() {
const checkpoint = useCheckpointIds()

return () => h('div', [JSON.stringify(checkpoint.value)])
},
})

const wrapper = mount(providerWrapper(Component, undefined, { checkpoints }))
expect(wrapper.text()).toBe('[[],"0",[]]')

store.setCell('pets', 'fido', 'sold', true)
checkpoints.addCheckpoint('sale')

await nextTick()

expect(wrapper.text()).toBe('[["0"],"1",[]]')
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { ReturnType, useListenable } from '../../common/useListenable.js'

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

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

export const useCheckpoint = ((checkpointId: MaybeRefOrGetter<Id>) =>
useListenable(injectCheckpoints(), 'Checkpoint', ReturnType.CellOrValue, [checkpointId])) as UseCheckpointFunction

export const useCheckpointIds = (() =>
useListenable(injectCheckpoints(), 'CheckpointIds', ReturnType.Checkpoints)) as UseCheckpointIdsFunction

0 comments on commit 1f11096

Please sign in to comment.