Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface DeltaType<ValueType, SourceType, CBORType>

Optional extension provided by DataType implementations that support incremental changes to values.

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.

Each implementation specifies a value type and a delta type. Two values, oldValue and new Value, can be compared to produce a delta using DeltaType.diff. The delta can be separately applied to oldValue to create newValue using DeltaType.apply.

Deferred parsing
Implementations can choose not to fully validate values when they are read, but instead defer parsing until it is required. Consequently, all methods that accept values may throw an error.
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

  • DeltaType

Index

Methods

apply

  • apply(oldValue: SourceType, delta: BinaryDelta): ValueType | null
  • Apply a delta to a value.

    throws

    an error if the value or delta is invalid

    Parameters

    • oldValue: SourceType
    • delta: BinaryDelta

      the delta to apply

    Returns ValueType | null

    the new value generated applying the delta to the old value

diff

  • diff(oldValue: SourceType, newValue: SourceType): BinaryDelta
  • Create a delta from two values.

    If there are many differences between oldValue and newValue, the result might require more bytes to transmit than the new value, or be computationally expensive to apply. In this case, it is better to discard oldValue and publish newValue in its place. This can be checked using DeltaType.isValueCheaper.

    The implementation can return the special constant DeltaType.noChange to indicate the old value and new value are equivalent and there is no change to publish.

    Parameters

    • oldValue: SourceType

      the old value

    • newValue: SourceType

      the new value

    Returns BinaryDelta

    the delta between values

isValueCheaper

  • isValueCheaper(value: SourceType, delta: BinaryDelta): boolean
  • Calculate if value is cheaper than the delta. The result is typically determined by the length of the serialised form, but may also consider the complexity of the delta.

    throws

    an error if the value or delta is invalid

    Parameters

    • value: SourceType

      the value to compare

    • delta: BinaryDelta

      the delta to compare

    Returns boolean

    true if the value is considered cheaper than the delta

name

  • name(): string
  • The name of this delta type

    Returns string

    the name

noChange

  • Constant returned by DeltaType.diff to indicate that both values are equivalent.

    Returns BinaryDelta

    unique object representing no change in value

readDelta

  • readDelta(buffer: Buffer, offset?: undefined | number, length?: undefined | number): BinaryDelta
  • Parse a delta from binary.

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

    throws

    an error if the binary is invalid

    Parameters

    • buffer: Buffer
    • Optional offset: undefined | number

      the offset from which to start reading from the buffer

    • Optional length: undefined | number

      the length of data to read from the buffer

    Returns BinaryDelta

    the delta

writeDelta

  • Serialise a delta to binary.

    throws

    an error if the delta cannot be serialised

    Parameters

    Returns Buffer

    the serialised form of the delta