Comments (8)
Definitely down to discuss this, in the meantime just in case you hadn't thought of it you can work around this:
class EventHelper() {
cancelChange() {
this.triggerChangeDebounced.cancel();
}
@autobind
triggerChange() {
this.triggerChangeDebounced(this);
}
@decorate(_.debounce, 100)
triggerChangeDebounced(context) {
// do something with context, like context.kickDog()
}
kickDog() {}
}
Let me think through some syntax options for cancel support.
from core-decorators.
@jayphelps thanks for your response,
I looked up through my code and noticed that I need to .cancel
delayed execution only when i destroying classes (in other cases it can be done without using @decorate
)
So if you want to keep it simple and easy for people to use i think you can adopt clearTimeout
function syntax:
import {debounce, clearTimeouts} from 'core-decorators';
class Bar {
@debounce(1000)
baz() {
// do something
}
}
const foo = {
bar: new Bar(),
bas: new Bar()
};
foo.bar.baz();
foo.bas.baz();
delete foo.bar;
delete foo.bas;
clearTimeouts(foo.bar);
Here foo.bar.baz
- will be canceled, foo.bas.baz
will be executed.
clearTimeouts
- will iterate through instance meta
and clear every meta timeout id
I think it's will be easiest way to implement and use
from core-decorators.
+1 Also wanted to cancel() debounce after React component unmount (isMounted() is deprecated!). Cant use clearTimeout, because youre hiding your metas behind non-exported Symbol.
from core-decorators.
Is a cancel()
function on the debounced method ideal? I'm happy with that.
class Example extends Component {
@debounce(1000)
doStuff() {
// do stuff
}
componentWillUnmount() {
this.doStuff.cancel();
}
}
from core-decorators.
@nehaleem also--in the meantime if you're also already using lodash, you can accomplish this with the @decorate
decorator and lodash's _.debounce
:
class Example extends Component {
@decorate(_.debounce, 1000)
doStuff() {
// do stuff
}
componentWillUnmount() {
this.doStuff.cancel();
}
}
If you need autobinding too, you can do this:
class Example extends Component {
@autobind
doStuff() {
this.doStuffDebounced();
}
@decorate(_.debounce, 1000)
doStuffDebounced() {
// do stuff
}
componentWillUnmount() {
this.doStuffDebounced.cancel();
}
}
from core-decorators.
@jayphelps Yeah, thats what i used for now, with future "//TODO" 😄
from core-decorators.
This turns out to be a non-trivial thing to do.
The issue is around making it so that the cancel function is unique for each instance of that class so it does not cancel all debounces for every instance.
This is a similar problem that @decorate
had and I sort of solve by generating a new function for each instance, the first time you look up that property: https://github.com/jayphelps/core-decorators.js/blob/master/src/decorate.js
I may try to do that here, but just a heads up that it probably will take some time..
from core-decorators.
There are no "elegant" ways to do it with decorators now, so i end up with:
class EventHelper() {
cancelChange() {
this.triggerChange.cancel()
}
triggerChange = _.debounce(() => {
// do something
}, 100)
}
Anyways, thanks you @jayphelps 👍
from core-decorators.
Related Issues (20)
- Unexpected token import HOT 3
- Why does test/unit/extendDescriptor.js not have .spec in its name? HOT 3
- Add tslint to the project
- Check-in lib & esm folders? HOT 2
- Typescript generates enumerable methods HOT 2
- Autobind decorator HOT 2
- Issue with Redux passing {type, foo} as Action parameter HOT 2
- Tree shaking seems not working. HOT 5
- Decorators for validations HOT 2
- @decorateMethod HOT 3
- Why is debounce and throttle depracated HOT 2
- Why is mixin deprecated? HOT 2
- docs: README shows wrong build status badge HOT 1
- Issues with readonly and enumreable HOT 4
- Cannot resolve symbol 'applyDecorators' Validate ES6 imports HOT 5
- Explain applyDecorators() for a function prototype
- Support Node 10 ECMAScript Modules HOT 1
- suppressWarnings decorator function cannot suppress deprecated decorator function call console.warn output
- core-decorators broken with react-native 0.57.1 and babel 7.0.0. HOT 4
- Override reports error when not all intermediate classes implement method
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core-decorators.