GithubHelp home page GithubHelp logo

Comments (8)

jayphelps avatar jayphelps commented on August 11, 2024 1

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.

avocadowastaken avatar avocadowastaken commented on August 11, 2024

@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.

nehaleem avatar nehaleem commented on August 11, 2024

+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.

jayphelps avatar jayphelps commented on August 11, 2024

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.

jayphelps avatar jayphelps commented on August 11, 2024

@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.

nehaleem avatar nehaleem commented on August 11, 2024

@jayphelps Yeah, thats what i used for now, with future "//TODO" 😄

from core-decorators.

jayphelps avatar jayphelps commented on August 11, 2024

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.

avocadowastaken avatar avocadowastaken commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.