-
-
Save tmcw/32a2800d9ab0c67435ba5a42fd90ec7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Left, Right, Either } from "purify-ts"; | |
import type { TransferHandler } from "comlink"; | |
interface SerializedEither { | |
__val: any; | |
__either: "left" | "right"; | |
} | |
export const EitherHandler: TransferHandler< | |
Either<unknown, unknown>, | |
SerializedEither | |
> = { | |
canHandle: (obj): obj is Either<unknown, unknown> => { | |
return Either.isEither(obj); | |
}, | |
serialize: (either: Either<unknown, unknown>) => { | |
const __val = either.extract(); | |
const transfer = []; | |
if (either.isRight() && (__val as any)?.result?.blob) { | |
transfer.push((__val as any).result.blob); | |
} | |
return [ | |
{ | |
__val: either.extract(), | |
__either: either.isLeft() ? "left" : "right", | |
}, | |
[], | |
]; | |
}, | |
deserialize: (obj) => { | |
return obj.__either === "left" ? Left(obj.__val) : Right(obj.__val); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment