Options
All
  • Public
  • Public/Protected
  • All
Menu

Class OptionalType<E, READ_E>

A type storing a value of another type or null or undefined. null and undefined are treated identically, and reading either value will result in null.

Example:

//If you have a job slot that may or may not be filled
let personType = new sb.StructType({
  age: new sb.UnsignedByteType,
  name: new sb.StringType
})
let type = new sb.StructType({
  title: new sb.StringType,
  employee: new sb.OptionalType(personType)
})

Type parameters

  • E

    The type of non-null values

  • READ_E: E = E

    The type of non-null read values

Hierarchy

  • AbsoluteType<E | null | undefined, READ_E | null>
    • OptionalType

Index

Constructors

constructor

  • Type parameters

    • E

    • READ_E = E

    Parameters

    • type: Type<E, READ_E>

      The Type used to write values if they are not null or undefined

    Returns OptionalType<E, READ_E>

Properties

Readonly type

type: Type<E, READ_E>

Accessors

Static _value

  • get _value(): number

Methods

addToBuffer

consumeValue

  • consumeValue(buffer: ArrayBuffer, offset: number): ReadResult<null | READ_E>
  • Parameters

    • buffer: ArrayBuffer
    • offset: number

    Returns ReadResult<null | READ_E>

equals

  • equals(otherType: unknown): boolean
  • Parameters

    • otherType: unknown

    Returns boolean

getHash

  • getHash(): string

getSignature

  • getSignature(): string

Private isBuffer

Private isSameType

  • isSameType(otherType: unknown): otherType is OptionalType<E, READ_E>
  • Determines whether the input is a Type with the same class

    Parameters

    • otherType: unknown

      A value, usually a Type instance

    Returns otherType is OptionalType<E, READ_E>

    whether this and otherType are instances of the same Type class

readValue

  • readValue(valueBuffer: ArrayBuffer | Uint8Array, offset?: number): null | READ_E
  • Parameters

    • valueBuffer: ArrayBuffer | Uint8Array
    • offset: number = 0

    Returns null | READ_E

toBuffer

  • toBuffer(): ArrayBuffer

valueBuffer

  • valueBuffer(value: undefined | null | E): ArrayBuffer
  • Parameters

    • value: undefined | null | E

    Returns ArrayBuffer

writeValue

  • Appends value bytes to an AppendableBuffer according to the type

    Examples:

    type.writeValue(buffer, {
      title: 'Manager',
      employee: null //or undefined
    })
    

    or

    type.writeValue(buffer, {
      title: 'Coder',
      employee: {age: 19, name: 'Johnny'}
    })
    
    throws

    If the value doesn't match the type, e.g. new sb.StringType().writeValue(buffer, 23)

    Parameters

    • buffer: AppendableBuffer

      The buffer to which to append

    • value: undefined | null | E

      The value to write

    Returns void

Generated using TypeDoc