perf: throttle keyboard input to improve responsiveness
parent
8c80e7d4e5
commit
8f6145e383
|
@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
|
||||||
|
|
||||||
import { search as icons } from '../svgs'
|
import { search as icons } from '../svgs'
|
||||||
import NimbleEmojiIndex from '../utils/emoji-index/nimble-emoji-index'
|
import NimbleEmojiIndex from '../utils/emoji-index/nimble-emoji-index'
|
||||||
|
import { throttleIdleTask } from '../utils/index'
|
||||||
|
|
||||||
export default class Search extends React.PureComponent {
|
export default class Search extends React.PureComponent {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
|
@ -15,9 +16,11 @@ export default class Search extends React.PureComponent {
|
||||||
this.data = props.data
|
this.data = props.data
|
||||||
this.emojiIndex = new NimbleEmojiIndex(this.data)
|
this.emojiIndex = new NimbleEmojiIndex(this.data)
|
||||||
this.setRef = this.setRef.bind(this)
|
this.setRef = this.setRef.bind(this)
|
||||||
this.handleChange = this.handleChange.bind(this)
|
|
||||||
this.clear = this.clear.bind(this)
|
this.clear = this.clear.bind(this)
|
||||||
this.handleKeyUp = this.handleKeyUp.bind(this)
|
this.handleKeyUp = this.handleKeyUp.bind(this)
|
||||||
|
|
||||||
|
// throttle keyboard input so that typing isn't delayed
|
||||||
|
this.handleChange = throttleIdleTask(this.handleChange.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
search(value) {
|
search(value) {
|
||||||
|
|
|
@ -189,6 +189,24 @@ function measureScrollbar() {
|
||||||
return scrollbarWidth
|
return scrollbarWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use requestIdleCallback() if available, else fall back to setTimeout().
|
||||||
|
// Throttle so as not to run too frequently.
|
||||||
|
function throttleIdleTask(func) {
|
||||||
|
const queue =
|
||||||
|
typeof requestIdleCallback === 'function' ? requestIdleCallback : setTimeout
|
||||||
|
const clear =
|
||||||
|
typeof cancelIdleCallback === 'function' ? cancelIdleCallback : clearTimeout
|
||||||
|
|
||||||
|
let id
|
||||||
|
|
||||||
|
return function throttled() {
|
||||||
|
if (id) {
|
||||||
|
clear(id)
|
||||||
|
}
|
||||||
|
id = queue(func)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
getData,
|
getData,
|
||||||
getSanitizedData,
|
getSanitizedData,
|
||||||
|
@ -197,4 +215,5 @@ export {
|
||||||
deepMerge,
|
deepMerge,
|
||||||
unifiedToNative,
|
unifiedToNative,
|
||||||
measureScrollbar,
|
measureScrollbar,
|
||||||
|
throttleIdleTask,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue