Skip to content

Commit 5758fea

Browse files
committed
Use KJ_CO_MAGIC BEGIN_DEFERRED_PROXYING in WebSocket::couple()
Just to show that it works.
1 parent 026f9a3 commit 5758fea

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/workerd/api/web-socket.c++

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ jsg::Ref<WebSocket> WebSocket::constructor(
291291
return ws;
292292
}
293293

294-
kj::Promise<DeferredProxy<void>> WebSocket::couple(kj::Own<kj::WebSocket> other) {
294+
DeferredProxyPromise<void> WebSocket::couple(kj::Own<kj::WebSocket> other) {
295295
auto& native = *farNative;
296296
JSG_REQUIRE(!native.state.is<AwaitingConnection>(), TypeError,
297297
"Can't return WebSocket in a Response if it was created with `new WebSocket()`");
@@ -333,13 +333,18 @@ kj::Promise<DeferredProxy<void>> WebSocket::couple(kj::Own<kj::WebSocket> other)
333333
if (locality == LOCAL) {
334334
// Since the WebSocket is terminated locally, we need the IoContext to stay live while
335335
// it is active.
336-
co_await promise;
337-
co_return DeferredProxy<void> { kj::READY_NOW };
336+
co_return co_await promise;
338337
} else {
339338
// Since the WebSocket is just proxying through, we can do the pump in a deferred proxy task.
340339
// Note that we don't need to (and can't) register any pending events in this case since the
341340
// IoContext is free to go away at this point.
342-
co_return DeferredProxy<void> { kj::mv(promise) };
341+
342+
// To begin deferred proxying, we can use this magic `KJ_CO_MAGIC` expression, which fulfills
343+
// our outer promise for a DeferredProxy<void>, which wraps a promise for the rest of this
344+
// coroutine.
345+
KJ_CO_MAGIC BEGIN_DEFERRED_PROXYING;
346+
347+
co_return co_await promise;
343348
}
344349
}
345350

src/workerd/api/web-socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class WebSocket: public EventTarget {
253253
// response once it's available. This includes assigning the native websocket and dispatching the
254254
// relevant `open`/`error` events.
255255

256-
kj::Promise<DeferredProxy<void>> couple(kj::Own<kj::WebSocket> other);
256+
DeferredProxyPromise<void> couple(kj::Own<kj::WebSocket> other);
257257
// Pumps messages from this WebSocket to `other`, and from `other` to this, making sure to
258258
// register pending events as appropriate. Used to implement FetchEvent.respondWith().
259259
//

0 commit comments

Comments
 (0)