Comments (9)
Yes it was.
I'm not opposed to nextTick. What would be your reason or use cases?
from tiny-emitter.
(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.
Let me think about how to handle this. I'll do some research on it.
from tiny-emitter.
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.
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.
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.
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.
@scottcorgan what about an optional init-time decision?
var emitter = new Emitter({
emitOnNextTick: true // default: false
});
from tiny-emitter.
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)
- off doesn't unbind once HOT 3
- Remove Testling badge and set up some other ci
- Clarify the relevant license terms (MIT?) for this project. HOT 3
- Annoying to use with ES6 imports and Browserify HOT 6
- Remove bower support
- Proposal for version 3.0 HOT 9
- main point to dist instead of the root? HOT 3
- TinyEmitter can't be imported in TypeScript with the normal module import syntax HOT 5
- Add support for ES6 module import HOT 2
- Is there any way to listen to all events irrespective to event name? HOT 3
- Listner event with regex HOT 1
- Multiple on events only first one firing
- can I got all event name?
- Feature: Add Persistent to Event
- Emitter expression is not constructable HOT 1
- Callback logic HOT 1
- Check whether callback is actually a function
- Implement inversion of control flavors of on(), once(), and off() HOT 4
- Push up to date version to bower HOT 1
- Make .on and .once return unsubscribe functions HOT 4
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 tiny-emitter.