about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-09-14 21:20:02 +0200
committerbptato <nincsnevem662@gmail.com>2024-09-14 21:21:34 +0200
commit11d102e2ba16f8f4a9976ab81d35b22da5c0ad2f (patch)
tree4f2296c51973bc6a14dcabe7c366f9a7023b7589
parent5b2a36579e53c69f154288a91ddc3e7c5375d7a6 (diff)
downloadchawan-11d102e2ba16f8f4a9976ab81d35b22da5c0ad2f.tar.gz
loader: improve map interface
directly accessing map was a bit too error prone
-rw-r--r--src/loader/loader.nim11
-rw-r--r--src/local/client.nim2
-rw-r--r--src/server/buffer.nim34
3 files changed, 25 insertions, 22 deletions
diff --git a/src/loader/loader.nim b/src/loader/loader.nim
index 0816c6f8..2b846fb5 100644
--- a/src/loader/loader.nim
+++ b/src/loader/loader.nim
@@ -60,7 +60,7 @@ type
     key*: ClientKey
     process*: int
     clientPid*: int
-    map*: seq[LoaderData]
+    map: seq[LoaderData]
     mapFds*: int # number of fds in map
     unregistered*: seq[int]
     registerFun*: proc(fd: int)
@@ -1060,9 +1060,14 @@ proc startRequest*(loader: FileLoader; request: Request;
     w.swrite(config)
   return stream
 
-iterator ongoing*(loader: FileLoader): OngoingData =
+iterator data*(loader: FileLoader): LoaderData {.inline.} =
   for it in loader.map:
-    if it != nil and it of OngoingData:
+    if it != nil:
+      yield it
+
+iterator ongoing*(loader: FileLoader): OngoingData {.inline.} =
+  for it in loader.data:
+    if it of OngoingData:
       yield OngoingData(it)
 
 func fd*(data: LoaderData): int =
diff --git a/src/local/client.nim b/src/local/client.nim
index e96b5163..b8e131f8 100644
--- a/src/local/client.nim
+++ b/src/local/client.nim
@@ -516,7 +516,7 @@ proc handleError(client: Client; fd: int) =
     #TODO do something here...
     stderr.write("Fork server crashed :(\n")
     client.quit(1)
-  elif client.loader.map[fd] != nil:
+  elif client.loader.get(fd) != nil:
     discard client.loader.onError(fd) #TODO handle connection error?
   elif fd in client.loader.unregistered:
     discard # already unregistered...
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index ea17cbfc..66a63b6a 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -975,18 +975,17 @@ proc clone*(buffer: Buffer; newurl: URL): int {.proxy.} =
     #TODO set buffer.window.timeouts.selector
     var connecting: seq[ConnectData] = @[]
     var ongoing: seq[OngoingData] = @[]
-    for it in buffer.loader.map:
-      if it != nil:
-        if it of ConnectData:
-          connecting.add(ConnectData(it))
-        else:
-          let it = OngoingData(it)
-          ongoing.add(it)
-          it.response.body.sclose()
-        buffer.loader.unregistered.add(it.fd)
-        buffer.loader.unset(it)
+    for it in buffer.loader.data:
+      if it of ConnectData:
+        connecting.add(ConnectData(it))
+      else:
+        let it = OngoingData(it)
+        ongoing.add(it)
+        it.response.body.sclose()
+      buffer.loader.unregistered.add(it.fd)
+      buffer.loader.unset(it)
     let myPid = getCurrentProcessId()
-    for it in ongoing.mitems:
+    for it in ongoing:
       let response = it.response
       # tee ongoing streams
       let (stream, outputId) = buffer.loader.tee(response.outputId, myPid)
@@ -1175,13 +1174,12 @@ proc forceRender*(buffer: Buffer) {.proxy.} =
 proc cancel*(buffer: Buffer) {.proxy.} =
   if buffer.state == bsLoaded:
     return
-  for it in buffer.loader.map:
-    if it != nil:
-      let fd = it.fd
-      buffer.selector.unregister(fd)
-      buffer.loader.unregistered.add(fd)
-      it.stream.sclose()
-      buffer.loader.unset(it)
+  for it in buffer.loader.data:
+    let fd = it.fd
+    buffer.selector.unregister(fd)
+    buffer.loader.unregistered.add(fd)
+    it.stream.sclose()
+    buffer.loader.unset(it)
   if buffer.istream != nil:
     buffer.selector.unregister(buffer.fd)
     buffer.loader.unregistered.add(buffer.fd)