aboutsummaryrefslogtreecommitdiffstats
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
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.
-rw-r--r--src/local/client.nim9
-rw-r--r--src/local/container.nim23
-rw-r--r--src/server/buffer.nim18
3 files changed, 30 insertions, 20 deletions
diff --git a/src/local/client.nim b/src/local/client.nim
index baea712f..7821eb23 100644
--- a/src/local/client.nim
+++ b/src/local/client.nim
@@ -432,7 +432,6 @@ proc showConsole(client: Client) {.jsfunc.} =
if client.pager.container != container:
client.consoleWrapper.prev = client.pager.container
client.pager.setContainer(container)
- container.requestLines()
proc hideConsole(client: Client) {.jsfunc.} =
if client.pager.container == client.consoleWrapper.container:
@@ -582,8 +581,9 @@ proc inputLoop(client: Client) =
if selectors.Event.Timer in event.events:
let r = client.timeouts.runTimeoutFd(event.fd)
assert r
- client.pager.container.requestLines().then(proc() =
- client.pager.container.cursorLastLine())
+ let container = client.consoleWrapper.container
+ if container != nil:
+ container.tailOnLoad = true
client.runJSJobs()
client.loader.unregistered.setLen(0)
client.acceptBuffers()
@@ -671,10 +671,9 @@ proc addConsole(pager: Pager, interactive: bool, clearFun, showFun, hideFun:
let url = newURL("stream:console").get
let container = pager.readPipe0("text/plain", CHARSET_UNKNOWN, pipefd[0],
some(url), ConsoleTitle, canreinterpret = false)
+ pager.registerContainer(container)
let err = newPosixStream(pipefd[1])
err.writeLine("Type (M-c) console.hide() to return to buffer mode.")
- err.flush()
- pager.registerContainer(container)
let console = newConsole(
err,
clearFun = clearFun,
diff --git a/src/local/container.nim b/src/local/container.nim
index da33a9fb..88848ac7 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -88,6 +88,9 @@ type
BufferFilter* = ref object
cmd*: string
+ LoadState = enum
+ lsLoading, lsCanceled, lsLoaded
+
Container* = ref object
# note: this is not the same as source.request.url (but should be synced
# with buffer.url)
@@ -123,7 +126,7 @@ type
hlon*: bool # highlight on?
sourcepair*: Container # pointer to buffer with a source view (may be nil)
needslines*: bool
- canceled: bool
+ loadState: LoadState
events*: Deque[ContainerEvent]
startpos: Option[CursorPosition]
hasstart: bool
@@ -229,7 +232,7 @@ proc clone*(container: Container, newurl: URL): Promise[Container] =
retry: container.retry,
hlon: container.hlon,
#needslines: container.needslines,
- canceled: container.canceled,
+ loadState: container.loadState,
events: container.events,
startpos: container.startpos,
hasstart: container.hasstart,
@@ -469,12 +472,13 @@ proc setNumLines(container: Container, lines: int, finish = false) =
proc cursorLastLine*(container: Container)
-proc requestLines*(container: Container, w = container.lineWindow): EmptyPromise
+proc requestLines(container: Container): EmptyPromise
{.discardable.} =
if container.iface == nil:
let res = EmptyPromise()
res.resolve()
return res
+ let w = container.lineWindow
return container.iface.getLines(w).then(proc(res: GetLinesResult) =
container.lines.setLen(w.len)
container.lineshift = w.a
@@ -486,7 +490,8 @@ proc requestLines*(container: Container, w = container.lineWindow): EmptyPromise
container.bgcolor = res.bgcolor
if res.numLines != container.numLines:
container.setNumLines(res.numLines, true)
- container.triggerEvent(STATUS)
+ if container.loadState != lsLoading:
+ container.triggerEvent(STATUS)
if res.numLines > 0:
container.updateCursor()
if container.tailOnLoad:
@@ -1369,12 +1374,13 @@ proc setLoadInfo(container: Container, msg: string) =
#TODO this should be called with a timeout.
proc onload*(container: Container, res: int) =
- if container.canceled:
+ if container.loadState == lsCanceled:
container.setLoadInfo("")
container.iface.cancel().then(proc() =
container.needslines = true
)
elif res == -1:
+ container.loadState = lsLoaded
container.setLoadInfo("")
container.triggerEvent(STATUS)
container.needslines = true
@@ -1390,10 +1396,9 @@ proc onload*(container: Container, res: int) =
let res = res.get
container.setCursorXYCenter(res.x, res.y)
)
- elif res == -2:
- container.setLoadInfo(convertSize(res) & " loaded")
else:
container.needslines = true
+ container.setLoadInfo(convertSize(res) & " loaded")
discard container.iface.load().then(proc(res: int) =
container.onload(res)
)
@@ -1468,8 +1473,8 @@ proc quit*(container: Container) =
proc cancel*(container: Container) {.jsfunc.} =
if container.select.open:
container.select.cancel()
- else:
- container.canceled = true
+ elif container.loadState == lsLoading:
+ container.loadState = lsCanceled
container.alert("Canceled loading")
proc findAnchor*(container: Container, anchor: string) =
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: