mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
re-attach port balls
This commit is contained in:
parent
b4a2f2f3d1
commit
116d6ce16a
2 changed files with 20 additions and 6 deletions
|
@ -2,14 +2,17 @@ import { ScramjetClient } from "./client";
|
||||||
import { encodeUrl } from "./shared";
|
import { encodeUrl } from "./shared";
|
||||||
|
|
||||||
export class ScramjetServiceWorkerRuntime {
|
export class ScramjetServiceWorkerRuntime {
|
||||||
|
recvport: MessagePort;
|
||||||
constructor(public client: ScramjetClient) {
|
constructor(public client: ScramjetClient) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
self.onconnect = (cevent: MessageEvent) => {
|
self.onconnect = (cevent: MessageEvent) => {
|
||||||
const port = cevent.ports[0];
|
const port = cevent.ports[0];
|
||||||
|
dbg.log("sw", "connected");
|
||||||
|
|
||||||
port.addEventListener("message", (event) => {
|
port.addEventListener("message", (event) => {
|
||||||
|
console.log("sw", event.data);
|
||||||
if ("scramjet$type" in event.data) {
|
if ("scramjet$type" in event.data) {
|
||||||
handleMessage(client, event.data);
|
handleMessage.call(this, client, event.data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -20,12 +23,18 @@ export class ScramjetServiceWorkerRuntime {
|
||||||
hook() {}
|
hook() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleMessage(client: ScramjetClient, data: MessageW2R) {
|
function handleMessage(
|
||||||
const port = data.scramjet$port;
|
this: ScramjetServiceWorkerRuntime,
|
||||||
|
client: ScramjetClient,
|
||||||
|
data: MessageW2R
|
||||||
|
) {
|
||||||
|
if (data.scramjet$port) this.recvport = data.scramjet$port;
|
||||||
|
const port = this.recvport;
|
||||||
const type = data.scramjet$type;
|
const type = data.scramjet$type;
|
||||||
const token = data.scramjet$token;
|
const token = data.scramjet$token;
|
||||||
|
|
||||||
if (type === "fetch") {
|
if (type === "fetch") {
|
||||||
|
dbg.log("ee", data);
|
||||||
const fetchhandlers = client.eventcallbacks.get(self);
|
const fetchhandlers = client.eventcallbacks.get(self);
|
||||||
if (!fetchhandlers) return;
|
if (!fetchhandlers) return;
|
||||||
|
|
||||||
|
@ -116,4 +125,4 @@ type MessageCommon = {
|
||||||
|
|
||||||
export type MessageR2W = MessageCommon & MessageTypeR2W;
|
export type MessageR2W = MessageCommon & MessageTypeR2W;
|
||||||
export type MessageW2R = MessageCommon &
|
export type MessageW2R = MessageCommon &
|
||||||
MessageTypeW2R & { scramjet$port: MessagePort };
|
MessageTypeW2R & { scramjet$port: MessagePort | undefined };
|
||||||
|
|
|
@ -4,6 +4,7 @@ export class FakeServiceWorker {
|
||||||
syncToken = 0;
|
syncToken = 0;
|
||||||
promises: Record<number, (val?: MessageR2W) => void> = {};
|
promises: Record<number, (val?: MessageR2W) => void> = {};
|
||||||
messageChannel = new MessageChannel();
|
messageChannel = new MessageChannel();
|
||||||
|
alreadytransfered = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public handle: MessagePort,
|
public handle: MessagePort,
|
||||||
|
@ -31,7 +32,7 @@ export class FakeServiceWorker {
|
||||||
const message: MessageW2R = {
|
const message: MessageW2R = {
|
||||||
scramjet$type: "fetch",
|
scramjet$type: "fetch",
|
||||||
scramjet$token: token,
|
scramjet$token: token,
|
||||||
scramjet$port: this.messageChannel.port2,
|
scramjet$port: !this.alreadytransfered && this.messageChannel.port2,
|
||||||
scramjet$request: {
|
scramjet$request: {
|
||||||
url: request.url,
|
url: request.url,
|
||||||
body: request.body,
|
body: request.body,
|
||||||
|
@ -43,7 +44,11 @@ export class FakeServiceWorker {
|
||||||
};
|
};
|
||||||
|
|
||||||
const transfer: any = request.body ? [request.body] : [];
|
const transfer: any = request.body ? [request.body] : [];
|
||||||
transfer.push(this.messageChannel.port2);
|
|
||||||
|
if (!this.alreadytransfered) {
|
||||||
|
this.alreadytransfered = true;
|
||||||
|
transfer.push(this.messageChannel.port2);
|
||||||
|
}
|
||||||
|
|
||||||
this.handle.postMessage(message, transfer);
|
this.handle.postMessage(message, transfer);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue