# observedvar

> Create a variable to which you can attach listeners

Latest version **1.2.0** (published 2021-09-06) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2021-09-06 |
| First published | 2021-09-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Arthaud Proust |
| Maintainers | arthaud |

## Links

- npm: https://www.npmjs.com/package/observedvar
- Repository: https://github.com/arthaud-proust/observedVar
- Homepage: https://github.com/arthaud-proust/observedVar#readme
- Issues: https://github.com/arthaud-proust/observedVar/issues
- npm.io page: https://npm.io/package/observedvar

## Recent versions

- 1.2.0 (latest) — 2021-09-06
- 1.1.3 — 2021-09-05
- 1.1.2 — 2021-09-05
- 1.1.1 — 2021-09-05
- 1.1.0 — 2021-09-05
- 1.0.2 — 2021-09-05
- 1.0.1 — 2021-09-04
- 1.0.0 — 2021-09-04

## README

# Package ObservedVar

## What is it?
A package which simply allows you to attach listeners to your variables.

## How to install ?
```shell
$ npm i observedvar
```

## How it works?

#### Includes package 
```js
const { ObservedVar } = require('observedvar');
// or (shorthand)
const { Ov } = require('observedvar');
```

#### Create an observed variable
```js
const foo = new ObservedVar('defaultValue');
// or (shorthand with the right import)
const foo = new Ov('defaultValue');
```

#### Set value 
```js
foo.value = 'new value'
// or
foo.set('new value')
```

#### Get value
```js
foo.value
// or 
foo.get()
```

#### Add a listener, for everytime or once
It return a listener object, use his id to unsubscribe.
```js
const callback = (listener, newValue)=>{
    console.log(`(listener ${listener.id}) Foo value changed, his value is now ${newValue}`)
    if(conditionToDestroy) {
        listener.destroy();
    }
};

const fooListener = foo.subscribe(
    callback,
    isDirectlyDestroyedAfterBeingCalled // default is false
);
// shorthands
foo.sub(...)
foo.once(...) // equals to foo.sub(callback, true)
```

#### Remove a listener
With the Ov variable
```js
foo.unsubscribe(fooListener.id);
// shorthand
foo.unsub(...);
```
Or directly from the listener
```js
fooListener.destroy();
```



#### The method once has another use  
You could use a listener to handle a fetch result.
To prevent the following case
```js
const fetchedData = new ObservedVar(null);
fetch(...).then(data=>{
    fetchedData.value = data;
});

...

if(fetchedData===null) {
    // supposing the data is fetching.
    // so we need to subscribe a listener
    fetchedData.subscribe(()=>handleData());
} else {
    // supposing the data is already fetched.
    // we don't need a listener
    handleData();
}
```
Use `once()` like that:
```js
const fetchedData = new ObservedVar(null);
fetch(...).then(data=>{
    fetchedData.value = data;
});

...

fetchedData.once(
    handleData,
    null // The value expected to subscribe a listener
);
```

## Listener object
It is returned when the method `.subscribe(...)` is called, or within the callback
```js
const fooListener = foo.sub(listener=>{
    console.log(listener)
}, once);

console.log(fooListener)

// listener = fooListener = {
//      once: false,
//      id: 0,
//      destroy: [Function: destroy],
//      callback: [Function: callback]
// }

```

## Shorthands
|Full|Shorthand|
|---|---|
|`new ObservedVar(defaultValue)`|`new Ov(defaultValue)`|
|`.subscribe(listener)`|`.sub(listener)`|
|`.unsubscribe(listenerId)`|`.unsub(listenerId)`|
|`.subscribe(listener, true)`|`.once(listener)`|

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