Skip to content

Commit bacc96e

Browse files
committed
eventloop cleanups
1 parent b7a21f0 commit bacc96e

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

examples/eventloop/README.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ keep the example somewhat simple. Some shortcomings include:
5959
* Buffer churn caused by allocating a new buffer for every socket read
6060
should be eliminated by reusing buffers where appropriate. Although
6161
churn doesn't increase memory footprint with reference counting, it
62-
is slower than reusing buffers and might aggravate memory fragmentation.
62+
is slower than reusing buffers and might increase memory fragmentation.
6363

6464
* There is no way to suspend reading or writing in the example. Adding
6565
them is straightforward: the poll set needs to be managed dynamically.
6666

6767
* The example uses poll() while one should use epoll() on Linux, kqueue()
6868
on BSD systems, etc.
6969

70-
* Error handling is mostly missing.
70+
* Error handling is mostly missing. Debug prints don't interact well
71+
with curses.

examples/eventloop/ecma_eventloop.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ EventLoop = {
2626
nextTimerId: 1,
2727
minimumDelay: 1,
2828
minimumWait: 1,
29+
maximumWait: 60000,
2930
maxExpirys: 10,
3031

3132
// sockets
@@ -57,7 +58,7 @@ EventLoop.getEarliestTimer = function() {
5758

5859
EventLoop.getEarliestWait = function() {
5960
var t = this.getEarliestTimer();
60-
return (t ? Math.max(this.minimumWait, t.target - Date.now()) : null);
61+
return (t ? t.target - Date.now() : null);
6162
}
6263

6364
EventLoop.insertTimer = function(timer) {
@@ -252,9 +253,15 @@ EventLoop.run = function() {
252253
*/
253254

254255
wait = this.getEarliestWait();
255-
if (!wait && poll_count === 0) {
256-
print('no active timers and no sockets to poll, exit');
257-
break;
256+
if (wait === null) {
257+
if (poll_count === 0) {
258+
print('no active timers and no sockets to poll, exit');
259+
break;
260+
} else {
261+
wait = this.maximumWait;
262+
}
263+
} else {
264+
wait = Math.min(this.maximumWait, Math.max(this.minimumWait, wait));
258265
}
259266

260267
/*

0 commit comments

Comments
 (0)