Comments (5)
Thanks for your feedback! I was able to reproduce it and I took the opportunity to refactor / update the code and also adding some unit tests, also covering this specific bug.
Background: when emitting an event, the code was iterating through an array of callbacks. It calls callback 1/2, then it gets removed, and there is no 2/1, so other callback does not get called.
In the bug fix, when emitting an event, an internal copy of the original array is created.
Please note: if you unbind listener2 within listener1, the second callback will still be called at least once. This is intended behavior, since the (internal) order of callbacks should not affect whether the other callback will be called or not in this use case.
My update is a breaking change (when using TypeScript), it is now registerEvent<[number]>()
rather than registerEvent<(arg: number) => any>()
.
from typed-event-emitter.
Thanks for so fast reaction ❤️. I will try to test out changes next week.
As for copying the array with listener. You can consider to use Set
for keeping callbacks. It is safe for similar loops:
const list = new Set([1, 2])
list.forEach(function(val) {
list.delete(1);
console.log(val)
})
from typed-event-emitter.
Thanks for the hint. In a Set
, a value must be unique. That would mean, the same callback function can't be bound to the same event multiple times, as it is currently possible.
Question is, what would the user expect if they bind the same function twice?
I did a test with DOM event listeners to compare what they are doing: if you try to bind the same callback twice, it is simply ignored and still only called once on event.
What is your oppinion on that?
from typed-event-emitter.
I just checked EventEmitter
from events
. It has a similar behavior as the DOM event emitter you described above.
For the sake of simplicity and consistency, Set
is a better fit than Array
from typed-event-emitter.
Thanks for your feedback! :) I think I'll change the behavior then. Would be another breaking change, so it would probably become version 4.0 then.
from typed-event-emitter.
Related Issues (1)
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 typed-event-emitter.