Giter Club home page Giter Club logo

Comments (9)

scottcorgan avatar scottcorgan commented on August 24, 2024

Yes it was.

I'm not opposed to nextTick. What would be your reason or use cases?

from tiny-emitter.

xixixao avatar xixixao commented on August 24, 2024

(setTimeout 1) Well, they are two very distinct behaviors. I quickly tried to search for some good practices, but I'd say it is simply easier to reason about events if they are emitted after the current execution stream has finished.

from tiny-emitter.

scottcorgan avatar scottcorgan commented on August 24, 2024

Let me think about how to handle this. I'll do some research on it.

from tiny-emitter.

silverbucket avatar silverbucket commented on August 24, 2024

You also don't block the call stack any more then necessary by using setTimeout(func, 0);. It's generally nicer to do this, adding it to the queue to be processed after the call stack has completed.

Consider the case of looping through an array of thousands of records. For every record we need to emit an event. If we are to emit the events immediately then the entire loop will slow down and execute the library logic each time, slowing down the loop considerably.

Alternatively, with a setTimeout the loop will only queue the process and continue through the loop much faster. The events are still emitted near immediately, they just don't block the loop and clog up the call stack.

I'm very much in favor of this behavior as well.

from tiny-emitter.

ayamflow avatar ayamflow commented on August 24, 2024

Shouldn't that be up to the user to emit on the next frame if needed ?
I'm thinking with other use cases in mind when one wants the listeners to trigger right after the event was emitted.

A simple example would be a backbone view with "life cycle events" like beforeRender/render/afterRender when you want each listener to fire before the next hook.

from tiny-emitter.

Prinzhorn avatar Prinzhorn commented on August 24, 2024

I'm with @ayamflow here. I trigger an event for every requestAnimationFrame with some custom data and every artificial delay would be undesirable.

from tiny-emitter.

scottcorgan avatar scottcorgan commented on August 24, 2024

All good points. It's been the way it is for quite some time. Going to close in favor of keeping current functionality.

Open for more discussion thought.

from tiny-emitter.

silverbucket avatar silverbucket commented on August 24, 2024

@scottcorgan what about an optional init-time decision?

var emitter = new Emitter({
  emitOnNextTick: true   // default: false
});

from tiny-emitter.

scottcorgan avatar scottcorgan commented on August 24, 2024

I do think it's an interesting feature, but I think wrapping tiny-emitter with that feature may be a better approach. Keeps it tiny. It's fairly easy to extend the class and overwrite the emit method in a new library.

class TinyEmitterNextTick extends TinyEmitter {
  constructor () {
    super()
  }

  emit (...args) {
    process.nextTick(() => super.emit(...args))
  }
}

let emitter = new TinyEmitterNextTick()

// Use the tiny emitter api as normal

from tiny-emitter.

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.