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

statebook

v1.1.0

Published

Fast, Reliable, Light and Incredibely Easy State Manager. Stateful Data Structures for React

Downloads

14

Readme

Statebook JS

Stateful Data Structures for React

Introduction

Statebook is a lightweight, easy to use, loaded with features and extensible state manager built with TypesScript for React Apps ⚛️

Statebook Topics (States) use the observer pattern in the background to let functions subscribe and unsubscribe for state updates.

Topics are stateful data structures where each topic comes with a set of handy methods that can update the state with ease. In addition, each Topic has a status object (Ex: Error :x:, Success ✅, Info ℹ️, warning ❗, and loading :clock1:), subscribed functions can receive the status of the object too.

|:star:| Statebook Features | |--|--| | 🔹 | Easy to use and Lightweight | | 🔹 | Built-in react hooks | | 🔹 | Stateful Data Structures | | 🔹 | Each Topic has a status (Error, Success, Warning, Info) | | 🔹 | Topics can be subscribed to, updated and created OUTSIDE React Components | | 🔹 | Fast Learning curve |

Getting Started

Compatbility

  • Can be used with ES and CommonJS Modules.
  • Supports React 17.0.2 or later.

TypeScript

Statebook is built with typescript so it supports typings out of the box.

Installation

just install the package in your project:

npm i statebook

# OR

yarn add statebook

Usage

How to use statebook in a react app;

import { StatebookFactory, Topics } from 'statebook';

// create new statebook
const statebook = StatebookFactory({
  counter: Topics.Number(0), // add topic to statebook
});

// increase counter every second
setInterval(() => {
  const {counter} = statebook;
  counter.set((totalCount) => totalCount + 1);
}, [1000])

function App() {
  const [ totalCount ] = statebook.useTopic('counter');

  return (
    <div>
      <h1>The Counter will Increase every second</h1>
      <p>total counts: {totalCount}</p>
    </div>
  );
}

export default App;

Master Statebook with 3 steps! ✨

Step 1️⃣: Create Statebook Instance

// create new statebook
const statebook = StatebookFactory({
  user: Topics.Object({
    name: 'Jhon Smith',
    age: 34
  }),
  posts: Topics.Array<{slug: string; title: string; body?: string}>([])
});

Step :two:: Create Services


export async function getPosts() {
  statebook.posts.setStatus('loading' , 'Loading Posts');
  try {
    const posts = await // fetch posts from endpoint
    statebook.posts.setStatus('success' , 'Loaded Posts Successfully');
    statebook.posts.set(posts);
  } catch {
    statebook.posts.setStatus('error' , 'Failed to load Posts');
  }
}

Step :three:: Create Subscribers

Inside React Components:

// inside react components
function App() {
  const [ user ] = statebook.useTopic('user');
  const [posts, postsStatus] = statebook.useTopic('posts');

  useEffect(() => {
    getPosts();
  }, []);

  {/* Note: One status will be available at a time */}
  return (
    <div>
      <h1>Hi, {user.name}</h1>
      <h3>Posts List</h3>
      {postsStatus.loading && <p style={{color: gray}}>{postsStatus.loading}</p>}
      {postsStatus.error && <p style={{color: red}}>{postsStatus.error}</p>}
      {postsStatus.success && <p style={{color: green}}>{postsStatus.success}</p>}
      <div style={{marginTop: 10}}>
        {posts.map((post) => {
          <div key={post.slug}>
            <h4>{post.title}</h4>
            <p>{post.body}</p>
          </div>
        })}
      </div>
    </div>
  );
}

export default App;

Outside React Components:


// log list updates
statebook.posts.subscribe((list, status) => {
  console.log({list, status})
})

// inside react components
function App() {
  //...

That's It!! :confetti_ball:

Built-in Stateful Topics (DataTypes / Data Structures)

The following are the built-in topics that you can use immediatly in statebook. they can be created from Topics

import {StatebookFactory, Topics} from 'statebook';

const statebook = StatebookFactory({
  language: Topics.String('en'); // string
})
  • [X] String
  • [X] Number
  • [X] Arrays
  • [X] Objects / HashMaps
  • [ ] Stacks (in progress)
  • [ ] Queues (in progress)
  • [ ] Linked List (in progress)

Creating Custom Topics

Custom Topics can be created by creating a class and extending the Topic abstract class

import {Topic} from 'statebook';

class BinarySearchTree<T> {
  insert(val: T) {
    const node = new Node(val);
    // logic
    return node;
  }
  find() {
    // logic
  }
}

export class BinarySearchTreeTopic<T> extends Topic<BinarySearchTree<T>> {
  insert(value: T) {
    // set last inserted node in the state to trigger update
    this.set((state) => state.insert(value)) 
  }
}

Maintainer

Abdel Rahman Hussein