Skip to content

refactor(websocket): Use Result from npm:option-t #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions browser/websocket/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
type PageCommitError,
type PageCommitResponse,
type PinChange,
type Result as SocketResult,
type Socket,
socketIO,
type TimeoutError,
Expand All @@ -26,6 +25,7 @@ import {
createOk,
isErr,
type Result,
unwrapErr,
unwrapOk,
} from "option-t/plain_result";
import type { HTTPError } from "../../rest/responseIntoResult.ts";
Expand Down Expand Up @@ -133,19 +133,20 @@ export const push = async (
const result = (await request("socket.io-request", {
method: "commit",
data,
})) as SocketResult<
})) as Result<
PageCommitResponse,
UnexpectedError | TimeoutError | PageCommitError
>;

if (result.ok) {
metadata.commitId = result.value.commitId;
if (createOk(result)) {
metadata.commitId = unwrapOk(result).commitId;
// success
return createOk(metadata.commitId);
}
const name = result.value.name;
const error = unwrapErr(result);
const name = error.name;
if (name === "UnexpectedError") {
return createErr({ name, message: JSON.stringify(result.value) });
return createErr({ name, message: JSON.stringify(error) });
}
if (name === "TimeoutError" || name === "SocketIOError") {
await delay(3000);
Expand Down
4 changes: 0 additions & 4 deletions browser/websocket/websocket-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ const pageCommitErrorNames = [
"NotFastForwardError",
];

export type Result<T, E = unknown> =
| { ok: true; value: T }
| { ok: false; value: E };

export interface EventMap {
"socket.io-request": (
req: { method: "commit"; data: PageCommit } | {
Expand Down
36 changes: 19 additions & 17 deletions browser/websocket/wrap.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createErr, createOk, type Result } from "option-t/plain_result";
import type { Socket } from "./socket-io.ts";
import {
type DataOf,
type EventMap,
type FailedResOf,
isPageCommitError,
type ListenEventMap,
type Result,
type SuccessResOf,
type TimeoutError,
type UnexpectedError,
Expand Down Expand Up @@ -62,25 +62,23 @@ export const wrap = (
typeof response.error.name === "string" &&
isPageCommitError({ name: response.error.name })
) {
resolve({ ok: false, value: response.error });
resolve(createErr(response.error));
} else {
resolve({
ok: false,
value: { name: "UnexpectedError", value: response.error },
});
resolve(
createErr(unexpectedError(response.error)),
);
}
} else if ("data" in response) {
resolve({ ok: true, value: response.data });
resolve(createOk(response.data));
}
break;
case "cursor":
if ("error" in response) {
resolve({
ok: false,
value: { name: "UnexpectedError", value: response.error },
});
resolve(
createErr(unexpectedError(response.error)),
);
} else if ("data" in response) {
resolve({ ok: true, value: response.data });
resolve(createOk(response.data));
}
break;
}
Expand All @@ -93,13 +91,12 @@ export const wrap = (
);
id = setTimeout(() => {
socket.off("disconnect", onDisconnect);
resolve({
ok: false,
value: {
resolve(
createErr({
name: "TimeoutError",
message: `Timeout: exceeded ${timeout}ms`,
},
});
}),
);
}, timeout);
socket.once("disconnect", onDisconnect);
});
Expand Down Expand Up @@ -135,3 +132,8 @@ export const wrap = (

return { request, response };
};

const unexpectedError = (value: unknown): UnexpectedError => ({
name: "UnexpectedError",
value,
});