Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface DataType<ValueType, SourceType, CBORType>

A data type is specified for a particular value type. It provides methods to convert values to and from binary. Diffusion provides several DataType implementations.

A data type can optionally support incremental changes to values, represented by one or more types of delta. A delta is the difference between two values. For large or composite values that change in small steps, it is more efficient to transmit an initial value followed by a delta for each change than to transmit a complete value for each change. The data type provides an implementation of DeltaType for each type of delta it supports via DataType.deltaType.

since

5.7

Type parameters

  • ValueType

    the value type of the data type

  • SourceType

    the type(s) from which a value can be constructed

  • CBORType

    the binary type containing the CBOR data

Hierarchy

Index

Methods

canReadAs

  • canReadAs(valueType: object): boolean
  • Test whether this data type is compatible with valueType. Compatibility with a valueType means than any valid binary representation of a value can be read as an instance of valueType.

    Every data type should be compatible with the following:

    • Value Type – the class corresponding to the data type's value type.

    For a data type with a value type of X, readAs(X, buffer) is equivalent to readValue(buffer).

    since

    6.0

    Parameters

    • valueType: object

      the type to check

    Returns boolean

    true if a binary representation created by this data type can read as an instance * of valueType

deltaType

  • deltaType(name?: undefined | string): DeltaType<ValueType, SourceType, CBORType>
  • Obtain a DeltaType by name or delta type.

    Example:

    // Get by name
    var deltas = datatype.deltaType("binary");

    Example:

    // Get by type
    var deltas = datatype.deltaType(delta);

    Parameters

    • Optional name: undefined | string

      the name, as returned by DeltaType.name

    Returns DeltaType<ValueType, SourceType, CBORType>

    the delta type

name

  • name(): string
  • The external type identifier.

    Returns string

    the name of this datatype

readAs

  • readAs<T>(valueType: object, buffer: Buffer, offset?: undefined | number, length?: undefined | number): T | null
  • readAs<T>(valueType: object, buffer: CBORType): T | null
  • Create a value of a compatible class from binary.

    throws

    an error if valueType is incompatible with this data type, or buffer does not * represent a valid value.

    since

    6.0

    Type parameters

    • T

    Parameters

    • valueType: object

      the type of the result

    • buffer: Buffer

      the binary data

    • Optional offset: undefined | number

      the offset to start reading from the provided buffer (default = 0)

    • Optional length: undefined | number

      the length of the data to read (default = input.length)

    Returns T | null

    the value in the form of the specified type

  • Type parameters

    • T

    Parameters

    • valueType: object
    • buffer: CBORType

    Returns T | null

readValue

  • readValue(input: Buffer, offset?: undefined | number, length?: undefined | number): ValueType | null
  • readValue(input: CBORType): ValueType | null
  • Parse a value from binary.

    When running the Diffusion Client in a browser context, access to the Buffer api is made available through {@link diffusion.buffer}.

    throws

    an error if the data is invalid for this type

    Parameters

    • input: Buffer

      the binary data

    • Optional offset: undefined | number

      the offset to start reading from the provided buffer (default = 0)

    • Optional length: undefined | number

      the length of the data to read (default = input.length)

    Returns ValueType | null

    an instance of this data type value

  • Parameters

    • input: CBORType

    Returns ValueType | null

writeValue

  • writeValue(value: SourceType | undefined | null): Buffer
  • Serialise a value to binary

    When running the Diffusion Client in a browser context, access to the Buffer api is made available through {@link diffusion.buffer}.

    throws

    an error if the value can not be serialised

    Parameters

    • value: SourceType | undefined | null

      the value to serialise. For primitive and JSON datatypes the value can be undefined or null. In this case a null value will be serialised.

    Returns Buffer

    the serialised value as a buffer