Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SwitchSerializer<Type, Remaining>

The SwitchSerializer is meant to give you an easy to use way of serializing Union Types. You can use this serializer by defining Type Guards and assign serializers to them with the register method.

The first fitting serializer, the first serializer whose type guard returns true, will be used to delegate the serialisation and deserialization to. This class will then prepend the data by the index of the used serializer to identify the correct one on deserialization.

Type parameters

  • Type

  • Remaining: Type

Hierarchy

Index

Methods

arrayBufferToValue

  • arrayBufferToValue(arrayBuffer: ArrayBuffer): Type

deserialize

  • deserialize(dv: DataView, offset: number): { offset: number; val: Type }

finalize

  • finalize(): this
  • This method needs to be called after all registers are done to make this serializer usable. The reason is that this serializer needs an internal IntSerializer to work which needs to be created at some point and be fitting to the number of registered serializers to be able to identify all of the indexes.

    Returns this

getByteSizeFromDataInBuffer

  • getByteSizeFromDataInBuffer(dv: DataView, offset: number): number

getSizeForValue

  • getSizeForValue(val: Type): number

getStaticSize

  • getStaticSize(): number | undefined

register

  • register<Selected>(tester: (val: Remaining) => val is Selected, serializer: ValueSerializer<Remaining extends Selected ? Remaining : never>): SwitchSerializer<Type>
  • This function allows you to register serializers for the specific sub types of the union. On serialization the first fitting serializer will be used. That means that the first serializer with a true returning tester will be used.

    You need to keep the order of registers the same to prevent any issue with the deserialization, since this class only prepends the index of the used serializer.

    You should add the most strict type guards first and then gradually add the more general ones, since the type guards will be tested in the order in which they were added via the register method, so a broad one would be like a catch all.

    There is a caveat with indistinguishable types. Two types are indistinguishable if you can assign a value from type A to a variable with type B and the other way around. Then Exclude<A, B> results in never. Which results in the next .register call to have a never as parameter type. One simple workaround is to create a property on the type which you assign a distinguishable type. In either case this can be cumbersome and is the reason the ValueSerializer has the TYPE_PINPOINT property.

    Type parameters

    • Selected: Remaining

    Parameters

    • tester: (val: Remaining) => val is Selected

      The function which distinguishes between values the provided serializer can serialize and which not.

        • (val: Remaining): val is Selected
        • Parameters

          • val: Remaining

          Returns val is Selected

    • serializer: ValueSerializer<Remaining extends Selected ? Remaining : never>

      The attached Serializer.

    Returns SwitchSerializer<Type>

serialize

  • serialize(dv: DataView, offset: number, val: Type): { offset: number }

typeCheck

  • typeCheck(val: Type, name: string | undefined): void

valueToArrayBuffer

  • valueToArrayBuffer(val: Type): ArrayBuffer

Generated using TypeDoc