Created
May 1, 2015 21:04
-
-
Save oliger/f4b1a845572ce9ace6ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const EVENT = 'CHANGE'; | |
function Engine() { | |
this._isInitialized = false; | |
Hull.init({ /* CREDENTIALS */ }, () => { | |
this._isInitialized = true; | |
this.emitChange(); | |
}; | |
this.emitChange(); | |
} | |
assign(Engine.prototype, EventEmitter.prototype, { | |
getState() { | |
return { | |
isInitialized: this._isInitialized; | |
} | |
}, | |
addChangeListener(listener) { | |
this.addListener(EVENT, listener) | |
}, | |
removeChangeListener(listener) { | |
this.removeListener(EVENT, listener); | |
}, | |
emitChange() { | |
this.emit(EVENT); | |
} | |
}); | |
const App = React.createClass({ | |
getInitialState() { | |
return this.props.engine.getState(); | |
}, | |
componentWillMount() { | |
this.props.engine.addChangeListener(this._onChange); | |
}, | |
componentWillUnmount() { | |
this.props.engine.removeChangeListener(this._onChange); | |
}, | |
_onChange() { | |
this.setState(this.props.engine.getState()); | |
}, | |
render() { | |
if (this.state.isInitialized) { | |
return <h1>Yep</h1>; | |
} else { | |
return <h1>Nope</h1>; | |
} | |
} | |
}); | |
function bootstrap() { | |
let engine = new Engine(); | |
let element = document.getElementById('app'); | |
React.render(<App engine={engine} />, element); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
assign
EventEmitter