about summary refs log tree commit diff stats
path: root/src/html
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/html
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/html')
-rw-r--r--src/html/dom.nim29
-rw-r--r--src/html/env.nim2
2 files changed, 19 insertions, 12 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 3d901eb9..20b10674 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -15,9 +15,10 @@ import encoding/decoderstream
 import html/tags
 import img/bitmap
 import img/painter
-import img/png
 import img/path
+import img/png
 import io/loader
+import io/promise
 import io/request
 import io/window
 import js/javascript
@@ -2371,26 +2372,30 @@ proc createClassicScript(source: string, baseURL: URL, options: ScriptOptions, m
 
 #TODO settings object
 proc fetchClassicScript(element: HTMLScriptElement, url: URL,
-                        options: ScriptOptions, cors: CORSAttribute,
-                        cs: Charset, onComplete: (proc(element: HTMLScriptElement,
-                                                       res: ScriptResult))) =
+    options: ScriptOptions, cors: CORSAttribute,
+    cs: Charset, onComplete: (proc(element: HTMLScriptElement,
+                                   res: ScriptResult))) =
   if not element.scriptingEnabled:
       element.onComplete(ScriptResult(t: RESULT_NULL))
   else:
     let loader = element.document.window.loader
     if loader.isSome:
       let request = createPotentialCORSRequest(url, RequestDestination.SCRIPT, cors)
-      #TODO this should be async...
-      let r = loader.get.doRequest(request)
-      if r.res != 0 or r.body == nil:
-        element.onComplete(ScriptResult(t: RESULT_NULL))
-      else:
-        #TODO use charset from content-type
+      loader.get.fetch(request).then(proc(r: Response): auto =
+        if r.res != 0 or r.body == nil:
+          #TODO remove res
+          element.onComplete(ScriptResult(t: RESULT_NULL))
+        else:
+          #TODO use charset from content-type
+          #TODO text() should decode
+          return r.text()
+      ).then(proc(s: string) =
+        let ss = newStringStream(s) #TODO unnecessary copy
         let cs = if cs == CHARSET_UNKNOWN: CHARSET_UTF_8 else: cs
-        let source = newDecoderStream(r.body, cs = cs).readAll()
+        let source = newDecoderStream(ss, cs = cs).readAll()
         #TODO use response url
         let script = createClassicScript(source, url, options, false)
-        element.markAsReady(ScriptResult(t: RESULT_SCRIPT, script: script))
+        element.markAsReady(ScriptResult(t: RESULT_SCRIPT, script: script)))
 
 proc log*(console: console, ss: varargs[string]) {.jsfunc.} =
   var s = ""
diff --git a/src/html/env.nim b/src/html/env.nim
index ca7e497d..5883dabf 100644
--- a/src/html/env.nim
+++ b/src/html/env.nim
@@ -6,6 +6,7 @@ import html/htmlparser
 import io/loader
 import io/promise
 import io/request
+import io/response
 import io/window
 import js/intl
 import js/javascript
@@ -120,6 +121,7 @@ proc addScripting*(window: Window, selector: Selector[int]) =
   ctx.addBlobModule()
   ctx.addFormDataModule()
   ctx.addRequestModule()
+  ctx.addResponseModule()
 
 proc runJSJobs*(window: Window) =
   window.jsrt.runJSJobs(window.console.err)