You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Anand Shukla
5269ec3c66
|
6 months ago | |
---|---|---|
.. | ||
es | 6 months ago | |
lib | 6 months ago | |
CHANGELOG.md | 6 months ago | |
README.md | 6 months ago | |
index.d.ts | 6 months ago | |
index.js | 6 months ago | |
license | 6 months ago | |
package.json | 6 months ago | |
ponyfill.d.ts | 6 months ago | |
ponyfill.js | 6 months ago |
README.md
symbol-observable
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
const symbolObservable = require('symbol-observable').default;
console.log(symbolObservable);
//=> Symbol(observable)
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:
- It's best if you just use one of the above libraries.
- If you're not, but sure you never
next
,error
orcomplete
on your observer aftererror
orcomplete
was called. - Likewise, make sure you don't
next
,error
orcomplete
afterunsubscribe
is called on the returned object.
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 - Check if a value is an Observable
- observable-to-promise - Convert an Observable to a Promise
License
MIT © Sindre Sorhus and Ben Lesh