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

v1.0.0-rc.3

Published

Create properties in LitElements that watch for updates in a redux store

Downloads

512

Readme

lit-watch

MIT NPM

Create decorators from redux stores and observables like RxJS objects.

Install

npm i lit-watch

Usage

You can create a decorator from any observable:

import { createWatcher } from 'lit-watch';

// Create a @watch decorator
export const watch = createWatcher(observable);

Use it in your LitElements:

import { html, LitElement } from 'lit';
import { watch } from './watch';

export class Account extends LitElement {
    @watch((state) => state.user.fullName)
    public user!: string;

    render() {
        return html`
            Signed in as ${this.user}
        `;
    }
}

NOTE: Use public (do not use private / protected)

One of the goals of lit-watch was to force type-checking between the decorator output and the property. Due to a TypeScript limitation, however, the properties with a watching decorator have to be public. In a sense, from the scope of the decorator, we are accessing the class type from the outside, . Because any private or protected property will be invisible to the decorator, you may see a cryptic error when using these on any non-public properties.

NOTE: Do not set a default value

The decorator redefines the property with an accessor (get), so setting default values does not work. You can add a default in your select function by using a nullish coalescing operator (??).

// Do
@watch((state) => state.some)
public some!: string;

// Do not
@watch((state) => state.some)
public some: string = 'default value';

// For defaults
@watch((state) => state.some ?? 'default value')
public some!: string;

By using !, we are letting TypeScript know that this value will be set, so we do not get any errors while using the --strictPropertyInitialization option.

NOTE: Do not use before connectedCallback

Do not use lit-watch values in your constructor, as value accessors are initialized and subscribed in the connectedCallback and will unsubscribe in the disconnectedCallback.

Creating a decorator from a redux store

Since redux is interoperable with observable/reactive libraries you can watch redux stores like any other observable.

Create the decorator from your redux store:

import { createWatcher } from 'lit-watch';
import { store } from './store';

// Create a @watch decorator
export const watch = createWatcher(store);

Filtering, transforming and combining data with reselect

You can use reselect's createSelector to create complex queries.

One of the benefits of using reselect over writing your own function is that reselect uses memoization to prevent unnecessary updates when the source data has not been changed.

For more information about reselect, see the reselect docs.

The examples below show some inline use of reselect, but you are free to pass your selectors from elsewhere.

Filtering

@watch(createSelector(
    (state) => state.messages,
    (messages) => {
        // This only runs if state.messages changed
        return messages.filter((item) => !item.read);
    }
))
public unread!: Message[];

Transforming

@watch(createSelector(
    (state) => state.cart.items,
    (items) => {
        // This only runs if state.cart.items changed
        return items.reduce(
            (sum, item) => sum + item.price,
            0,
        );
    }
))
public total!: number;

Combining

@watch(createSelector(
    (state) => state.user.name,
    (state) => state.user.cart.items.length,
    (user, items) => {
        // This only runs if either state.user.name, or
        // state.user.cart.items.length changed (or both)
        return `${user} has ${items} in their cart`;
    }
))
public message!: string;

Multiple watchers

If you are planning on watching multiple observables, or maybe even multiple stores, it might make sens to name your decorators something more descriptive.

// @watchStore()
export const watchStore = createWatcher(store);

// @watchPrice()
export const watchPrice = createWatcher(priceObservable);
export class Account extends LitElement {
    @watchStore((state) => state.item.description)
    public description!: string;

    @watchPrice()
    public price!: number;

Use with Observables and RxJS

You can create watchers from RxJS (and other) observables by passing them to createWatcher.

const watchClimate = createWatcher(climateObservable);

License

MIT

Made by Paul Gerarts