Skip to content

Commit

Permalink
Renamed all back-end switch references to device where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
6XGate committed Nov 19, 2024
1 parent 6313c6a commit 1de23d3
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 79 deletions.
22 changes: 11 additions & 11 deletions src/main/dao/switches.ts → src/main/dao/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import { Database, DocumentId, inferDocumentOf, inferNewDocumentOf, inferUpdates
import useTiesDatabase from './ties'
import type { RevisionId } from '../services/database'

export const SwitchModel = z.object({
export const DeviceModel = z.object({
driverId: DocumentId,
title: z.string().min(1),
path: z.string().min(1)
})

const useSwitchesDatabase = memo(
const useDevicesDatabase = memo(
() =>
new (class extends Database.of('switches', SwitchModel) {
new (class extends Database.of('switches', DeviceModel) {
readonly #ties = useTiesDatabase()

override async remove(id: DocumentId, rev?: RevisionId) {
await super.remove(id, rev)

const related = await this.#ties.forSwitch(id)
const related = await this.#ties.forDevice(id)
await Promise.all(
related.map(async ({ _id, _rev }) => {
await this.#ties.remove(_id, _rev)
Expand All @@ -28,11 +28,11 @@ const useSwitchesDatabase = memo(
})()
)

export type Switch = inferDocumentOf<typeof SwitchModel>
export const Switch = inferDocumentOf(SwitchModel)
export type NewSwitch = inferNewDocumentOf<typeof SwitchModel>
export const NewSwitch = inferNewDocumentOf(SwitchModel)
export type SwitchUpdate = inferUpdatesOf<typeof SwitchModel>
export const SwitchUpdate = inferUpdatesOf(SwitchModel)
export type Device = inferDocumentOf<typeof DeviceModel>
export const Device = inferDocumentOf(DeviceModel)
export type NewDevice = inferNewDocumentOf<typeof DeviceModel>
export const NewDevice = inferNewDocumentOf(DeviceModel)
export type DeviceUpdate = inferUpdatesOf<typeof DeviceModel>
export const DeviceUpdate = inferUpdatesOf(DeviceModel)

export default useSwitchesDatabase
export default useDevicesDatabase
2 changes: 1 addition & 1 deletion src/main/dao/ties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const indices = { sourceId: ['sourceId'], switchId: ['switchId'] }
const useTiesDatabase = memo(
() =>
new (class extends Database.of('ties', TieModel, indices) {
async forSwitch(switchId: DocumentId) {
async forDevice(switchId: DocumentId) {
return await this.run(
async (db) =>
await db
Expand Down
22 changes: 22 additions & 0 deletions src/main/routes/data/devices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import useDevicesDatabase, { NewDevice, DeviceUpdate } from '../../dao/devices'
import { DocumentId } from '../../services/database'
import { procedure, router } from '../../services/rpc/trpc'

export default function useDevicesRouter() {
const devices = useDevicesDatabase()
return router({
compact: procedure.mutation(async () => {
await devices.compact()
}),
all: procedure.query(async () => await devices.all()),
get: procedure.input(DocumentId).query(async ({ input }) => await devices.get(input)),
add: procedure.input(NewDevice).mutation(async ({ input }) => await devices.add(input)),
update: procedure.input(DeviceUpdate).mutation(async ({ input }) => await devices.update(input)),
remove: procedure.input(DocumentId).mutation(async ({ input }) => {
await devices.remove(input)
}),
clear: procedure.mutation(async () => {
await devices.clear()
})
})
}
2 changes: 0 additions & 2 deletions src/main/routes/data/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { DocumentId } from '../../services/database'
import { procedure, router } from '../../services/rpc/trpc'
import { Attachment } from '@/attachments'

export type { Source, NewSource, SourceUpdate } from '../../dao/sources'

const InsertInputs = z.tuple([NewSource]).rest(z.instanceof(Attachment))
const UpdateInputs = z.tuple([SourceUpdate]).rest(z.instanceof(Attachment))

Expand Down
24 changes: 0 additions & 24 deletions src/main/routes/data/switches.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/main/routes/data/ties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import useTiesDatabase, { NewTie, TieUpdate } from '../../dao/ties'
import { DocumentId } from '../../services/database'
import { procedure, router } from '../../services/rpc/trpc'

export type { Tie, NewTie, TieUpdate } from '../../dao/ties'

export default function useTiesRouter() {
const ties = useTiesDatabase()
return router({
Expand All @@ -20,7 +18,7 @@ export default function useTiesRouter() {
clear: procedure.mutation(async () => {
await ties.clear()
}),
forSwitch: procedure.input(DocumentId).query(async ({ input }) => await ties.forSwitch(input)),
forDevice: procedure.input(DocumentId).query(async ({ input }) => await ties.forDevice(input)),
forSource: procedure.input(DocumentId).query(async ({ input }) => await ties.forSource(input))
})
}
4 changes: 2 additions & 2 deletions src/main/routes/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { memo } from 'radash'
import useAppInfo from '../services/app'
import { procedure, router } from '../services/rpc/trpc'
import useUserInfo from '../services/user'
import useDevicesRouter from './data/devices'
import useSourcesRouter from './data/sources'
import useUserStoreRouter from './data/storage'
import useSwitchesRouter from './data/switches'
import useTiesRouter from './data/ties'
import useDriversRouter from './drivers'
import useSerialPortRouter from './ports'
Expand All @@ -25,7 +25,7 @@ export const useAppRouter = memo(() =>
// Data service routes
storage: useUserStoreRouter(),
ties: useTiesRouter(),
switches: useSwitchesRouter(),
devices: useDevicesRouter(),
sources: useSourcesRouter(),
updates: useUpdaterRouter()
})
Expand Down
2 changes: 1 addition & 1 deletion src/preload/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type { PortEntry } from '../main/services/ports'
export type { UpdateInfo, ProgressInfo } from '../main/services/updater'

export type { Source, NewSource, SourceUpdate } from '../main/dao/sources'
export type { Switch, NewSwitch, SwitchUpdate } from '../main/dao/switches'
export type { Device, NewDevice, DeviceUpdate } from '../main/dao/devices'
export type { Tie, NewTie, TieUpdate } from '../main/dao/ties'
export type { ApiLocales } from '../main/locale'

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/hooks/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { toValue, computed, readonly } from 'vue'
import { useI18n } from 'vue-i18n'
import type { I18nSchema } from '../locales/locales'
import type { PortEntry } from '../services/ports'
import type { NewSwitch } from '../services/switches'
import type { NewDevice } from '../services/switches'
import type { LocationType } from '@/location'
import type { MessageProps } from '@vuelidate/validators'
import type { MaybeRefOrGetter, Ref } from 'vue'
Expand Down Expand Up @@ -102,7 +102,7 @@ export function useLocation(location: Ref<string | undefined>, validSwitches: Ma
}

export const useSwitchLocation = (
switcher: MaybeRefOrGetter<NewSwitch>,
switcher: MaybeRefOrGetter<NewDevice>,
validSwitches: MaybeRefOrGetter<readonly PortEntry[]>
) => {
const location = computed({
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/modals/SwitchDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import useDrivers from '../services/driver'
import usePorts from '../services/ports'
import { useDialogs, useSwitchDialog } from './dialogs'
import type { I18nSchema } from '../locales/locales'
import type { NewSwitch } from '../services/switches'
import type { NewDevice } from '../services/switches'
import type { DeepReadonly } from 'vue'
const props = defineProps<{
Expand All @@ -19,12 +19,12 @@ const props = defineProps<{
visible?: boolean
// Form
editing: boolean
switch: DeepReadonly<NewSwitch>
switch: DeepReadonly<NewDevice>
}>()
const emit = defineEmits<{
(on: 'update:visible', value: boolean): void
(on: 'confirm', value: NewSwitch): void
(on: 'confirm', value: NewDevice): void
}>()
const { t } = useI18n<I18nSchema>()
Expand All @@ -39,7 +39,7 @@ const ports = usePorts()
onBeforeMount(ports.all)
// eslint-disable-next-line vue/no-setup-props-reactivity-loss -- Prop reactivity not desired.
const target = ref<NewSwitch>(structuredClone(props.switch))
const target = ref<NewDevice>(structuredClone(props.switch))
const location = computed({
get: () => v$.path.$model,
set: (v) => {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pages/SourcePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { I18nSchema } from '../locales/locales'
import type { DriverInformation } from '../services/driver'
import type { Source } from '../services/sources'
import type { DocumentId } from '../services/store'
import type { Switch } from '../services/switches'
import type { Device } from '../services/switches'
import type { NewTie, Tie } from '../services/ties'
import type { DeepReadonly } from 'vue'
import { isNotNullish } from '@/basics'
Expand Down Expand Up @@ -89,7 +89,7 @@ onBeforeMount(loadSource)
//
interface TieEntry {
switch: DeepReadonly<Switch>
switch: DeepReadonly<Device>
driver: DriverInformation
tie: DeepReadonly<Tie>
}
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/pages/SwitchList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useDrivers from '../services/driver'
import { useSwitches } from '../services/switches'
import type { I18nSchema } from '../locales/locales'
import type { DriverInformation } from '../services/driver'
import type { NewSwitch, Switch } from '../services/switches'
import type { NewDevice, Device } from '../services/switches'
import type { DeepReadonly } from 'vue'
import { isNotNullish } from '@/basics'
Expand All @@ -22,7 +22,7 @@ const dialogs = useDialogs()
const { t } = useI18n<I18nSchema>()
interface Item {
switch: DeepReadonly<Switch>
switch: DeepReadonly<Device>
driver: DriverInformation
}
Expand All @@ -45,7 +45,7 @@ const refresh = useGuardedAsyncOp(async () => {
onMounted(refresh)
async function addSwitch(target: NewSwitch) {
async function addSwitch(target: NewDevice) {
try {
await switches.add(target)
} catch (e) {
Expand All @@ -54,7 +54,7 @@ async function addSwitch(target: NewSwitch) {
}
}
async function updateSwitch(target: Switch, changes: NewSwitch) {
async function updateSwitch(target: Device, changes: NewDevice) {
try {
await switches.update({ ...target, ...changes })
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/services/backup/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ export async function importSettings(file: File) {
const switchItem = switches.items.find((s) => s._id === item.switchId)
// Non-fatally skip ties that reference missing switches or sources.
if (sourceItem == null) {
console.warn(`Switch for tie no longer present; ${item.sourceId}`)
console.warn(`Source for tie no longer present; ${item.sourceId}`)
return
}

if (switchItem == null) {
console.warn(`Switch for tie no longer present; ${item.switchId}`)
console.warn(`Device for tie no longer present; ${item.switchId}`)
return
}

Expand Down
22 changes: 11 additions & 11 deletions src/renderer/services/switches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { forceUndefined } from '../hooks/utilities'
import { useClient } from './rpc/trpc'
import { useDataStore } from './store'
import type { DocumentId } from './store'
import type { NewSwitch, Switch, SwitchUpdate } from '../../preload/api'
import type { NewDevice, Device, DeviceUpdate } from '../../preload/api'

export type { NewSwitch, Switch, SwitchUpdate } from '../../preload/api'
export type { NewDevice, Device, DeviceUpdate } from '../../preload/api'

export const useSwitches = defineStore('switches', function defineSwitches() {
const { switches } = useClient()
const store = useDataStore<Switch>()
const { devices } = useClient()
const store = useDataStore<Device>()

function blank(): NewSwitch {
function blank(): NewDevice {
return {
driverId: forceUndefined(),
title: forceUndefined(),
Expand All @@ -20,19 +20,19 @@ export const useSwitches = defineStore('switches', function defineSwitches() {
}

const compact = store.defineOperation(async () => {
await switches.compact.mutate()
await devices.compact.mutate()
})

const all = store.defineFetchMany(async () => await switches.all.query())
const all = store.defineFetchMany(async () => await devices.all.query())

const get = store.defineFetch(async (id: DocumentId) => await switches.get.query(id))
const get = store.defineFetch(async (id: DocumentId) => await devices.get.query(id))

const add = store.defineInsertion(async (document: NewSwitch) => await switches.add.mutate(document))
const add = store.defineInsertion(async (document: NewDevice) => await devices.add.mutate(document))

const update = store.defineMutation(async (document: SwitchUpdate) => await switches.update.mutate(document))
const update = store.defineMutation(async (document: DeviceUpdate) => await devices.update.mutate(document))

const remove = store.defineRemoval(async (id: DocumentId) => {
await switches.remove.mutate(id)
await devices.remove.mutate(id)
return id
})

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/services/ties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const useTies = defineStore('ties', function defineTies() {
return id
})

const forSwitch = store.defineFetchMany(async (id: DocumentId) => await ties.forSwitch.query(id))
const forSwitch = store.defineFetchMany(async (id: DocumentId) => await ties.forDevice.query(id))

const forSource = store.defineFetchMany(async (id: DocumentId) => await ties.forSource.query(id))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest'
import useSwitchesDatabase from '../../../main/dao/switches'
import useDevicesDatabase from '../../../main/dao/devices'
import useTiesDatabase from '../../../main/dao/ties'
import { seedDatabase } from '../../seeds/database.seed'
import type { NewSwitch } from '../../../main/dao/switches'
import type { NewDevice } from '../../../main/dao/devices'
import { Attachment } from '@/attachments'

const mock = await vi.hoisted(async () => await import('../../support/mock'))
Expand All @@ -13,12 +13,12 @@ beforeAll(() => {
})

let database: Awaited<ReturnType<typeof seedDatabase>>
let switchesDao: ReturnType<typeof useSwitchesDatabase>
let switchesDao: ReturnType<typeof useDevicesDatabase>
let tiesDao: ReturnType<typeof useTiesDatabase>

beforeEach(async () => {
database = await seedDatabase()
switchesDao = useSwitchesDatabase()
switchesDao = useDevicesDatabase()
tiesDao = useTiesDatabase()
})

Expand Down Expand Up @@ -46,7 +46,7 @@ const kRevPattern = /^[0-9]-[0-9a-f]{32}$/u
test('adding', async () => {
const file = new File([Buffer.from('hello')], 'hello.txt', { type: 'text/plain' })
const attachment = await Attachment.fromFile(file)
const raw = { title: 'Extron', driverId: database.extronSis.guid, path: 'ip:192.168.10.2' } satisfies NewSwitch
const raw = { title: 'Extron', driverId: database.extronSis.guid, path: 'ip:192.168.10.2' } satisfies NewDevice
const doc = await switchesDao.add(raw, attachment)

expect(doc._id).toMatch(kUuidPattern)
Expand Down
8 changes: 4 additions & 4 deletions src/tests/seeds/database.seed.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { alphabetical, map } from 'radash'
import useDevicesDatabase from '../../main/dao/devices'
import useSourcesDatabase from '../../main/dao/sources'
import useSwitchesDatabase from '../../main/dao/switches'
import useTiesDatabase from '../../main/dao/ties'
import useDrivers from '../../main/services/drivers'
import type { Device } from '../../main/dao/devices'
import type { Source } from '../../main/dao/sources'
import type { Switch } from '../../main/dao/switches'
import type { Tie } from '../../main/dao/ties'
import { Attachment } from '@/attachments'
import { raiseError } from '@/error-handling'

export async function seedDatabase() {
const switchDao = useSwitchesDatabase()
const switchDao = useDevicesDatabase()
const sourceDao = useSourcesDatabase()
const tieDao = useTiesDatabase()

Expand All @@ -33,7 +33,7 @@ export async function seedDatabase() {
{ title: 'Sony BVM24D', driverId: sonyRemote.guid, path: 'port:/dev/ttys1' }
],
async (doc) => await switchDao.add(doc)
)) as [Switch, Switch, Switch]
)) as [Device, Device, Device]

const file = new File([Buffer.from('test')], 'test.txt', { type: 'text/plain' })
const image = await Attachment.fromFile(file)
Expand Down

0 comments on commit 1de23d3

Please sign in to comment.