basic-signals

0.1.1 • Public • Published

Basic-signals

This project is similar to basic-streams in its purpose. The idea is to explore what kind of operations we can implement for a certain async abstraction. And to find cases where that abstraction can be usable.

Basic-streams project explores the "Stream" abstraction which is very similar to RxJS observables in the core idea, but with much simpler and limited API. While basic-signals focuses on a slightly different abstraction called "Signal" with the following differences from streams:

  • Signals are not inert. When we create a stream it does nothing until we subscribe to it, signals on other hand are more like Promises, they "activate" immediately once created. Adding or removing an observer should not affect any other observers in any way.
  • Each signal have a current value, which is always available without observing, and must always be up to date even if there are no observers. Adding or removing an observer should not affect the current value.

Signal API

Signal is an object with two methods.

get

Must return the current value of the signal. Calling get must have no side effects, for instance it must not affect observers or change the current value.

observe

Accepts an observer, and must return an unobserve function. An observer is a function which must be called with no arguments every time the current value changes. After unobserve was called, observer must not be called. Calling an unobserve second time must have no effect i.e., it must be idempotent.

console.log('Current value is:', signal.get())
 
const unobserve = signal.observe(() => {
  console.log('Value has changed, the new value is:', signal.get())
})
 
...
 
unobserve()

Development

npm run lobot -- --help

Run lobot commands as npm run lobot -- args...

Readme

Keywords

none

Package Sidebar

Install

npm i basic-signals

Weekly Downloads

1

Version

0.1.1

License

MIT

Last publish

Collaborators

  • pozadi