# datem

> 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.

Latest version **0.1.4** (published 2016-02-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install datem
pnpm add datem
yarn add datem
bun add datem
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2016-02-11 |
| First published | 2016-01-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 4.0.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Michael McGlothlin |
| Maintainers | kavlon |
| Keywords | abstract, value |

## Links

- npm: https://www.npmjs.com/package/datem
- npm.io page: https://npm.io/package/datem

## Recent versions

- 0.1.4 (latest) — 2016-02-11
- 0.1.3 — 2016-02-02
- 0.1.2 — 2016-02-02
- 0.1.1 — 2016-02-02
- 0.1.0 — 2016-01-06

## 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

---
_Source: https://npm.io/package/datem · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
