Skip to content
Symbol.observable ponyfill
Branch: master
Clone or download
joshburgess and benlesh Typo fix (#39)
symbolObservable -> Symbol_observable
Latest commit e56e765 May 25, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
es feat(browser): Fully qualified import for native esm browser support (#… Nov 28, 2017
test
ts-test
.babelrc
.editorconfig
.gitattributes
.gitignore
.travis.yml
CHANGELOG.md chore(publish): 1.2.0 Jan 26, 2018
README.md
index.d.ts
index.js fix(bundlers): fix issue that caused some bundlers not to be able to … Jun 15, 2016
license
package-lock.json chore(publish): 1.1.0 Nov 28, 2017
package.json chore(publish): 1.2.0 Jan 26, 2018
tsconfig.json

README.md

symbol-observable Build Status

Symbol.observable ponyfill

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:

  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.
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

License

MIT © Sindre Sorhus and Ben Lesh

You can’t perform that action at this time.