1. symbol-observable
Symbol.observable ponyfill
symbol-observable
Package: symbol-observable
Created by: blesh
Last modified: Mon, 27 Jun 2022 03:05:38 GMT
Version: 4.0.0
License: MIT
Downloads: 54,703,094
Repository: https://github.com/blesh/symbol-observable

Install

npm install symbol-observable
yarn add symbol-observable

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

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code