New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rx-lit

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rx-lit

Base class for LitElement that makes working with Observables easier.

  • 1.0.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

Rx Lit

Build npm version

Reasoning

Working with Observables inside a LitElement component is easy enough, but it involves a bit of code. We need at least to subscribe, unsubscribe and make sure we schedule an update when we get a new value. An implementation might look like this:

class DemoElement extends LitElement {
  unsubscribe$ = new Subject();
  streamValues: number;

  connectedCallback() {
    super.connectedCallback();
    stream$.pipe(takeUntil(this.unsubscribe$)).subscribe((res) => {
      this.streamValues = number;
      this.requestUpdate();
    });
  }

  disconnectedCallback() {
    super.disconnectedCallback();
    this.unsubscribe$.next();
  }
}

We can avoid writing this code for each Observable using RxLitElement base class.

RxLitElement

RxLitElement is a base class, that extends LitElement, and provides a subscribe method.

Definition: subscribe(propKey, stream$). We pass in the property we want to assign values to and the Observable we want to subscribe.

Using it we can rewrite the above code:

class DemoElement extends RxLitElement {
  streamValues: number;

  connectedCallback() {
    super.connectedCallback();
    this.subscribe('streamValues', stream$);
  }
}

Beside abstracting away some of the code, we have some other benefits:

  • type safety (we can use only existing property names and Observable objects)
  • unsubscribes when the component is removed
  • unsubscribes from old observable if called again on the same property with different Observable
  • ignores calls on the same property with the same Observable

Decorator

In some cases, we can use a decorator to simplify further. If the Observable we want to subscribe to is not a property of our class, we can simplify the code:

class DemoElement extends RxLitElement {
  @subscribe(stream$)
  streamValues: number;

  @subscribe(from([1]))
  anotherStreamValues: number;
}

Why

Why not a simple function?

You cannot monkey patch life cycle hooks of custom elements. This means we cannot extend the functionality of disconnectedCallback to know when the element was removed and unsubscribe.

Why not a lit-html directive?

There is not straight forward way of knowing when the element is removed. So again, we cannot unsubscribe from our Observable.

If you know a solution to this problems or have a different idea, feel free to open a pull request :)

Keywords

FAQs

Package last updated on 12 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc