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

redom-state

v1.5.0

Published

State for redom apps

Downloads

34

Readme

State management made for RE:DOM apps

Install

yarn add redom-state
// or
npm i redom-state

Usage

Define your state using default state helper.

import state from "redom-state";
import { mount } from "redom";
import App from "./App";

state(mount(document.body, new App()), () => ({ value: 0 }));

Define your state with async bootstrap.

import state from "redom-state";
import { mount } from "redom";
import App from "./App";

state(mount(document.body, new App()), () => [
  { state: "loading" }, // Setup your init state
  promise1(),
  promise2(),
  { state: "none" },
]);

Each elements in array returned by init function will be resolved and merged with state, then application will be updated with new version state. This should helps to manage loaders and progress information.

Define your state with generator function.

import state from "redom-state";
import { mount } from "redom";
import App from "./App";

state(mount(document.body, new App()), async function* () {
  yield { state: "loading" }; // returns immediately
  const result = await promise1();
  yield result;
  yield await promise2(result); // uses result of previus call
});

Generator function is most complex way to bootstrap state. Each yield is merged with currend state and application is updated.

Updating state

Updating state is available through specially defined function using wire helper. Each call of this function will update state and triggers application update. Each wired function returns whole new state. It means that on module side there is no merge or diff. All merging should be done by wired function. Wired function should not handle async calls, promises are not resolved. Async update can be done differently.

import { wire } from "redom-state";

const switchPage = wire((state, page) => ({
  ...state,
  page,
}));

This example presents simple page switcher. That function can be experted and used across whole app in redom components.

Fetching chunk of state

State should be passed by update function and received by parrent but sometimes it maight be need to fetch some data directly from state with predefined filter or reducer. Use pick function to define special shortcuts to data. As wired function also pickers can be shared accross whole app by export/import module

import { pick } from "redom-state";

// no arguments
const getPage = pick((state) => state.page);

// more arguments
const listPages = pick((state, title, author) => state.pages.filter(page => page.title.includes(title) && page.author.includes(author));

Sometimes we want to call this function many times in one cycle, to prevent repeating havy operations you can add information how to build cache key for this call.

const picker = pick(
  (state, title, author) => {},
  (title, author) => `pages-${title}-${author}`
);

Dealing with async calls

For example if you want fetch from server page contents before you show new page.

import { wire, pick } from "redom-state";

// set view state
const setViewState = wire((state, viewState) => ({
  ...state,
  viewState
}));

// stash page object
const stashPage = wire((state, page) => ({
  ...state
  pages: {
    ...pages,
    ...page
  }
}))

// set current page
const setCurrentPage = wire((state, currentPage) => ({
  ...state,
  currentPage
}));

// switch page with conditional assync call
const switchPage = pick((state, page) => {
  if( !state.pages[page] ){
    setViewState("loading");
    fetch(`/pages/${page}`).then(response => response.json()).then( body =>  {
      stashPage({ [page]: body });
      setCurrentPage(page);
      setViewState("ready");
    });
  }else{
    setCurrentPage(page);
  }
});

Define own state object

With helper function also state class is provided. You can define and expose your own state for app.

import { RedomState } from "redom-state";
import { mount } from "redom";

const state = new RedomState(null, () => ({
  value: 0,
}));

export const { wire, pick, app, run } = state.export();

Example

Define your component with state functions

// Counter.js
import {wire} from "redom-state";
import {el, text} from "redom";

export const add = wire((state) => ({
  value: state.value + 1
}));

export const sub = wire((state)) => ({
  value: state.value - 1
}));

export const set = wire((state, payload) => ({
  value: payload
}));

export default class Counter {
  constructor() {
    this.el = el('.counter',
      this.add = el('button.add', text("+")),
      this.sub = el('button.sub', text("-"))
    );

    this.add.onclick = e => add();
    this.sub.onclick = e => sub();
  }
  update(){}
}

Define your app class

Reuse previously defined actions

// App.js
import { el, text } from "redom";
import Counter, { set } from "./Counter";

export default class App {
  constructor() {
    this.el = el(
      ".app",
      (this.value = el("input.value", { type: "text" })),
      (this.counter = new Counter()),
      (this.reset = el("button.reset", text("reset")))
    );
    this.reset.onclick = (e) => set(0);
  }

  update(state) {
    this.value.value = state.value;
  }
}

Bootstrap application

// bootstrap.js
import state from "redom-state";
import { mount } from "redom";
import App from "./App";

state(mount(document.body, new App()), () => {
  return {
    value: 0,
  };
});