# just-observable

> Create a simple observable

Latest version **0.4.0** (published 2023-11-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install just-observable
pnpm add just-observable
yarn add just-observable
bun add just-observable
```

## Health

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

Positive: has types; no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2023-11-24 |
| First published | 2021-02-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Michael Szmadzinski |
| Maintainers | smujmaiku |

## Links

- npm: https://www.npmjs.com/package/just-observable
- Repository: https://github.com/smujmaiku/just-observable
- Homepage: https://github.com/smujmaiku/just-observable#readme
- Issues: https://github.com/smujmaiku/just-observable/issues
- npm.io page: https://npm.io/package/just-observable

## Recent versions

- 0.4.0 (latest) — 2023-11-24
- 0.3.0 — 2021-02-21
- 0.2.3 — 2021-02-21
- 0.2.2 — 2021-02-16
- 0.2.1 — 2021-02-16
- 0.2.0 — 2021-02-14
- 0.1.1 — 2021-02-10

## README

# Just Observable

Do you need an observable that can be subscribed and emitted?
Are you looking for a single emitter to observe like rxjs subject?
Would you like a promise to wait on the next event?

## Installation

`npm i just-observable`

## Usage

```js
const justObservable = require('just-observable');
const observer = justObservable();

const unsubscribe = observer.subscribe(callback);

observer.next('some data');
observer.next('more data');

unsubscribe();
```

### toPromise method

Create a Promise that resolves at the next emitted value.
Sequential `toPromise` methods may miss data if the `next` method is called repeatedly while blocking.

```js
const justObservable = require('just-observable');
const observer = justObservable();

setTimeout(() => {
	observer.next('data1');
	observer.next('data2');	// will be missed by `toPromise`
}, 5);
setTimeout(() => {
	observer.next('data3');
}, 10);

const value1 = await observer.toPromise();
// value1: "data1"

const value2 = await observer.toPromise();
// value2: "data3"

await observer.toPromise(100);	// Optional timeout in 100ms
// Error: timeout
```

## Examples

### Create queue

```js
const justObservable = require('just-observable');
const observer = justObservable();

const unsubscribe = observer.subscribe((report) => queue.push(report));

(async () => {
	const queue = [];
	while (true) {
		if (queue.length === 0) {
			await observer.next();
		}

		while(queue.length > 0) {
			const row = queue.shift();
			console.log(row);
		}
	}
})();

observer.next('some data');
```

## License

Copyright (c) 2021, Michael Szmadzinski. (MIT License)

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