Skip to content

Commit

Permalink
Add deserialize cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Nov 22, 2023
1 parent a02127b commit 8666609
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/start/config/server-runtime.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deserialize, toJSONAsync } from "seroval";
import { createIslandReference } from "../server/islands";

async function deserializeStream(response) {
async function deserializeStream(id, response) {
if (!response.body) {
throw new Error('missing body');
}
Expand All @@ -28,7 +28,9 @@ async function deserializeStream(response) {
const serialized = new TextDecoder().decode(result.value);
const revived = deserialize(serialized);

pop().catch(() => {
pop().then(() => {
delete self.$R[id];
}, () => {
// no-op
});

Expand All @@ -38,6 +40,7 @@ async function deserializeStream(response) {
let INSTANCE = 0;

async function fetchServerAction(base, id, args) {
const instance = `server-action:${INSTANCE++}`;
const response = await fetch(base, {
method: "POST",
headers: {
Expand All @@ -46,14 +49,15 @@ async function fetchServerAction(base, id, args) {
"server-action": id
},
body: JSON.stringify({
instance: `server-action:${INSTANCE++}`,
instance,
args: await toJSONAsync(args),
}),
});
const result = deserializeStream(instance, response);
if (response.ok) {
return deserializeStream(response);
return result;
}
throw deserializeStream(response);
throw result;
}

export function createServerReference(fn, id, name) {
Expand Down

0 comments on commit 8666609

Please sign in to comment.