datem
v0.1.4
Published
Represent a value and a flag indicating if the value has been set. Presents a constant object whose actual value may be changing. Emits change events.
Downloads
7
Readme
datem
Datem provides abstract datum types. Datum types represent a value and an additional flag indicating if the value has been set. They present constant objects whose actual values may be changing.
Datum objects are event emitters - listeners can be notified whenever the value changes or an error is handled.
Datum Types
- SET_EVENT ( datum, value )
- CHANGE_EVENT ( datum, value )
- ERROR_EVENT ( datum, error )
Datum
Datum extends EventEmitter.
new Datum( value )
Sets the value.
bool hasValue { get; }
Indicates if a value is set.
any value { get; }
Value that is set. Undefined if not set.
any getValue()
Value that is set. Throws NO VALUE DatumError if not set.
any getValueOrDefault( any defaultValue )
Value that is set. Returns defaultValue if not set.
MutableDatum
MutableDatum extends Datum. Provides the ability to change the value.
- Emits SET_EVENT when value is set.
- Emits CHANGE_EVENT when value is changed. Only actually changes if differs.
- Emits ERROR_EVENT when an error happens but is caught.
new MutableDatum( value )
Sets an intial value.
any value { get; set; }
Value that is set. Undefined if not set.
this clearValue()
Clears the set value. When cleared hasValue is false. Returns itself.
this setValue( any value )
Sets the value. Returns itself.
RefreshableDatum
RefreshableDatum extends MutableDatum. Provides the ability to update with a refresh function. Trying to set, clear, or refresh while a refresh is pending will throw PENDING REFRESH DatumError.
- Emits SET_EVENT when value is set.
- Emits CHANGE_EVENT when value is changed. Only actually changes if differs.
- Emits ERROR_EVENT when an error happens but is caught.
new RefreshableDatum( refreshFunction, value )
Sets an initial value and registers a function to be used in refreshing.
bool pendingRefresh { get; }
Indicates if a refresh is in progress.
this refreshValue()
Runs the refresh function and updates the value. Returns itself.
refresh function
any refreshFunction ( RefreshableDatum datum )
Refresh function will be used to update the value of a RefreshableDatum. The return value will be set as the new value of the datum. If an error is thrown then an ERROR_EVENT will be emitted by the datum. If the return value is a promise then will try to resolve it.
DatumError
DatumError extends Error.
- NO VALUE
- PENDING REFRESH
error original { get; }
If wrapping another error then the original error. Otherwise undefined.
Example
const RefreshableDatum = require('datem').RefreshableDatum;
const CHANGE_EVENT = require('datem').CHANGE_EVENT;
function refreshFunction( selfDatum ) {
return selfDatum.value + 1;
}
let datum = new RefreshableDatum( refreshFunction, 123 );
datum.on( CHANGE_EVENT,
( selfDatum, value ) => {
console.log ( selfDatum.valueOf(), value );
// { hasValue: bool, value: value } value
}
);
datum.value = 100;
datum.refreshValue();
License
ISC