mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
batch update head contents (#129)
* head-manager: batch update * side-effect: remove redundant rendering cycle checks
This commit is contained in:
parent
93d856706b
commit
037e2d9c26
|
@ -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 {
|
||||||
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
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
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
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue