Skip to content

Commit

Permalink
fix: npm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
orYoffe committed Jul 19, 2024
1 parent b9722bd commit 2d9cb51
Show file tree
Hide file tree
Showing 4 changed files with 3,608 additions and 3,450 deletions.
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
# JStates

A super small, simple and fast ⚡ JavaScript state library

Also checkout [JStates library for Reactjs](https://github.com/orYoffe/jstates-react)
A simple Observer (publisher - subscriber) pattern implementaion

[![NPM](https://nodei.co/npm/jstates.png)](https://npmjs.org/package/jstates)

Expand All @@ -35,6 +34,8 @@ npm i -S jstates

## Usage

### Counter

```js
import { createState } from "jstates";
// types exported: import { JStateGetState, SubFunction, JstateInstance } from "jstates";
Expand All @@ -56,33 +57,32 @@ myState.setState((state) => ({ counter: ++state.counter }));
// => onUpdate: counter changed to 2
```

## API

```js
const initialState = {};
const stateInstance = createState(initialState);
/* => returns state Instance
{
state,
subscribers,
setState,
subscribe,
unsubscribe,
};
*/
### Todos (TS)

// Get the state
stateInstance.state;
```ts
import { createState } from "jstates";
const todosState = createState<TodoState>({
todos: [],
});

// Change the state
stateInstance.setState(<object or a function that returns and object>);
// => returns a promise
function onUpdate(state: typeof todosState.state) {
console.log("onUpdate: todos changed to ", state.todos);
}

// Subscribe to state changes
stateInstance.subscribe(<function that will be called with the state on each update>);
todosState.subscribe(onUpdate);

function removeTodo(todo: string) {
todosState.setState((s: typeof todosState.state) => ({
todos: s.todos.filter((t: string) => t !== todo),
}));
}

// Unsubscribe from state changes
stateInstance.unsubscribe(<function that already subscribed>);
const addTodo = (todo: string) => {
todosState.setState((s: typeof todosState.state) => ({
todos: s.todos.concat(todo),
}));
};

addTodo("Buy milk");
addTodo("Buy eggs");
```
Loading

0 comments on commit 2d9cb51

Please sign in to comment.