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

subjective

v6.3.0

Published

Opinionated state management

Downloads

6

Readme

Subjective

  • Opinionated state management
  • Type safety by design. Type inference works for both Update and Selector functions.
  • Logging. Inspect where Update function was called.
  • Pause observable stream, if needed.
  • Always receive the whole state, if needed.

Concepts

State

const state = new Subjective(
    productState,
    productStateFns,
);
Default Logger
const state = new Subjective(
    productState,
    productStateFns,
    // use default Logger (dev only)
    true
);
Custom Logger
const state = new Subjective(
    productState,
    productStateFns,
    // use custom Logger (dev only)
    (updateFnName: string, payload: any, updateFnRef: Function) => {
        // LOG
        const data = JSON.stringify(payload);
        const dataTrimmed = data.substring(0, 80);
        // logging to console
        console.groupCollapsed(
            `%c${fnName}: %c${dataTrimmed}${
                dataTrimmed.length < data.length ? '…' : ''
            }`,
            `color: green; font-weight: 300;`,
            `color: gray; font-weight: 100;`,
        );
        console.log(payload);
        console.log(updateFnRef);
        console.groupEnd();
    }
);

EXAMPLE

Selector function

Subscribe to state.filter.type and notify with its value
state.select(s => s.filter.type).subscribe();
Subscribe to state.filter.type and notify with a whole state
state.select(s => s.filter.type, true).subscribe();

EXAMPLE

Update function

Update value of state.filter.type
state.update(f => f.updateFilterType, type);
Update value of state.filter.type and do not notify subscribers
state.update(f => f.updateFilterType, type, false);
Update value of state.filter.type and return updated state
const updatedState = state.update(f => f.updateFilterType, type);

EXAMPLE

Examples

NOTES

Immutable pattern

Always use immutable pattern otherwise it will not work. We can't rely on mutations since object reference is always the same.

Type-safety

Types are always inferred either from state class or payload parameter of the update function.

FUTURE

I've been thinking of this new syntax...check it out.

Credits