Class: DeltaType

diffusion.datatypes. DeltaType


new DeltaType()

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

Each implementation specifies a value type and a delta type. Two values, oldValue and new Value, can be compared to produce a delta using diffusion.datatypes.DeltaType#diff. The delta can be separately applied to oldValue to create newValue using diffusion.datatypes.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

Methods


apply(old, delta)

Apply a delta to a value.

Parameters:
Name Type Description
old Object

The old value

delta Object

The delta to apply

Throws:

Error if the value or delta is invalid

Returns:

The new value generated applying the delta to the old value

Type
Object

diff(oldValue-, newValue)

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 diffusion.datatypes.DeltaType#isValueCheaper.

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

Parameters:
Name Type Description
oldValue- Object

The old value

newValue Object

The new value

Returns:

The delta between values

Type
Object

isValueCheaper(value, delta)

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.

Parameters:
Name Type Description
value Object

The value to compare

delta Object

The delta to compare

Throws:

Error if the value or delta is invalid

Returns:

true if the value is considered cheaper than the delta

Type
Boolean

name()

The name of this delta type

Returns:

The name

Type
String

noChange()

Constant returned by diffusion.datatypes.DeltaType#diff to indicate that both values are equivalent.

Returns:

unique object representing no change in value

Type
Object

readDelta(binary [, offset] [, length])

Parse a delta from binary.

Parameters:
Name Type Argument Description
binary Buffer

The binary data

offset Number <optional>

The offset from which to start reading from the buffer

length Number <optional>

The length of data to read from the buffer

Throws:

Error if the binary is invalid

Returns:

The delta

Type
Object

writeDelta(delta)

Serialise a delta to binary.

Parameters:
Name Type Description
delta Object

The delta to serialise

Throws:

Error if the delta cannot be serialised

Returns:

The serialised form of the delta

Type
Buffer