primitate
TypeScript icon, indicating that this package has built-in type declarations

5.2.1 • Public • Published

Primitate

Build Status

Primitate is a javascript library for managing state of your app by the only two methods.

Methods

  • createAction: Create a function the only way to change the state.
  • subscribe: Emit functions when state was changed by the Action.

Official packages

Install

npm install --save primitate

How to use

Case 1: Initial state is a primitive value or Array

/* import { Primitate } from "primitate"  // ES2015 modules style */
const { Primitate } = require("primitate");
 
// ---------------------------
// 1. Create Primitate Item
// ---------------------------
const Counter = Primitate(0);
 
 
// ---------------------------
// 2. Create Action
// ---------------------------
function increment(x) {
  return x + 1;
}
const increment$ = Counter.createAction(increment);
 
 
// ---------------------------
// 3. Subscribe
// ---------------------------
const unsubscribe =
  Counter.subscribe( count => console.log(count) );
 
 
// ---------------------------
// 4. Emit Action !!
// ---------------------------
increment$();
increment$();
// console.log: 2

Case 2: Initial state is an object

const { Primitate } = require("primitate");
 
 
// ---------------------------
// 1. Create Primitate Item
// ---------------------------
const Counter = Primitate({ count1: 0, count2: 0 });
 
 
// ---------------------------
// 2. Create Action
// ---------------------------
function increment(x) {
  return x + 1;
}
const increment$ = Counter.createAction(
  increment,
  state => state.count1
);
 
 
// ---------------------------
// 3. Subscribe
// ---------------------------
const unsubscribe = Counter.subscribe(
  state => console.log(state),
  [state => state.count1]
);
 
  
// ---------------------------
// 4. Emit Action !!
// ---------------------------
increment$();
increment$();
// console.log({ count1: 2, count2: 0 });

Documentation

In preparation...

Lisence

MIT

Package Sidebar

Install

npm i primitate

Weekly Downloads

0

Version

5.2.1

License

MIT

Last publish

Collaborators

  • yoshibu