rocket-connect / for-emit-of

Turn Node.js Events into Async Iterables.
https://www.npmjs.com/package/for-emit-of
MIT License
9 stars 2 forks source link

Solving uncomplying timeout in some cases #12

Closed Farenheith closed 4 years ago

Farenheith commented 4 years ago

Closes #11

What problem I found?

The problem happened because the timeout was updating the deadline only when Promise.race was used. This happens only if no new event is generated (events.length === 0). If, in such case, the operation of the for loop demand too much time, this time was being considered for the timeout calculation, as the last deadline was calculated before that.

Solution

I removed the updateDeadline function from the TimeoutWrapper and created a Context interface. This interface will have the last moment a result had being emitted as it is updated at the event listener function. Finally, this context is passed to the timeout control function and is used to calculate the real deadline