-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Event pooling + async decorators #10
Comments
I currently work around this potential problem like so: import React from "react"
import {debounce} from "react-decoration"
class MyComponent extends React.PureComponent {
render() {
const {value} = this.props
return <input
type="text"
value={value}
onChange={this.onChange}
/>
}
onChange = event => {
// No need to call `event.persist()` because we immediately retrieve the value.
const {target: {value}} = event
this.fetch(value)
}
@debounce(200)
fetch(value) {
const {fetchData} = this.props
fetchData(value)
}
}
export {Search}
export default Search |
Hi @jneuendorf,
I've tested them and it seems that, unfortunately, they have the same problem as underscore or lodash with the instances of the component.
Yeah, because I always use them to handle user interactions (I used to decorate
I think that we should add some tests and fix this issue. Let me know if you are interested in contributing 😄 |
Hey @mbasso, I just realized that debouncing an event handler is actually a special case because any function can be debounced. So I guess my "workaround from above" would be a valid solution. Other convenient ways I can think of are
I guess the last option is the best because it provides most flexibility, is explicit and easy to implement (using your Anyway, I would like to contribute stuff ;) |
Yeah, I think that the last option would be fine, in this way we can use it both with or without the
Awesome! Thank you so much for your interest in this library! 😄
Sure. I think that we should write a test like this and also a test to solve the following problem with the instance (from you link):
|
Hey, I have done some research (https://www.youtube.com/watch?v=dRo_egw7tBc) on event pooling but could not get the event to be released to the pool. This might be related to Since event pooling is done for performance reasons it might not be a good idea to persist the event unless one actually needs a reference to the instance. Thus it would be better to use I have not looked into the multiple-instances-share-debounced-method problem yet ;) |
The real DOM in the browser causes event pooling to behave like in the React docs. Locally both versions of an @persistEvent
@debounce(100)
onChange(event) {
console.log(event.type);
} @extractValue
@debounce(100)
onChange(value) {
console.log(value);
} |
Cool, it looks good to me, I think that this is the right way to do that. We should certainly clarify this in the documentation.
Awesome, it's perfect! Exactly what I had in mind
I'll try to create a snippet of this issue in the next few days. I think that this it might be not to easy to fix, it might create some problems... We should try the 3 solutions proposed on stackoverflow, from the first to the last one. |
Wouldn't it be sufficient to internally use the
What do you mean by Update It might really be a bit difficult to have 1 debounce function per instance because The trivial solution would be changing all decorators to do their wrapping at runtime but this is less performant, of course. 😕 Update 2 The non-trivial solution would be detecting whether the descriptor has a getter or a value. This would be quite a refactoring, I guess. |
I'm sorry for my late reply but I have been a little bit busy in last days...
yeah, just a PR to see the progress and review the code together. It's also fine to open it once stuff is getting close to being final, as you said. As you prefer
This seems interesting, I think that it is certainly something that needs to be done... It might involve some changes in the codebase and new tests but it will represent a big improvement for the library. The only solution that I had in mind was the third: var SearchBox = React.createClass({
method: function() {...},
componentWillMount: function() {
this.method = debounce(this.method,100);
},
}); but class SearchBox extends React.Component {
constructor(props) {
super(props);
this.method = debounce(this.method,1000);
}
method() { ... }
} but I think that it will break decorator chaining as well. I have no other ideas in mind at the moment, it seems that your non-trivial solution is the best. |
Hi @jneuendorf, |
@mbasso Sorry I haven't been working on this lately...I was busy with other projects but I haven't forgotten about this! 😉 I'll open the WIP PR within the next couple days |
@mbasso I've done it! 😄 Though, I haven't written any tests for the new debounce yet - just jotted it down before going to bed 😉 |
@jneuendorf awesome! |
General Information
Description
I have a question. According to https://stackoverflow.com/a/28046731/6928824 event pooling may be a problem for the
throttle
/debounce
decorators. In your README example you use thedebounce
decorator for event handling so I guess it has worked for you so far.So wouldn't it be better to call
event.persist()
before running the decorated function (I haven't found any code doing that)?Maybe your example works because the event is cleaned after a longer time - so
debounce(10e4)
would not work...I haven't played around it yet.Versions
The text was updated successfully, but these errors were encountered: