2.0.0 • Published 4 years ago

@dubaua/observable v2.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

Observable

A little class providing reactive value and subscribe method. The method is a higher order function accepting callback as the only parameter and returning unsubscribe function. A callback accepts next value as first parameter and previous value as second. Callback will be fired if instance value is changed.

const observable = new Observable();

const log = [];
function logger(next) {
  log.push(next);
}

const stopLogging = observable.subscribe(logger);

When instance value changed callback will be fired:

reactive.value = 35; // log [35]
reactive.value = 35; // log [35] didn't logged, because value the same
reactive.value = 42; // log [35, 42]

You can unsubscribe with function returned by subscribe method.

stopLogging();
reactive.value = 69; // log [35, 42]