em-observable

Make a variable observable through accessors

Usage no npm install needed!

<script type="module">
  import emObservable from 'https://cdn.skypack.dev/em-observable';
</script>

README

Observable

Make a variable observable through accessors

Usage

1. Create observable

let myObservable = new Observable<T>();

the observable can optionally be initialized with a value

2. Add some observers

Observers are consumer lamdas.

you can add observers at any time. if you save their tokens, you can also remove them again

let myToken = myObservable((value : T)=>{...});

these observers will be notified the next time the value changes

3. Alter the value

myObservable.set(something : T) : T;

now all observers will be notified if the value has changed.

if the new value is identical to the old value, subscribers will not be notified.

4. Inspect the value

myObservable.get() : T;

will return the current internal value