about summary refs log tree commit diff stats
path: root/src/buffer/buffer.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-15 01:14:48 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-15 01:14:48 +0200
commit36390a564a191a92d470bb650e693546623774af (patch)
treeff3b0a218ad614d0ccefb90c5a813a95f6da52eb /src/buffer/buffer.nim
parentc13a4cf4e144f7ab36167ebbb1333f7faeed11b3 (diff)
downloadchawan-36390a564a191a92d470bb650e693546623774af.tar.gz
Cleanups & bug fixes involving promises
Now a promise returning nil doesn't just leave the rest of the then
chain hanging. Hooray.
Diffstat (limited to 'src/buffer/buffer.nim')
-rw-r--r--src/buffer/buffer.nim19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/buffer/buffer.nim b/src/buffer/buffer.nim
index 7a51792a..6c2a8eda 100644
--- a/src/buffer/buffer.nim
+++ b/src/buffer/buffer.nim
@@ -28,7 +28,6 @@ import img/png
 import io/loader
 import io/posixstream
 import io/promise
-import io/request
 import io/teestream
 import io/window
 import ips/serialize
@@ -559,8 +558,14 @@ proc loadResource(buffer: Buffer, elem: HTMLLinkElement): EmptyPromise =
       let media = parseMediaQueryList(parseListOfComponentValues(newStringStream(media)))
       if not media.applies(document.window): return
     return buffer.loader.fetch(newRequest(url)).then(proc(res: Response) =
-      if res.contenttype == "text/css":
-        elem.sheet = parseStylesheet(res.body))
+      if res.res == 0: #TODO remove res
+        #TODO we should use ReadableStreams for this (which would allow us to
+        # parse CSS asynchronously)
+        # yet another hack: needed because closing a stream before
+        # unregistering breaks
+        if res.contenttype == "text/css":
+          elem.sheet = parseStylesheet(res.body)
+        res.unregisterFun())
 
 proc loadResource(buffer: Buffer, elem: HTMLImageElement): EmptyPromise =
   let document = buffer.document
@@ -569,10 +574,12 @@ proc loadResource(buffer: Buffer, elem: HTMLImageElement): EmptyPromise =
   let url = parseURL(src, document.url.some)
   if url.isSome:
     let url = url.get
-    return buffer.loader.fetch(newRequest(url)).then(proc(res: Response) =
+    return buffer.loader.fetch(newRequest(url)).then(proc(res: Response): Promise[string] =
       if res.contenttype == "image/png":
-        let pngData = res.body.readAll()
-        elem.bitmap = fromPNG(toOpenArrayByte(pngData, 0, pngData.high)))
+        #TODO using text() for PNG is wrong
+        return res.text()
+    ).then(proc(pngData: string) =
+      elem.bitmap = fromPNG(toOpenArrayByte(pngData, 0, pngData.high)))
 
 proc loadResources(buffer: Buffer): EmptyPromise =
   let document = buffer.document