adds handling of some initial events

This commit is contained in:
Lars Berning 2022-01-10 22:31:00 +01:00
parent 582c5fd41c
commit c753cb4cf4
No known key found for this signature in database
GPG Key ID: 028E73C9E1D8A0B3
4 changed files with 26 additions and 17 deletions

View File

@ -30,10 +30,14 @@ export class App extends AppElement {
// this is how we implement changes to the global state:
// once any child component sends this CustomEvent we update the global state according
// to the changes that were passed to us
this.addEventListener('app-state-changed', (event) => {
this.addEventListener('appStateChanged', (event) => {
const newState = event.detail
this.globalAppState = newState
})
this.addEventListener('triggerAction', (event) => {
this.app.handleAction(event.detail)
})
}
static properties = {

View File

@ -19,9 +19,14 @@ export class AppElement extends LitElement {
// ..and state changes are send back to the root component of the app by dispatching
// a CustomEvent
updateState () {
this.sendEvent('appStateChanged', { ...this.appState })
}
// a helper to dispatch events to the parent components
sendEvent (eventType, eventData) {
this.dispatchEvent(
new CustomEvent('app-state-changed', {
detail: { ...this.appState },
new CustomEvent(eventType, {
detail: eventData,
bubbles: true,
composed: true
})

View File

@ -49,15 +49,14 @@ export class DashboardActions extends AppElement {
window.close()
}
// todo: should use events instead of communicating via a global app object
reset () {
window.app.reset()
this.sendEvent('triggerAction', { command: 'reset' })
}
switchPeripheralMode () {
// window.app.switchPeripheralMode()
// todo: this is just a test property to see if the concept works...
this.appState.peripheralMode = 'PM5'
this.updateState()
// this.appState.peripheralMode = 'PM5'
// this.updateState()
this.sendEvent('triggerAction', { command: 'switchPeripheralMode' })
}
}

View File

@ -125,13 +125,15 @@ export function createApp () {
}
}
function reset () {
resetFields()
if (socket)socket.send(JSON.stringify({ command: 'reset' }))
}
function switchPeripheralMode () {
if (socket)socket.send(JSON.stringify({ command: 'switchPeripheralMode' }))
function handleAction (action) {
if (action.command === 'switchPeripheralMode') {
if (socket)socket.send(JSON.stringify({ command: 'switchPeripheralMode' }))
} else if (action.command === 'reset') {
resetFields()
if (socket)socket.send(JSON.stringify({ command: 'reset' }))
} else {
console.error('no handler defined for action', action)
}
}
function setMetricsCallback (callback) {
@ -142,8 +144,7 @@ export function createApp () {
}
return {
reset,
switchPeripheralMode,
handleAction,
metrics,
setMetricsCallback
}