This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

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

1.2.3 • Public • Published

reistore - Relational Immutable State Manager

Make state managers greate again!

  1. Faster initialization compare to redux
  2. Native SSR support
  3. Transactions
  4. You need "Reducer" only when need to describe store relationships
  5. Comparable speed with redux
  6. Supports module architecture

Travis codecov GitHub issues GitHub license npm version npm downloads

Install

npm i reistore

Examples

Usage

Simple

import { 
    createStore,
    Path
} from "reistore";
 
const initState = {
    counter: 0
};
const store = createStore(undefined, initState);
const counter = Path.create(f => f.counter);
 
store.subscibe((state, changes) => {
  if(changes.some(change => change.in(counter))) { // check is counter value changed
    console.log("Counter value: ", state.counter);
  }
});
 
store.set(counter, 1);
// > Counter value: 1
const value = store.state.counter;
// value = 1

Batch API

You also can use batch api for executing series of commands. Batch always faster when you need execute more than one command.

import {
  createStore,
  Path
} from "reistore";
 
const initState = {
  counter: 0
};
const store = createStore(undefined, initState);
const counter = Path.create(f => f.counter);
 
store.batch(instructor => {
  store.set(counter, 1);
  instructor.set(counter, 2);
 
  console.log(store.state.counter);
  // value = 0
 
  store.set(counter, 3);
});
 
console.log(store.get(counter)); // same as store.state.counter
// value = 3

Injection API

You also can use injection API that give you access to state in batch and access to store modification.

import {
  createStore,
  Path
} from "reistore";
 
const initState = {
  counter: 0
};
const store = createStore(undefined, initState);
const counter = Path.create(f => f.counter);
 
store.batch(instructor => {
  instructor.set(counter, 1);
  instructor.inject((state, instructor) => {
    console.log(state.counter);
    // value = 1
    instructor.set(counter, state.counter + 1);
  });
 
  console.log(store.state.counter);
  // value = 0
 
  instructor.set(counter, v => v + 3);
});
 
console.log(store.get(counter));
// value = 5

Min-Max transform

import {
  createStore,
  createSchema,
  Path
} from "reistore";
 
const initState = {
  min: 0,
  max: 0
};
const path = {
  min: Path.create(f => f.min),
  max: Path.create(f => f.max)
}
function* transformator(change, { state, set }) {
  yield change; // apply change to state
  if (state.min > state.max) {
    yield set(path.max, change.value); // apply change to max if min > max
  } else if (state.max < state.min) {
    yield set(path.min, change.value); // apply change to max if max < min
  }
}
const store = createStore(undefined, initState, transformator);
 
store.set(path.min, 1);
console.log(store.state);
// { min: 1, max: 1 }
 
store.set(path.max, v => v - 10);
console.log(store.state);
// { min: -9, max: -9 }
 
store.set(path.min, -15);
// { min: -15, max: -9 }
console.log(store.state);

Scope

import {
  createStore,
  createSchema,
  createScope,
  Path
} from "reistore";
 
const initState = {
  sum: 0
};
const scopeInitState = {
  min: 0,
  max: 0
}
const schema = createSchema(initState);
 
function* transformator(change, {state, set, scope}) {
  if (change.in(path.min) && change.value > scope.max) { // if changed min and new value(min) > state.scope.max 
    yield set(path.max, change.value);
  } else if (change.in(path.max) && change.value < scope.min) { // if changed max and new value(max) < state.scope.min
    yield set(path.min, change.value);
  }
  yield change; // apply change to state
  yield set(path.sum, state.scope.max + state.scope.min); // update sum
}
const scope = createScope(schema, f => f.scope, scopeInitState, transformator);
const path = {
  sum: Path.create(f => f.sum),
  min: scope.joinPath(f => f.min),
  max: scope.joinPath(f => f.max)
}
const store = createStore(schema);
 
store.set(path.min, 1);
console.log(store.get(scope));
// { min: 1, max: 1 }
console.log(store.state);
// { sum: 2, scope: { min: 1, max: 1 } }

Package Sidebar

Install

npm i reistore

Weekly Downloads

31

Version

1.2.3

License

MIT

Unpacked Size

129 kB

Total Files

91

Last publish

Collaborators

  • wroud