# symbol-observable

> Symbol.observable ponyfill

Latest version **4.0.0** (published 2021-04-15) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2021-04-15 |
| First published | 2015-11-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=0.10 |
| Dependencies | 0 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 187 |
| Author | Ben Lesh |
| Maintainers | gaearon, blesh, jayphelps |
| Keywords | symbol, observable, observables, ponyfill, polyfill, shim |

## Links

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

## Recent versions

- 4.0.0 (latest) — 2021-04-15
- 3.0.0 — 2020-11-02
- 2.0.3 — 2020-09-30
- 2.0.2 — 2020-09-29
- 2.0.1 — 2020-09-04
- 2.0.0 — 2020-09-01
- 1.2.0 — 2018-01-26
- 1.1.0 — 2017-11-28
- 1.0.4 — 2016-10-13
- 1.0.3 — 2016-10-11
- 1.0.2 — 2016-08-09
- 1.0.1 — 2016-06-15
- 1.0.0 — 2016-06-13
- 0.2.4 — 2016-04-25
- 0.2.3 — 2016-04-24
- … 4 more at https://npm.io/package/symbol-observable/versions

## README

# symbol-observable [![Build Status](https://travis-ci.org/benlesh/symbol-observable.svg?branch=master)](https://travis-ci.org/benlesh/symbol-observable)

> [`Symbol.observable`](https://github.com/zenparsing/es-observable) [pony/polyfill](https://ponyfill.com)

This will polyfill `Symbol.observable` if `Symbol` exists, but will not polyfill `Symbol` if it doesn't exist. Meant to be used as a "ponyfill", meaning you're meant to use the module's exported symbol value as described below. This is all done to ensure that everyone is using the same version of the symbol (or string depending on the environment), as per the nature of symbols in JavaScript.


## Install

```
$ npm install --save symbol-observable
```


## Basic Usage

```js
const symbolObservable = require('symbol-observable').default;

console.log(symbolObservable);
//=> Symbol(observable)
```

```ts
import Symbol_observable from 'symbol-observable';

console.log(Symbol_observable);
//=> Symbol(observable)
```

## Making an object "observable":

You can do something like what you see below to make any object "observable" by libraries like RxJS, XStream and Most.js.

Things to know:

1. It's best if you just use one of the above libraries.
2. If you're not, but sure you never `next`, `error` or `complete` on your observer after `error` or `complete` was called.
3. Likewise, make sure you don't `next`, `error` or `complete` after `unsubscribe` is called on the returned object.

```ts
import Symbol_observable from 'symbol-observable';

someObject[Symbol_observable] = () => {
  return {
    subscribe(observer) {
      const handler = e => observer.next(e);
      someObject.addEventListener('data', handler);
      return {
        unsubscribe() {
          someObject.removeEventListener('data', handler);
        }
      }
    },
    [Symbol_observable]() { return this }
  }
}
```

Often, it's not very hard, but it can get tricky in some cases.

## Related

- [is-observable](https://github.com/sindresorhus/is-observable) - Check if a value is an Observable
- [observable-to-promise](https://github.com/sindresorhus/observable-to-promise) - Convert an Observable to a Promise


## License

MIT © [Sindre Sorhus](https://sindresorhus.com) and [Ben Lesh](https://github.com/benlesh)

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