about summary refs log tree commit diff stats
path: root/src/server
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-03 17:33:09 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-03 17:33:09 +0100
commitb4a0b79cacefdb58d556aec8ee75d6f1f7a0be49 (patch)
tree5cdf6dfafcb6ad90e64b5ad2f4f44b0ef7178ead /src/server
parent6776f4dba975137f4034b1295c0f1958b752a2cb (diff)
downloadchawan-b4a0b79cacefdb58d556aec8ee75d6f1f7a0be49.tar.gz
buffer: improve/fix onload return values
Only report when bytesRead has changed, otherwise we get unnecessary
load requests. (This means -2 return value no longer exists; it did
not work correctly anyway.)

Also, fix the race condition that broke onload returns when onload
happened before client requested load.
Diffstat (limited to 'src/server')
-rw-r--r--src/server/buffer.nim18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index dced572f..32592076 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -98,6 +98,7 @@ type
     selector: Selector[int]
     istream: SocketStream
     bytesRead: int
+    reportedBytesRead: int
     state: BufferState
     prevnode: StyledNode
     loader: FileLoader
@@ -1164,14 +1165,19 @@ proc finishLoad(buffer: Buffer): EmptyPromise =
 
 # Returns:
 # * -1 if loading is done
-# * -2 if the page was partially rendered
-# * a positive number for just reporting the number of bytes loaded.
+# * a positive number for reporting the number of bytes loaded and that the page
+#   has been partially rendered.
 proc load*(buffer: Buffer): int {.proxy, task.} =
   if buffer.state == bsLoaded:
     return -1
+  elif buffer.bytesRead > buffer.reportedBytesRead:
+    buffer.do_reshape()
+    buffer.reportedBytesRead = buffer.bytesRead
+    return buffer.bytesRead
   else:
     # will be resolved in onload
     buffer.savetask = true
+    return -2 # unused
 
 proc resolveTask[T](buffer: Buffer, cmd: BufferCommand, res: T) =
   let packetid = buffer.tasks[cmd]
@@ -1219,15 +1225,15 @@ proc onload(buffer: Buffer) =
           buffer.resolveTask(bcLoad, -1)
         )
         return # skip incr render
-      buffer.resolveTask(bcLoad, buffer.bytesRead)
     except ErrorAgain:
       break
   # incremental rendering: only if we cannot read the entire stream in one
   # pass
-  if not buffer.config.isdump:
-    # only makes sense when not in dump mode
+  if not buffer.config.isdump and buffer.tasks[bcLoad] != 0:
+    # only makes sense when not in dump mode (and the user has requested a load)
     buffer.do_reshape()
-    buffer.resolveTask(bcLoad, -2)
+    buffer.reportedBytesRead = buffer.bytesRead
+    buffer.resolveTask(bcLoad, buffer.bytesRead)
 
 proc getTitle*(buffer: Buffer): string {.proxy.} =
   if buffer.document != nil: