Skip to content

Instantly share code, notes, and snippets.

@saifelse
Created August 25, 2016 12:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saifelse/a7bae208737b283a21aa7b25565f4df1 to your computer and use it in GitHub Desktop.
Save saifelse/a7bae208737b283a21aa7b25565f4df1 to your computer and use it in GitHub Desktop.
React = require 'react'
Perf = require 'react-addons-perf'
ReactDOM = require 'react-dom'
rd = React.DOM
###
Usage: `require('debug/RecordPerfButton').attachToDOM()`
Add to any file to attach a record button to the DOM, toggleable via click and <alt> keypress.
Useful for quickly testing performance as it will:
- start/stop a perf measurement,
- make Perf accessible on window,
- and log the wasted time.
###
hasRendered = false
RecordPerfButton = React.createClass {
displayName: 'RecordPerfButton'
statics: {
attachToDOM: ->
if hasRendered
return
hasRendered = true
document.addEventListener 'DOMContentLoaded', ->
# Find / create the container
container = document.createElement('div')
container.style.position = 'fixed'
container.style.top = 0
container.style.zIndex = 9999
node = document.body.appendChild(container)
ReactDOM.render React.createElement(RecordPerfButton), container
return
return
}
getInitialState: -> {recording: false}
toggleRecord: ->
if not @state.recording
Perf.start()
window.Perf = Perf
@setState({recording: true})
else
Perf.stop()
Perf.printWasted()
@setState({recording: false})
return
mouseDownHandler: (e) ->
e.preventDefault()
return
handleKeyDown: (e) ->
if e.altKey
@toggleRecord()
return
componentDidMount: ->
window.addEventListener('keydown', @handleKeyDown)
return
componentWillUnmount: ->
window.removeEventListener('keydown', @handleKeyDown)
return
render: ->
return rd.div {
onMouseDown: @mouseDownHandler
onClick: @toggleRecord
style: {
display: 'inline-block'
marginTop: 13
marginLeft: 13
width: 30
height: 30
borderRadius: 15
backgroundColor: if @state.recording then 'red' else 'grey'
}
}
}
module.exports = RecordPerfButton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment