Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

sobanieca/minst

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Introduction

(This project is "work in progress...")

minst is a minimalistic state manager that is intended to be super simple to use and avoid boilerplate code as much as possible. It is also supposed to utilize latest features of modern JavaScript language, thus it's not compatible with pre-ES6 runtimes.

Getting Started

  1. Installation

npm install minst

  1. Usage

Inside moduleA.js:

// moduleA.js
import stateManager from "minst";

// get state that one wants to use, open it in ReadWrite mode by providing
// writerName
let sampleState = stateManager.getState("sample", "writerName");

// modify state:
sampleState.field1 = 10;

Inside moduleB.js:

// moduleB.js
import stateManager from "minst";

// get state in ReadOnly mode by not providing writerName parameter:
let sampleState = stateManager.getState("sample");

// subscribe to all state changes:
stateManager.subscribe("sample", (previousValue, newValue, writerName) => {
    console.log("State has changed!");
    console.log(`Previous value: ${JSON.stringify(previousValue)}`);
    console.log(`New value: ${JSON.stringify(newValue)}`);
    console.log(`Changes done by: ${writerName}`);
});

Remarks

Once you obtain state object via getState() method, do not try to replace it:

let state = stateManager.getState("sample");
state = { field1: 10 };

getState() method returns proxy object where each change can be trackable. If you replace it with new object, you are loosing change tracking. Not to mention that actual state will remain unchanged.

If you want to replace whole state object, use replaceOne(stateName, value) method.

Motivation

Why use minst instead of currently existing state managers? Currently JavaScript frameworks are very complex and thus require many time spent on learning and troubleshooting issues. For instance, state managers require you to define stores, commands, queries and introducing additional layers to your application. With ES6 Javascript has changed significantly and it's possible to write complex applications without using complex tools. However, it still lacks simple to use 'observable' capabilities and that's where minst is supposed to shine. It saves you from writing boilerplate code.

To summarize:

Because it's way simpler => lowers development cost Because it supports modern JavaScript Because you don't need to support pre-ES6 runtimes (older browsers like IE for instance)*

*If for any reason you need to support older runtimes - you will most likely need to stick to current frameworks and tools.

Build and Test

npm i
npm run lint
npm run test

Contribute

You are more then welcome to submit a PR. Especially if it's about bugfix. In case of new features, please submit an issue first to discuss it.

This project is supposed to be minimalistic and way simpler then currently available state managers, so new features need to be carefully discussed

About

minst - minimalistic state management

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published