perf: use requestPostAnimationFrame to reduce layout thrashing (#1376)
This commit is contained in:
parent
98e02cf650
commit
87bab8662c
4 changed files with 36 additions and 9 deletions
|
@ -69,6 +69,7 @@
|
||||||
import { observe } from 'svelte-extras'
|
import { observe } from 'svelte-extras'
|
||||||
import { get } from '../../_utils/lodash-lite'
|
import { get } from '../../_utils/lodash-lite'
|
||||||
import { on } from '../../_utils/eventBus'
|
import { on } from '../../_utils/eventBus'
|
||||||
|
import { requestPostAnimationFrame } from '../../_utils/requestPostAnimationFrame'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
oncreate () {
|
oncreate () {
|
||||||
|
@ -117,7 +118,7 @@
|
||||||
},
|
},
|
||||||
setupAutosize () {
|
setupAutosize () {
|
||||||
const textarea = this.refs.textarea
|
const textarea = this.refs.textarea
|
||||||
requestAnimationFrame(() => {
|
requestPostAnimationFrame(() => {
|
||||||
mark('autosize()')
|
mark('autosize()')
|
||||||
autosize(textarea)
|
autosize(textarea)
|
||||||
stop('autosize()')
|
stop('autosize()')
|
||||||
|
|
|
@ -28,12 +28,13 @@
|
||||||
import { virtualListStore } from './virtualListStore'
|
import { virtualListStore } from './virtualListStore'
|
||||||
import { registerResizeListener, unregisterResizeListener } from '../../_utils/resize'
|
import { registerResizeListener, unregisterResizeListener } from '../../_utils/resize'
|
||||||
import { mark, stop } from '../../_utils/marks'
|
import { mark, stop } from '../../_utils/marks'
|
||||||
|
import { requestPostAnimationFrame } from '../../_utils/requestPostAnimationFrame'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
oncreate () {
|
oncreate () {
|
||||||
const { key } = this.get()
|
const { key } = this.get()
|
||||||
const node = this.refs.node
|
const node = this.refs.node
|
||||||
requestAnimationFrame(() => {
|
requestPostAnimationFrame(() => {
|
||||||
if (!node || !key) {
|
if (!node || !key) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
import { Store } from 'svelte/store.js'
|
import { Store } from 'svelte/store.js'
|
||||||
import QuickLRU from 'quick-lru'
|
import QuickLRU from 'quick-lru'
|
||||||
import { mark, stop } from './marks'
|
import { mark, stop } from './marks'
|
||||||
|
import { requestPostAnimationFrame } from './requestPostAnimationFrame'
|
||||||
|
|
||||||
export class RealmStore extends Store {
|
export class RealmStore extends Store {
|
||||||
constructor (init, maxSize) {
|
constructor (init, maxSize) {
|
||||||
|
@ -47,7 +48,7 @@ export class RealmStore extends Store {
|
||||||
}
|
}
|
||||||
batch[subKey] = value
|
batch[subKey] = value
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestPostAnimationFrame(() => {
|
||||||
const batch = this._batches[currentRealm] && this._batches[currentRealm][key]
|
const batch = this._batches[currentRealm] && this._batches[currentRealm][key]
|
||||||
if (!batch) {
|
if (!batch) {
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,9 +1,33 @@
|
||||||
// modeled after https://github.com/andrewiggins/afterframe
|
// modeled after https://github.com/andrewiggins/afterframe
|
||||||
// see also https://github.com/WICG/requestPostAnimationFrame
|
// see also https://github.com/WICG/requestPostAnimationFrame
|
||||||
export const requestPostAnimationFrame = cb => {
|
const channel = process.browser && new MessageChannel()
|
||||||
requestAnimationFrame(() => {
|
const callbacks = []
|
||||||
const channel = new MessageChannel()
|
|
||||||
channel.port1.onmessage = cb
|
if (process.browser) {
|
||||||
channel.port2.postMessage(undefined)
|
channel.port1.onmessage = onMessage
|
||||||
})
|
}
|
||||||
|
|
||||||
|
function runCallback (callback) {
|
||||||
|
try {
|
||||||
|
callback()
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onMessage () {
|
||||||
|
for (const callback of callbacks) {
|
||||||
|
runCallback(callback)
|
||||||
|
}
|
||||||
|
callbacks.length = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function postMessage () {
|
||||||
|
channel.port2.postMessage(undefined)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const requestPostAnimationFrame = callback => {
|
||||||
|
if (callbacks.push(callback) === 1) {
|
||||||
|
requestAnimationFrame(postMessage)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue