Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

symbol-observable

blesh64.2mMIT4.0.0TypeScript support: included

Symbol.observable ponyfill

symbol, observable, observables, ponyfill, polyfill, shim

readme

symbol-observable Build Status

Symbol.observable pony/polyfill

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:

  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.

License

MIT © Sindre Sorhus and Ben Lesh

changelog

4.0.0 (2021-04-15)

Bug Fixes

  • If Symbol.for doesn't exist, just use Symbol (ef81924), closes #53

Code Refactoring

  • Revert to symbol from unique symbol, the latter caused compatibility issues and provided no real benefit. (72ce7dc)

BREAKING CHANGES

  • Following the advice of the TypeScript team, the type for Symbol.observable is reverted back to symbol from unique symbol. This is to improve compatibility with other libraries using this module. Sincerely sorry for the trashing. Getting the types right for modules is hard. If this continues to cause issues for you, please file an issue with the TypeScript project.

3.0.0 (2020-11-02)

Bug Fixes

  • TypeScript: Symbol.observable is now unique symbol. (2ef1476), closes #41
  • TypeScript: Symbol[Symbol.observable] is no longer incorrectly defined (fe4301f)

BREAKING CHANGES

  • TypeScript: Older versions of @types/node will clash with this, because @types/node incorrectly defined Symbol.observable has symbol and not unique symbol. Update to @types/node@14.14.6 or higher.

2.0.3 (2020-09-30)

Bug Fixes

  • package.json ponyfill.d.ts added to package (b25f5bf)

2.0.2 (2020-09-29)

Bug Fixes

  • TypeScript ponyfill types added (10d2ec8)

2.0.1 (2020-09-04)

Bug Fixes

  • package.json: es/ponyfill.js no longer typoed (ad2c079)
  • Resolve issues in environments with frozen Symbol (2323882)

2.0.0 (2020-09-01)

Bug Fixes

  • Resolve issues in environments with frozen Symbol (2323882)

BREAKING CHANGE: This moves to using Symbol.for to create the symbol instance. It's possible that some runtimes that support Symbol do not support Symbol.for. Therefor, I'm marking this as a breaking change. If you find that you hit this issue, please report it. You can work around it by polyfilling Symbol.for.

1.2.0 (2018-01-26)

Bug Fixes

  • TypeScript: Remove global Symbol declaration (427c3d7)
  • common js usage example (#30) (42c2ffa)

Features

  • bundlers: Add module and main entries in package.json (#33) (97673e1)

1.1.0 (2017-11-28)

Bug Fixes

  • TypeScript: update TS to 2.0, fix typings (e08474e), closes #27

Features

  • browser: Fully qualified import for native esm browser support (#31) (8ae5f8e)
  • index.d.ts: add type info to Symbol.observable (e4be157)

1.0.4 (2016-10-13)

Bug Fixes

  • global: global variable location no longer assumes module exists (4f85ede), closes #24

1.0.3 (2016-10-11)

Bug Fixes

  • mozilla addons support: fix obtaining global object (#23) (38da34d), closes #23

1.0.2 (2016-08-09)

Bug Fixes

  • ECMAScript 3: ensure output is ES3 compatible (3f37af3)

1.0.1 (2016-06-15)

Bug Fixes

1.0.0 (2016-06-13)

Bug Fixes

  • index.js: use typeof to check for global or window definitions (#8) (5f4c2c6)
  • types: use default syntax for typedef (240e3a6)
  • TypeScript: exported ponyfill now works with TypeScript (c0b894e)

Features

  • es2015: add es2015 implementation to support rollup (#10) (7a41de9)

BREAKING CHANGES

  • TypeScript: CJS users will now have to require('symbol-observable').default rather than just require('symbol-observable') this was done to better support ES6 module bundlers.

0.2.4 (2016-04-25)

Bug Fixes

  • IE8 support: Ensure ES3 support so IE8 is happy (9aaa7c3)
  • Symbol.observable: should NOT equal Symbol.for('observable'). (3b0fdee), closes #7

0.2.3 (2016-04-24)

Bug Fixes

  • IE8/ECMAScript 3: Make sure legacy browsers don't choke on a property named for. (9aaa7c)

0.2.2 (2016-04-19)

Features

  • TypeScript: add TypeScript typings file (befd7a)

0.2.1 (2016-04-19)

Bug Fixes

  • publish: publish all required files (5f26c3a)

0.2.0 (2016-04-19)

Bug Fixes

  • Symbol.observable: ensure Symbol.for('observable') matches Symbol.observable (ada343f), closes #1 #2