Actually implement event-driver timeouts.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 14 Feb 2022 16:47:57 +0000 (17:47 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 14 Feb 2022 16:47:57 +0000 (17:47 +0100)
src/jagi/event/Driver.java

index 84238d2..a8475b4 100644 (file)
@@ -59,6 +59,8 @@ public class Driver {
        }
 
        void handle(Watcher w, int evs) {
+           if(!watching.containsKey(w))
+               return;
            try {
                pause(w);
                submit(() -> {
@@ -88,8 +90,8 @@ public class Driver {
            boolean quit = false;
            Throwable error = null;
            try {
+               double now = time();
                while(true) {
-                   double now = time();
                    long timeout = 0;
                    synchronized(selectors) {
                        Double first = timeheap.keypeek();
@@ -109,6 +111,13 @@ public class Driver {
                    }
                    for(SelectionKey key : poll.selectedKeys())
                        handle((Watcher)key.attachment(), key.readyOps());
+                   now = time();
+                   while(true) {
+                       Double first = timeheap.keypeek();
+                       if((first == null) || (first > now))
+                           break;
+                       handle(timeheap.remove(), 0);
+                   }
                }
            } catch(Throwable t) {
                error = t;