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

create-project-state

v0.0.2

Published

```bash npm i create-project-state # or yarn add create-project-state ```

Downloads

5

Readme

Installation

npm i create-project-state
# or
yarn add create-project-state

What is this?

This is an opinionated state management library for editors. It comes with some neat features:

  • :white_check_mark: Fully typed
  • :white_check_mark: Built-in undo stack
  • :white_check_mark: Optional React hooks
  • :white_check_mark: Stateless state management
  • :white_check_mark: Tiny - around 2KB, <1KB gzipped

What is "Stateless state management"?

When you use a state management method like useState, useReducer or redux, it will manage both modifying the state and holding the state. In this library, only the management (mutation) of the state is handled, and your state can be held in a separate object, in another library, in a WASM module, on a remote server or really anything accessible in JavaScript. This allows this library to be used in a lot more cases, and to make adding realtime collaboration easier by allowing to decouple the state holding onto a server.

What is this for?

As the name suggests, this library has a focus on being used inside editors, IDEs, etc. Features like the undo stack, realtime collaboration and serializing the whole project to a file can be harder tasks, which are made easier using this.

  • If your state is held in an object representing the project, you can just JSON.stringify it.
  • If you want to add an undo/redo feature to your editor, it is included with this library.
  • If you want to add realtime collaboration to your editor, it can be done in less than an hour using this library.

Examples

Basic example

import { createProjectState } from "create-project-state";

// This is the object that holds the state
const name = { surname: "foo", name: "bar" };

// Create a project state that will manage the states of the individual components
const { createState } = createProjectState();

// Create a state manager for the name object
const NameState = createState({
  name: "nameState",
  getState: () => {
    return `${name.surname} ${name.name}`;
  },
  dispatchers: {
    setSurname: (newName: string) => {
      name.surname = newName;
    },
    setName: (newSurname: string) => {
      name.name = newSurname;
    },
  },
});

console.log(NameState.getState()); // "foo bar"
NameState.dispatchers.setSurname("foobar");
console.log(NameState.getState()); // "foobar bar"

Undo stack example

import { createProjectState } from "create-project-state";

const { undoStack, createState } = createProjectState();

let myState = "foo";
const state = createState({
  name: "myState",
  getState: () => myState,
  dispatchers: {
    setState: (newState: string) => {
      const oldState = myState;
      myState = newState;
      return () => {
        myState = oldState;
      };
    },
  },
});

const { setState } = state.dispatchers;
console.log(myState); // "foo"
setState("bar");
console.log(myState); // "bar"
undoStack.undo();
console.log(myState); // "foo"
undoStack.redo();
console.log(myState); // "bar"

React example

import { createProjectState } from "create-project-state";
import { useProjectState } from "create-project-state/react";

// This is the object that holds the state
const name = { surname: "foo", name: "bar" };

// Create a project state that will manage the states of the individual components
const { createState } = createProjectState();

// Create a state manager for the name object
const NameState = createState({
  name: "nameState",
  getState: () => {
    return `${name.surname} ${name.name}`;
  },
  dispatchers: {
    setSurname: (newName: string) => {
      name.surname = newName;
    },
    setName: (newSurname: string) => {
      name.name = newSurname;
    },
  },
});

// Now we make a React component to display and interact with the state:
const MyComponent = () => {
  // Using this hook, the component will update whenever the state is changed even though it isn't managed by react.
  const [name, { setSurname, setName }] = useProjectState(NameState);

  return (
    <>
      <p>{name}</p>
      <input onChange={(e) => setSurname(e.target.value)}>Surname</input>
      <input onChange={(e) => setName(e.target.value)}>Name</input>
    </>
  );
};