new DeltaType()
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) → {Object}
-
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 invalidReturns:
The new value generated applying the delta to the old value- Type
- Object
-
diff(oldValue-, newValue) → {Object}
-
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) → {Boolean}
-
Calculate if
value
is cheaper than thedelta
. 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 invalidReturns:
true
if the value is considered cheaper than the delta- Type
- Boolean
-
name() → {String}
-
The name of this delta type
Returns:
The name- Type
- String
-
noChange() → {Object}
-
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) → {Object}
-
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 invalidReturns:
The delta- Type
- Object
-
writeDelta(delta) → {Buffer}
-
Serialise a delta to binary.
Parameters:
Name Type Description delta
Object The delta to serialise Throws:
Error if the delta cannot be serialisedReturns:
The serialised form of the delta- Type
- Buffer