1 | # symbol-observable [![Build Status](https://travis-ci.org/benlesh/symbol-observable.svg?branch=master)](https://travis-ci.org/benlesh/symbol-observable)
|
---|
2 |
|
---|
3 | > [`Symbol.observable`](https://github.com/zenparsing/es-observable) [pony/polyfill](https://ponyfill.com)
|
---|
4 |
|
---|
5 | 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.
|
---|
6 |
|
---|
7 |
|
---|
8 | ## Install
|
---|
9 |
|
---|
10 | ```
|
---|
11 | $ npm install --save symbol-observable
|
---|
12 | ```
|
---|
13 |
|
---|
14 |
|
---|
15 | ## Basic Usage
|
---|
16 |
|
---|
17 | ```js
|
---|
18 | const symbolObservable = require('symbol-observable').default;
|
---|
19 |
|
---|
20 | console.log(symbolObservable);
|
---|
21 | //=> Symbol(observable)
|
---|
22 | ```
|
---|
23 |
|
---|
24 | ```ts
|
---|
25 | import Symbol_observable from 'symbol-observable';
|
---|
26 |
|
---|
27 | console.log(Symbol_observable);
|
---|
28 | //=> Symbol(observable)
|
---|
29 | ```
|
---|
30 |
|
---|
31 | ## Making an object "observable":
|
---|
32 |
|
---|
33 | You can do something like what you see below to make any object "observable" by libraries like RxJS, XStream and Most.js.
|
---|
34 |
|
---|
35 | Things to know:
|
---|
36 |
|
---|
37 | 1. It's best if you just use one of the above libraries.
|
---|
38 | 2. If you're not, but sure you never `next`, `error` or `complete` on your observer after `error` or `complete` was called.
|
---|
39 | 3. Likewise, make sure you don't `next`, `error` or `complete` after `unsubscribe` is called on the returned object.
|
---|
40 |
|
---|
41 | ```ts
|
---|
42 | import Symbol_observable from 'symbol-observable';
|
---|
43 |
|
---|
44 | someObject[Symbol_observable] = () => {
|
---|
45 | return {
|
---|
46 | subscribe(observer) {
|
---|
47 | const handler = e => observer.next(e);
|
---|
48 | someObject.addEventListener('data', handler);
|
---|
49 | return {
|
---|
50 | unsubscribe() {
|
---|
51 | someObject.removeEventListener('data', handler);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | },
|
---|
55 | [Symbol_observable]() { return this }
|
---|
56 | }
|
---|
57 | }
|
---|
58 | ```
|
---|
59 |
|
---|
60 | Often, it's not very hard, but it can get tricky in some cases.
|
---|
61 |
|
---|
62 | ## Related
|
---|
63 |
|
---|
64 | - [is-observable](https://github.com/sindresorhus/is-observable) - Check if a value is an Observable
|
---|
65 | - [observable-to-promise](https://github.com/sindresorhus/observable-to-promise) - Convert an Observable to a Promise
|
---|
66 |
|
---|
67 |
|
---|
68 | ## License
|
---|
69 |
|
---|
70 | MIT © [Sindre Sorhus](https://sindresorhus.com) and [Ben Lesh](https://github.com/benlesh)
|
---|