Options
All
  • Public
  • Public/Protected
  • All
Menu

Class RecursiveType<E, READ_E>

A type that can refer recursively to itself. This is not a type in its own right, but allows you to have some other type use itself in its definition. Values that contain circular references will have the references preserved after serialization and deserialization.

Note that it is impossible to create self-referencing types any other way, due to the immutable nature of types. Also, other types will infinitely recurse on writes with circular references.

Recursive types must be of one of the following types:

This is due to the way that recursive values are deserialized. For example, say the value was created with the following code:

let selfType = new sb.RecursiveType('self-type')
selfType.setType(new sb.StructType({
  self: selfType
}))
let self = {} //note that we had to give self a "base value" of {}
self.self = self

In order to deserialize the value, we need to carry out the same process:

  • Set the value to some mutable base value so we have a reference to it
  • Read the value for the self field, which gives the base value
  • Assign the result of the read to the self field, mutating the base value

The base values used are as follows:

Base values for other types are harder to compute, which is why they are not allowed as RecursiveTypes. You can always use another type by making it the sole field of a StructType.

Example:

//A binary tree of unsigned bytes
let treeType = new sb.RecursiveType('tree-node')
treeType.setType(new sb.StructType({
  left: new sb.OptionalType(treeType),
  value: new sb.UnsignedByteType,
  right: new sb.OptionalType(treeType)
}))

Type parameters

  • E

    The type of value this type can write (presumably a recursive value)

  • READ_E: E = E

    The type of value this type will read

Hierarchy

  • AbstractType<E, READ_E>
    • RecursiveType

Index

Constructors

constructor

  • new RecursiveType<E, READ_E>(name: string): RecursiveType<E, READ_E>

Properties

Readonly name

name: string

Accessors

type

Static _value

  • get _value(): number

Methods

addToBuffer

consumeValue

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

    • buffer: ArrayBuffer
    • offset: number

    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 RecursiveType<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 RecursiveType<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

setType

  • setType(type: RegisterableType): void
  • An alternative to registerType, to avoid writing the type's name twice. Please use this instead of registerType.

    So this

    let type = new sb.RecursiveType('abc')
    sb.registerType({
      type: new sb.StructType({
        //...
      }),
      name: 'abc'
    })
    

    becomes

    let type = new sb.RecursiveType('abc')
    type.setType(new sb.StructType({
      //...
    }))
    

    Parameters

    • type: RegisterableType

      The type to register

    Returns void

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:

    treeType.writeValue(buffer, {
      left: {
        left: {value: 1},
        value: 2,
        right: {value: 3}
      },
      value: 4,
      right: {
        value: 5,
        right: {value: 6}
      }
    })
    
    throws

    If the value doesn't match the type, e.g. new sb.StringType().writeValue(buffer, 23); also throws if no type has been registered with this type's name

    Parameters

    • buffer: AppendableBuffer

      The buffer to which to append

    • value: E

      The value to write

    Returns void

Generated using TypeDoc