# ortum

> Minimal unidirectional state management library

Latest version **1.1.0** (published 2018-12-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install ortum
pnpm add ortum
yarn add ortum
bun add ortum
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2018-12-19 |
| First published | 2018-12-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 8 |
| Dependencies | 0 |
| Unpacked size | 21.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 29 |
| Author | Albert Groothedde |
| Maintainers | alber70g |

## Links

- npm: https://www.npmjs.com/package/ortum
- Repository: https://github.com/alber70g/ortum
- Homepage: https://github.com/alber70g/ortum#readme
- Issues: https://github.com/alber70g/ortum/issues
- npm.io page: https://npm.io/package/ortum

## Recent versions

- 1.1.0 (latest) — 2018-12-19
- 1.0.0 — 2018-12-19
- 0.0.1-4 — 2018-12-18
- 0.0.1-2 — 2018-12-13
- 0.0.1-0 — 2018-12-13

## README

Ortum
=====

![](https://img.shields.io/bundlephobia/min/ortum.svg)

Ortum is a small framework agnostic, unidirectional state 
management library.  
It's inspired by the work of André Staltz' on [Profunctor State Optics](https://github.com/staltz/use-profunctor-state)

Ortum allows your features to be independent from the global state while having 
the ability to use the global state.  
By using this, your features are decoupled from their environment, thus easy to test.


Getting started
---------------

Install into your project:

```bash
$ npm install --save ortum
```

> Give it a try at: https://stackblitz.com/edit/ortum-counter?file=src/counter.ts

### Usage in Javascript

Create an initial state, and use it to create the Profunctor.  
The `useProfunctor` returns an array with two elements: 
 - the profunctor: basically a store that holds the state and allows access and update it  
 using the functions `getState`, `setState` and `promap`.
 - an onStateChange that you can pass a callback of `(state) => console.log(state)`

```js
const { SimpleStateContainer, useProfunctor } = require('ortum');

const initialState = { 
  counter: 0, 
  authors: {
    1: {
      name: 'Andre Staltz',
      github: 'staltz'
    },
    2: {
      name: 'Albert Groothedde',
      github: 'alber70g',
    }
  }
  
};

// Create the app profunctor
const [appProf, onStateChange] = useProfunctor(
  new SimpleStateContainer(initialState),
);

// Listen to state changes
onStateChange((state) => console.log(state));

// Use the profunctor to get and set the state
console.log(appProf.getState()); 
// `{ counter: 0, authors: { .. }  }` 

// Set state using an Updater: prevState => newState
// After this statement you'll get a console.log with 
// // { counter: 1, authors: { .. }  }
appProf.setState((prevState) => {
  return Object.assign(
    {}, 
    prevState, 
    { counter: prevState.counter + 1 }
  )
});

// To create a substate profunctor
const authorsProf = appProf.promap(
  // map/getter
  (state) => state.authors,
  // unmap/setter
  (authors, prevState) => {
    return Object.assign({}, prevState, { authors: authors })
  }
)

console.log(authorsProf.getState()); 
// { 
//   counter: 1,
//   1: {
//     name: 'Andre Staltz',
//     github: 'staltz'
//   },
//   2: {
//     name: 'Albert Groothedde',
//     github: 'alber70g',
//   }
// }

```

---
_Source: https://npm.io/package/ortum · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
