Options
All
  • Public
  • Public/Protected
  • All
Menu

Class StructType<E, READ_E>

Intended to model a generic JavaScript object, whose field names are known in advance. If field names are part of the value rather than the type, use a MapType instead.

The value passed into the constructor should resemble the values to be written. For example, to write {a: 100, b: 'abc', c: false}, you could use:

new sb.StructType({
  a: new sb.UnsignedIntType,
  b: new sb.StringType,
  c: new sb.BooleanType
})

Example:

//For storing a person's information
let type = new sb.StructType({
  name: new sb.StringType,
  age: new sb.UnsignedByteType,
  netWorth: new sb.FloatType
})

Type parameters

  • E: Record<string, any>

    The type of object values this type can write

  • READ_E: E = E

    The type of object values this type will read

Hierarchy

  • AbsoluteType<E, READ_E>
    • StructType

Index

Constructors

constructor

  • Type parameters

    • E: Record<string, any>

    • READ_E: Record<string, any> = E

    Parameters

    • fields: StructFields<E, READ_E>

      A mapping of field names to their types.

    Returns StructType<E, READ_E>

Properties

Readonly fields

fields: StructField<E>[]

An array of the field names with their corresponding types. Fields are sorted lexicographically by name, so that passing in equivalent fields objects to the constructor always gives the same result. Field names' UTF-8 representations are also cached.

Accessors

Static _value

  • get _value(): number

Methods

addToBuffer

consumeValue

  • consumeValue(buffer: ArrayBuffer, offset: number, baseValue?: object): ReadResult<READ_E>
  • Parameters

    • buffer: ArrayBuffer
    • offset: number
    • Optional baseValue: object

    Returns ReadResult<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 StructType<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 StructType<E, READ_E>

    whether this and otherType are instances of the same Type class

readValue

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

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

    Returns READ_E

toBuffer

  • toBuffer(): ArrayBuffer

valueBuffer

  • valueBuffer(value: E): ArrayBuffer
  • Parameters

    • value: E

    Returns ArrayBuffer

writeValue

  • Appends value bytes to an AppendableBuffer according to the type

    Example:

    type.writeValue(buffer, {
      name: 'Gertrude',
      age: 29,
      netWorth: 1.2e6
    })
    
    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: E

      The value to write

    Returns void

Generated using TypeDoc