npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lit-observe

v1.0.1-a

Published

Super simple observable / reactive library for lit-element to manage state.

Downloads

3

Readme

lit-observe

Simple observable / reactive library for lit-element to manage state.

Why

State management gets complex real quick. For lit-element the primary need for state is to know when some resource changes so that we can update our web component. You can drive yourself crazy with actions, hooks, or what not, but here's a simple system.

Instead of trying to manage large pieces of state, here we allow LitElement components to bind to special objects, with class Observable. An observable is like a normal Object, but it remembers subscribers via observable.subscribe(callback), and tells everyone when it updates via observable.notify().

Now custom elements can manage their internal state with normal properties, but they can also update automatically when observables change. An observable could be as complex as a Store object that manages state across the client, or they could be as targeted as a single resource. Up to you.

Install

Install from npm:

> npm install --save lit-observe

Easy to use

When we create our element, let's do it like this. Our example is an article header:

// ArticleHeader.js
import { observer, Observable } from 'lit-observe';
import { user } from './auth.js';

class ArticleHeader extends observer(LitElement) {
  static get properties() {
    return {
      article: {type: Object, observe: true}
    };
  }

  static get observing() {
    return [user];
  }

  render() {
    if (this.article.loading) {
      return html`Loading...`;
    }
    if (user.isAdmin) {
      return html`${this.article.title}
      <a href="${this.article.url}/edit">[edit]</a>`;
    }
    return html`${this.article.title}`;
  }
}
customElement.define('article-header', ArticleHeader);

// Use the article header in another component
export const article = new Observable({loading: true});

fetch(ARTICLE_URL).then(response => {
  Object.assign(article, response.json());
  article.loading = false;
  article.notify();   // --> Our component updates!
});

return
  html`<article-header .article=${article}></article-header>`;

// Some other code
user.isAdmin = true;
user.notify() // --> Our component updates!

Here's a brief breakdown of what's happens. If you are confused, or want this broken down further, create an issue in this repo.

observer is a mixin that lets our element actually observe. It automatically hooks on .connectedCallback() and unhooks on .disconnectedCallback() to observables marked with { observe: true }, and also any observables listed in the get observing() static method.

Observable is expected to be a base class to whatever your main objects are, but you can also use it plain. Here, we create the article with .loading = true. In our component, we look render a 'Loading...' as long as that is true. Once our fetch comes back, we move all of the data to it (via Object.assign()) and then run .notify(). All subscribing observers, including our element now update themselves with a call to .requestUpdate() via the normal LitElement lifecycle.

user.notify() will also update our element, because we have subscribed to it in our static get observing() method.

RxJS

The Observable class matches a very small subset of the RxJS Observable interface, so you could swap those in.

API Reference

Sorry, please look at the source. If you'd appreciate a real api reference, please create an issue in this repo.

Running the Tests

npm test