Loëck Vézien
7 years ago
8 changed files with 110 additions and 42 deletions
@ -0,0 +1,19 @@ |
|||||
|
// eslint-disable import/prefer-default-export
|
||||
|
|
||||
|
export const devicesUpdate = payload => dispatch => |
||||
|
dispatch({ |
||||
|
type: 'DEVICES_UPDATE', |
||||
|
payload, |
||||
|
}) |
||||
|
|
||||
|
export const deviceAdd = payload => dispatch => |
||||
|
dispatch({ |
||||
|
type: 'DEVICE_ADD', |
||||
|
payload, |
||||
|
}) |
||||
|
|
||||
|
export const deviceRemove = payload => dispatch => |
||||
|
dispatch({ |
||||
|
type: 'DEVICE_REMOVE', |
||||
|
payload, |
||||
|
}) |
@ -1,34 +1,48 @@ |
|||||
import React, { Component } from 'react' |
// @flow
|
||||
|
|
||||
|
import React, { PureComponent } from 'react' |
||||
|
import { connect } from 'react-redux' |
||||
import { ipcRenderer } from 'electron' |
import { ipcRenderer } from 'electron' |
||||
|
|
||||
class App extends Component { |
import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices' |
||||
state = { |
|
||||
devices: [], |
type Props = { |
||||
} |
devicesUpdate: (devices: Object) => void, |
||||
|
deviceAdd: (device: Object) => void, |
||||
|
deviceRemove: (device: Object) => void, |
||||
|
devices: Object, |
||||
|
} |
||||
|
|
||||
componentDidMount() { |
class Home extends PureComponent<Props> { |
||||
|
componentWillMount() { |
||||
|
const { devicesUpdate, deviceAdd, deviceRemove } = this.props |
||||
|
|
||||
|
ipcRenderer.on('updateDevices', (e, devices) => devicesUpdate(devices)) |
||||
|
ipcRenderer.on('addDevice', (e, device) => deviceAdd(device)) |
||||
|
ipcRenderer.on('removeDevice', (e, device) => deviceRemove(device)) |
||||
|
|
||||
|
// First renderer, we get all devices
|
||||
|
ipcRenderer.send('getDevices') |
||||
|
|
||||
|
// Start detection when we plug/unplug devices
|
||||
ipcRenderer.send('listenDevices') |
ipcRenderer.send('listenDevices') |
||||
|
} |
||||
|
|
||||
ipcRenderer.on('addDevice', (e, device) => |
componentWillUnmount() { |
||||
this.setState(prev => ({ |
ipcRenderer.removeAllListeners('updateDevices') |
||||
devices: [...prev.devices, device].filter( |
ipcRenderer.removeAllListeners('addDevice') |
||||
(v, i, s) => s.findIndex(t => t.path === v.path) === i, |
ipcRenderer.removeAllListeners('removeDevice') |
||||
), |
|
||||
})), |
|
||||
) |
|
||||
|
|
||||
ipcRenderer.on('removeDevice', (e, device) => |
|
||||
this.setState(prev => ({ |
|
||||
devices: prev.devices.filter(d => d.path !== device.path), |
|
||||
})), |
|
||||
) |
|
||||
} |
} |
||||
|
|
||||
render() { |
render() { |
||||
const { devices } = this.state |
const { devices } = this.props |
||||
|
|
||||
return <div>{devices.map(device => device.path)}</div> |
return <div>{devices.map(device => device.path)}</div> |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
export default App |
export default connect(({ devices }) => ({ devices }), { |
||||
|
deviceAdd, |
||||
|
deviceRemove, |
||||
|
devicesUpdate, |
||||
|
})(Home) |
||||
|
@ -0,0 +1,14 @@ |
|||||
|
// @flow
|
||||
|
|
||||
|
import { handleActions } from 'redux-actions' |
||||
|
|
||||
|
const state = [] |
||||
|
|
||||
|
const handlers = { |
||||
|
DEVICES_UPDATE: (state, { payload: devices }) => devices, |
||||
|
DEVICE_ADD: (state, { payload: device }) => |
||||
|
[...state, device].filter((v, i, s) => s.findIndex(t => t.path === v.path) === i), |
||||
|
DEVICE_REMOVE: (state, { payload: device }) => state.filter(d => d.path !== device.path), |
||||
|
} |
||||
|
|
||||
|
export default handleActions(handlers, state) |
Loading…
Reference in new issue