import type { FabricValue } from "@/interface.ts"; import { BaseFabricCodec } from "@/codec-common/BaseFabricCodec.ts"; import type { ReconstructionContext } from "@/codec-common/interface.ts"; import { CODEC_TYPE_TAGS } from "@/codec-common/codec-type-tags.ts"; /** * Codec for `undefined`. Encodes to the `Undefined@1` tag with `null` state. * `undefined` has no corresponding class, so there is no `uniqueHandledClass`; * matching is by `canEncode()`. See Section 1.4.1 of the formal spec. */ export class UndefinedCodec extends BaseFabricCodec { constructor() { super(CODEC_TYPE_TAGS.Undefined, undefined); } /** @inheritDoc */ override canEncode(value: FabricValue): boolean { return value === undefined; } /** @inheritDoc */ encode(_value: FabricValue): FabricValue { return null; } /** @inheritDoc */ decode( typeTag: string, state: FabricValue, _context: ReconstructionContext, ): FabricValue { if (state !== null) { throw new Error( `${typeTag}: expected \`null\` state, got ${typeof state}`, ); } return undefined; } }