Last active
August 29, 2015 14:21
-
-
Save iamdustan/d4e924e7766967130bd0 to your computer and use it in GitHub Desktop.
Some react-hardware internals.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import createReactHardwareComponentClass from '../createReactHardwareComponentClass'; | |
import modes from './inputModes'; | |
var {PropTypes} = React; | |
var viewConfig = { | |
uiViewClassName: 'Led', | |
validAttributes: { | |
pin: true, | |
value: true, | |
mode: true, | |
}, | |
}; | |
class Led extends React.Component { | |
render() { | |
return ( | |
<Hardware {...this.props} /> | |
); | |
} | |
} | |
Led.displayName = 'Led'; | |
Led.propTypes = { | |
pin: PropTypes.number.isRequired, | |
mode: PropTypes.number, | |
value: PropTypes.number, | |
}; | |
var Hardware = createReactHardwareComponentClass(viewConfig); | |
Hardware.defaultProps = {mode: modes.OUTPUT}; | |
export default Led; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import createReactHardwareComponentClass from '../createReactHardwareComponentClass'; | |
import modes from './inputModes'; | |
import HardwareManager from '../HardwareManager'; | |
import ReactHardwareEventEmitter from '../ReactHardwareEventEmitter'; | |
import findNodeHandle from '../findNodeHandle'; | |
var {PropTypes} = React; | |
var CHANGE_EVENT = 'topChange'; | |
var CLOSE_EVENT = 'topClose'; | |
var OPEN_EVENT = 'topOpen'; | |
var collect = (obj, ...things) => | |
things.reduce((memo, thing) => ((memo[thing] = obj[thing]), memo), {}); | |
var EVENT_TYPE = collect( | |
HardwareManager.customDirectEventTypes, | |
CHANGE_EVENT, CLOSE_EVENT, OPEN_EVENT | |
); | |
var SWITCH_REF = 'switch'; | |
var viewConfig = { | |
uiViewClassName: 'Switch', | |
validAttributes: { | |
pin: true, | |
mode: true, | |
onChange: true, | |
onClose: true, | |
onOpen: true, | |
}, | |
}; | |
function emitEvent(componentInstance, eventName, value) { | |
ReactHardwareEventEmitter.receiveEvent( | |
findNodeHandle(componentInstance), | |
eventName, | |
{ | |
value: value, | |
target: componentInstance, | |
type: EVENT_TYPE[eventName].registrationName, | |
} | |
); | |
} | |
class Switch extends React.Component { | |
componentDidMount() { | |
// set up the hardware polling | |
HardwareManager.read(findNodeHandle(this.refs[SWITCH_REF]), newValue => { | |
if (newValue !== this.value) { | |
var eventName = newValue === 0 ? CLOSE_EVENT : OPEN_EVENT; | |
emitEvent(this, eventName, newValue); | |
emitEvent(this, CHANGE_EVENT, newValue); | |
this.value = newValue; | |
} | |
}); | |
} | |
componentWillUnmount() { | |
// TODO: maybe move this destroyer to the HardwareManager | |
HardwareManager.destroyRead(findNodeHandle(this.refs[SWITCH_REF])); | |
} | |
render() { | |
var props = Object.assign({}, this.props); | |
return ( | |
<Hardware | |
ref={SWITCH_REF} | |
mode={modes.INPUT} | |
{...props} /> | |
); | |
} | |
} | |
var Hardware = createReactHardwareComponentClass(viewConfig); | |
Switch.propTypes = { | |
pin: PropTypes.number.isRequired, | |
mode: PropTypes.number, | |
onChange: PropTypes.func, | |
onClose: PropTypes.func, | |
onOpen: PropTypes.func, | |
}; | |
export default Switch; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment