source: trip-planner-front/node_modules/symbol-observable/README.md@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.2 KB
Line 
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
5This 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
18const symbolObservable = require('symbol-observable').default;
19
20console.log(symbolObservable);
21//=> Symbol(observable)
22```
23
24```ts
25import Symbol_observable from 'symbol-observable';
26
27console.log(Symbol_observable);
28//=> Symbol(observable)
29```
30
31## Making an object "observable":
32
33You can do something like what you see below to make any object "observable" by libraries like RxJS, XStream and Most.js.
34
35Things to know:
36
371. It's best if you just use one of the above libraries.
382. If you're not, but sure you never `next`, `error` or `complete` on your observer after `error` or `complete` was called.
393. Likewise, make sure you don't `next`, `error` or `complete` after `unsubscribe` is called on the returned object.
40
41```ts
42import Symbol_observable from 'symbol-observable';
43
44someObject[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
60Often, 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
70MIT © [Sindre Sorhus](https://sindresorhus.com) and [Ben Lesh](https://github.com/benlesh)
Note: See TracBrowser for help on using the repository browser.