Skip to content

Commit

Permalink
libs: Create automagic
Browse files Browse the repository at this point in the history
This library is responsible for performing functionalities independent of user action.
  • Loading branch information
rafaellehmkuhl committed Dec 27, 2023
1 parent c962e3b commit 83dedc3
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/libs/automagic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useControllerStore } from '@/stores/controller'
import { useMainVehicleStore } from '@/stores/mainVehicle'

/**
* A cockpit function to be formed independent of user action
*/
export enum AutomagicId {
ImportJoystickFunctionsOnBoot = 'ImportJoystickFunctionsOnBoot',
}
export const allAutomagicIds = Object.keys(AutomagicId).filter((v) => isNaN(Number(v))) as unknown as AutomagicId[]

export const availableAutomagics = [
{
id: AutomagicId.ImportJoystickFunctionsOnBoot,
description: 'Import joystick function mappings from the vehicle on Cockpit boot.',
function: async () => {
const { globalAddress } = useMainVehicleStore()
const controllerStore = useControllerStore()
await controllerStore.importFunctionsMappingFromVehicle(globalAddress)
},
},
]

export const runAutomagics = async (selectedAutomagicsIds: AutomagicId[]): Promise<void> => {
console.info(`Running the following automagics: ${selectedAutomagicsIds}.`)
availableAutomagics
.filter((automagic) => selectedAutomagicsIds.includes(automagic.id))
.forEach((automagic) => {
try {
console.info(`Running automagic '${automagic.id}'...`)
automagic.function()
} catch (error) {
console.error(`Could not run automagic '${automagic.id}'. ${error}`)
}
})
}

0 comments on commit 83dedc3

Please sign in to comment.