1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 18:39:51 +02:00

Flush change immediately (#1479) (#1480)

Propose to flush change immediately instead of delaying it using `setTimeout()`.

- Flushing change is only use for `props.onChange()` callback and invoked at the end of render lifecycle (didMount/didUpdate), can't find the rationale for delaying it.
- `setTimeout()` delay are not dependable for a predictive behaviour
- `onChange()` is an important callback for a controlled component, delaying it can cause race condition. e.g. any changes outside of the component lifecycle that happen between the delay of flushing change will be overridden
This commit is contained in:
Stan Chang Khin Boon
2017-12-30 03:07:55 +08:00
committed by Ian Storm Taylor
parent 23ab02626f
commit 9c07a6baa4

View File

@@ -157,17 +157,6 @@ class Editor extends React.Component {
this.flushChange()
}
/**
* When the component unmounts, clear flushTimeout if it has been set
* to avoid calling onChange after unmount.
*/
componentWillUnmount = () => {
if (this.tmp.flushTimeout) {
clearTimeout(this.tmp.flushTimeout)
}
}
/**
* Queue a `change` object, to be able to flush it later. This is required for
* when a change needs to be applied to the value, but because of the React
@@ -192,13 +181,10 @@ class Editor extends React.Component {
flushChange = () => {
const { change } = this.tmp
if (change && !this.tmp.flushTimeout) {
if (change) {
debug('flushChange', { change })
this.tmp.flushTimeout = setTimeout(() => {
delete this.tmp.change
delete this.tmp.flushTimeout
this.props.onChange(change)
})
delete this.tmp.change
this.props.onChange(change)
}
}