Skip to content

Commit e587b83

Browse files
committed
Update Fetch and HttpFetcher so they can be called in a web worker script as well.
1 parent 41685a8 commit e587b83

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

frontend/browser-ext/src/jsMain/kotlin/com/varabyte/kobweb/browser/http/Fetch.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.varabyte.kobweb.browser.http
22

3-
import kotlinx.browser.window
3+
import com.varabyte.kobweb.browser.coroutines.asCoroutineDispatcher
44
import kotlinx.coroutines.CompletableDeferred
5+
import kotlinx.coroutines.CoroutineDispatcher
56
import kotlinx.coroutines.CoroutineScope
6-
import kotlinx.coroutines.asCoroutineDispatcher
77
import kotlinx.coroutines.launch
88
import org.khronos.webgl.Int8Array
99
import org.khronos.webgl.get
10-
import org.w3c.dom.Window
10+
import org.w3c.dom.WindowOrWorkerGlobalScope
1111
import org.w3c.fetch.RequestInit
1212
import org.w3c.fetch.RequestRedirect
1313
import org.w3c.fetch.Response
@@ -42,8 +42,8 @@ private suspend fun Response.getBodyBytes(): ByteArray {
4242
}
4343
}
4444

45-
private fun Response.getBodyBytesAsync(result: (ByteArray) -> Unit) {
46-
CoroutineScope(window.asCoroutineDispatcher()).launch {
45+
private fun Response.getBodyBytesAsync(dispatcher: CoroutineDispatcher, result: (ByteArray) -> Unit) {
46+
CoroutineScope(dispatcher).launch {
4747
result(getBodyBytes())
4848
}
4949
}
@@ -84,7 +84,7 @@ object FetchDefaults {
8484
}
8585

8686
@Deprecated("DO NOT IGNORE. Please change to `fetchBytes` instead. This method will be modified soon in a backwards incompatible way, in order to support additional cases that the current form doesn't support.", replaceWith = ReplaceWith("fetchBytes(method, resource, headers, body, redirect, abortController)"))
87-
suspend fun Window.fetch(
87+
suspend fun WindowOrWorkerGlobalScope.fetch(
8888
method: HttpMethod,
8989
resource: String,
9090
headers: Map<String, Any>? = FetchDefaults.Headers,
@@ -104,7 +104,7 @@ suspend fun Window.fetch(
104104
* @param headers An optional map of headers to send with the request. Note: If a body is specified, the
105105
* `Content-Length` header will be automatically set. However, any headers set manually will always take precedence.
106106
*/
107-
suspend fun Window.fetchBytes(
107+
suspend fun WindowOrWorkerGlobalScope.fetchBytes(
108108
method: HttpMethod,
109109
resource: String,
110110
headers: Map<String, Any>? = FetchDefaults.Headers,
@@ -138,12 +138,12 @@ suspend fun Window.fetchBytes(
138138
requestInitDynamic["signal"] = abortController.signal
139139
}
140140

141-
val _ = window.fetch(resource, requestInit).then(
141+
val _ = fetch(resource, requestInit).then(
142142
onFulfilled = { res ->
143143
if (res.ok) {
144-
res.getBodyBytesAsync { bodyBytes -> responseBytesDeferred.complete(bodyBytes) }
144+
res.getBodyBytesAsync(this.asCoroutineDispatcher()) { bodyBytes -> responseBytesDeferred.complete(bodyBytes) }
145145
} else {
146-
res.getBodyBytesAsync { bodyBytes ->
146+
res.getBodyBytesAsync(this.asCoroutineDispatcher()) { bodyBytes ->
147147
responseBytesDeferred.completeExceptionally(ResponseException(res, bodyBytes))
148148
}
149149
}
@@ -154,7 +154,7 @@ suspend fun Window.fetchBytes(
154154
}
155155

156156
@Deprecated("DO NOT IGNORE. Please change to `tryFetchBytes` instead. This method will be modified soon in a backwards incompatible way, in order to support additional cases that the current form doesn't support.", replaceWith = ReplaceWith("tryFetchBytes(method, resource, headers, body, redirect, logOnError, abortController)"))
157-
suspend fun Window.tryFetch(
157+
suspend fun WindowOrWorkerGlobalScope.tryFetch(
158158
method: HttpMethod,
159159
resource: String,
160160
headers: Map<String, Any>? = FetchDefaults.Headers,
@@ -167,7 +167,7 @@ suspend fun Window.tryFetch(
167167
/**
168168
* Like [fetchBytes] but returns null if the fetch fails for any reason instead of throwing.
169169
*/
170-
suspend fun Window.tryFetchBytes(
170+
suspend fun WindowOrWorkerGlobalScope.tryFetchBytes(
171171
method: HttpMethod,
172172
resource: String,
173173
headers: Map<String, Any>? = FetchDefaults.Headers,

frontend/browser-ext/src/jsMain/kotlin/com/varabyte/kobweb/browser/http/HttpFetcher.kt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.varabyte.kobweb.browser.http
22

33
import kotlinx.browser.window
44
import org.w3c.dom.Window
5+
import org.w3c.dom.WindowOrWorkerGlobalScope
56
import org.w3c.fetch.RequestRedirect
67

78
/**
@@ -50,7 +51,7 @@ class AbortController {
5051
*
5152
* The class additionally exposes a `logOnError` field which globally applies to all `try...` methods.
5253
*/
53-
class HttpFetcher(private val window: Window) {
54+
class HttpFetcher(private val fetchScope: WindowOrWorkerGlobalScope) {
5455
/**
5556
* If true, when using any of the "try" methods, log any errors, if they occur, to the console.
5657
*
@@ -81,7 +82,7 @@ class HttpFetcher(private val window: Window) {
8182
headers: Map<String, Any>? = FetchDefaults.Headers,
8283
redirect: RequestRedirect? = FetchDefaults.Redirect,
8384
abortController: AbortController? = null
84-
): ByteArray = window.fetchBytes(HttpMethod.DELETE, resource, headers, body = null, redirect, abortController)
85+
): ByteArray = fetchScope.fetchBytes(HttpMethod.DELETE, resource, headers, body = null, redirect, abortController)
8586

8687
/**
8788
* Like [delete], but returns null if the request failed for any reason.
@@ -107,7 +108,7 @@ class HttpFetcher(private val window: Window) {
107108
redirect: RequestRedirect? = FetchDefaults.Redirect,
108109
abortController: AbortController? = null
109110
): ByteArray? =
110-
window.tryFetchBytes(HttpMethod.DELETE, resource, headers, body = null, redirect, logOnError, abortController)
111+
fetchScope.tryFetchBytes(HttpMethod.DELETE, resource, headers, body = null, redirect, logOnError, abortController)
111112

112113
/**
113114
* Call GET on a target resource.
@@ -132,7 +133,7 @@ class HttpFetcher(private val window: Window) {
132133
headers: Map<String, Any>? = FetchDefaults.Headers,
133134
redirect: RequestRedirect? = FetchDefaults.Redirect,
134135
abortController: AbortController? = null
135-
): ByteArray = window.fetchBytes(HttpMethod.GET, resource, headers, body = null, redirect, abortController)
136+
): ByteArray = fetchScope.fetchBytes(HttpMethod.GET, resource, headers, body = null, redirect, abortController)
136137

137138
/**
138139
* Like [get], but returns null if the request failed for any reason.
@@ -158,7 +159,7 @@ class HttpFetcher(private val window: Window) {
158159
redirect: RequestRedirect? = FetchDefaults.Redirect,
159160
abortController: AbortController? = null
160161
): ByteArray? =
161-
window.tryFetchBytes(HttpMethod.GET, resource, headers, body = null, redirect, logOnError, abortController)
162+
fetchScope.tryFetchBytes(HttpMethod.GET, resource, headers, body = null, redirect, logOnError, abortController)
162163

163164
/**
164165
* Call HEAD on a target resource.
@@ -183,7 +184,7 @@ class HttpFetcher(private val window: Window) {
183184
headers: Map<String, Any>? = FetchDefaults.Headers,
184185
redirect: RequestRedirect? = FetchDefaults.Redirect,
185186
abortController: AbortController? = null
186-
): ByteArray = window.fetchBytes(HttpMethod.HEAD, resource, headers, body = null, redirect, abortController)
187+
): ByteArray = fetchScope.fetchBytes(HttpMethod.HEAD, resource, headers, body = null, redirect, abortController)
187188

188189
/**
189190
* Like [head], but returns null if the request failed for any reason.
@@ -209,7 +210,7 @@ class HttpFetcher(private val window: Window) {
209210
redirect: RequestRedirect? = FetchDefaults.Redirect,
210211
abortController: AbortController? = null
211212
): ByteArray? =
212-
window.tryFetchBytes(HttpMethod.HEAD, resource, headers, body = null, redirect, logOnError, abortController)
213+
fetchScope.tryFetchBytes(HttpMethod.HEAD, resource, headers, body = null, redirect, logOnError, abortController)
213214

214215
/**
215216
* Call OPTIONS on a target resource.
@@ -234,7 +235,7 @@ class HttpFetcher(private val window: Window) {
234235
headers: Map<String, Any>? = FetchDefaults.Headers,
235236
redirect: RequestRedirect? = FetchDefaults.Redirect,
236237
abortController: AbortController? = null
237-
): ByteArray = window.fetchBytes(HttpMethod.OPTIONS, resource, headers, body = null, redirect, abortController)
238+
): ByteArray = fetchScope.fetchBytes(HttpMethod.OPTIONS, resource, headers, body = null, redirect, abortController)
238239

239240
/**
240241
* Like [options], but returns null if the request failed for any reason.
@@ -260,7 +261,7 @@ class HttpFetcher(private val window: Window) {
260261
redirect: RequestRedirect? = FetchDefaults.Redirect,
261262
abortController: AbortController? = null
262263
): ByteArray? =
263-
window.tryFetchBytes(HttpMethod.OPTIONS, resource, headers, body = null, redirect, logOnError, abortController)
264+
fetchScope.tryFetchBytes(HttpMethod.OPTIONS, resource, headers, body = null, redirect, logOnError, abortController)
264265

265266
/**
266267
* Call PATCH on a target resource.
@@ -289,7 +290,7 @@ class HttpFetcher(private val window: Window) {
289290
body: ByteArray? = null,
290291
redirect: RequestRedirect? = FetchDefaults.Redirect,
291292
abortController: AbortController? = null
292-
): ByteArray = window.fetchBytes(HttpMethod.PATCH, resource, headers, body, redirect, abortController)
293+
): ByteArray = fetchScope.fetchBytes(HttpMethod.PATCH, resource, headers, body, redirect, abortController)
293294

294295
/**
295296
* Like [patch], but returns null if the request failed for any reason.
@@ -317,7 +318,7 @@ class HttpFetcher(private val window: Window) {
317318
redirect: RequestRedirect? = FetchDefaults.Redirect,
318319
abortController: AbortController? = null
319320
): ByteArray? =
320-
window.tryFetchBytes(HttpMethod.PATCH, resource, headers, body, redirect, logOnError, abortController)
321+
fetchScope.tryFetchBytes(HttpMethod.PATCH, resource, headers, body, redirect, logOnError, abortController)
321322

322323
/**
323324
* Call POST on a target resource.
@@ -346,7 +347,7 @@ class HttpFetcher(private val window: Window) {
346347
body: ByteArray? = null,
347348
redirect: RequestRedirect? = FetchDefaults.Redirect,
348349
abortController: AbortController? = null
349-
): ByteArray = window.fetchBytes(HttpMethod.POST, resource, headers, body, redirect, abortController)
350+
): ByteArray = fetchScope.fetchBytes(HttpMethod.POST, resource, headers, body, redirect, abortController)
350351

351352
/**
352353
* Like [post], but returns null if the request failed for any reason.
@@ -374,7 +375,7 @@ class HttpFetcher(private val window: Window) {
374375
redirect: RequestRedirect? = FetchDefaults.Redirect,
375376
abortController: AbortController? = null
376377
): ByteArray? =
377-
window.tryFetchBytes(HttpMethod.POST, resource, headers, body, redirect, logOnError, abortController)
378+
fetchScope.tryFetchBytes(HttpMethod.POST, resource, headers, body, redirect, logOnError, abortController)
378379

379380
/**
380381
* Call PUT on a target resource.
@@ -403,7 +404,7 @@ class HttpFetcher(private val window: Window) {
403404
body: ByteArray? = null,
404405
redirect: RequestRedirect? = FetchDefaults.Redirect,
405406
abortController: AbortController? = null
406-
): ByteArray = window.fetchBytes(HttpMethod.PUT, resource, headers, body, redirect, abortController)
407+
): ByteArray = fetchScope.fetchBytes(HttpMethod.PUT, resource, headers, body, redirect, abortController)
407408

408409
/**
409410
* Like [put], but returns null if the request failed for any reason.
@@ -430,8 +431,10 @@ class HttpFetcher(private val window: Window) {
430431
body: ByteArray? = null,
431432
redirect: RequestRedirect? = FetchDefaults.Redirect,
432433
abortController: AbortController? = null
433-
): ByteArray? = window.tryFetchBytes(HttpMethod.PUT, resource, headers, body, redirect, logOnError, abortController)
434+
): ByteArray? = fetchScope.tryFetchBytes(HttpMethod.PUT, resource, headers, body, redirect, logOnError, abortController)
434435
}
435436

436-
@Suppress("unused") // We tie our class to the "Window" class on purpose, so it can be used instead of `fetch`
437-
val Window.http by lazy { HttpFetcher(window) }
437+
val WindowOrWorkerGlobalScope.http: HttpFetcher get() = with(this.asDynamic()) {
438+
httpFetcher = httpFetcher ?: HttpFetcher(this)
439+
httpFetcher
440+
}

0 commit comments

Comments
 (0)