1.1.0 • Published 2 years ago

statebook v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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:
```typescript
// 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
})
  • String
  • Number
  • Arrays
  • 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

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.16

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.1.0

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.1.28

4 years ago

1.1.27

4 years ago

1.1.26

4 years ago

1.1.25

4 years ago

1.1.24

4 years ago

1.1.23

4 years ago

1.1.22

4 years ago

1.1.21

4 years ago

1.1.20

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.1

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

0.1.2

4 years ago

0.1.0

4 years ago