Commit 037e2d9c authored by Naoyuki Kanezawa's avatar Naoyuki Kanezawa Committed by GitHub

batch update head contents (#129)

* head-manager: batch update

* side-effect: remove redundant rendering cycle checks
parent 93d85670
...@@ -3,7 +3,20 @@ import HTMLDOMPropertyConfig from 'react/lib/HTMLDOMPropertyConfig' ...@@ -3,7 +3,20 @@ import HTMLDOMPropertyConfig from 'react/lib/HTMLDOMPropertyConfig'
const DEFAULT_TITLE = '' const DEFAULT_TITLE = ''
export default class HeadManager { export default class HeadManager {
constuctor () {
this.requestId = null
}
updateHead (head) { updateHead (head) {
// perform batch update
window.cancelAnimationFrame(this.requestId)
this.requestId = window.requestAnimationFrame(() => {
this.requestId = null
this.doUpdateHead(head)
})
}
doUpdateHead (head) {
const tags = {} const tags = {}
head.forEach((h) => { head.forEach((h) => {
const components = tags[h.type] || [] const components = tags[h.type] || []
......
...@@ -9,10 +9,6 @@ class Head extends React.Component { ...@@ -9,10 +9,6 @@ class Head extends React.Component {
render () { render () {
return null return null
} }
componentWillUnmount () {
this.context.headManager.updateHead([])
}
} }
function reduceComponents (components) { function reduceComponents (components) {
......
...@@ -24,7 +24,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan ...@@ -24,7 +24,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
const mountedInstances = new Set() const mountedInstances = new Set()
let state let state
let shouldEmitChange = false
function emitChange (component) { function emitChange (component) {
state = reduceComponentsToState([...mountedInstances]) state = reduceComponentsToState([...mountedInstances])
...@@ -36,12 +35,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan ...@@ -36,12 +35,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
} }
} }
function maybeEmitChange (component) {
if (!shouldEmitChange) return
shouldEmitChange = false
emitChange(component)
}
class SideEffect extends Component { class SideEffect extends Component {
// Try to use displayName of wrapped component // Try to use displayName of wrapped component
static displayName = `SideEffect(${getDisplayName(WrappedComponent)})` static displayName = `SideEffect(${getDisplayName(WrappedComponent)})`
...@@ -60,8 +53,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan ...@@ -60,8 +53,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
throw new Error('You may only call rewind() on the server. Call peek() to read the current state.') throw new Error('You may only call rewind() on the server. Call peek() to read the current state.')
} }
maybeEmitChange()
const recordedState = state const recordedState = state
state = undefined state = undefined
mountedInstances.clear() mountedInstances.clear()
...@@ -70,24 +61,15 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan ...@@ -70,24 +61,15 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
componentWillMount () { componentWillMount () {
mountedInstances.add(this) mountedInstances.add(this)
shouldEmitChange = true emitChange(this)
}
componentDidMount () {
maybeEmitChange(this)
}
componentWillUpdate () {
shouldEmitChange = true
} }
componentDidUpdate () { componentDidUpdate () {
maybeEmitChange(this) emitChange(this)
} }
componentWillUnmount () { componentWillUnmount () {
mountedInstances.delete(this) mountedInstances.delete(this)
shouldEmitChange = false
emitChange(this) emitChange(this)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment