1. djinn-state
Powerful yet simple state machine
djinn-state
Package: djinn-state
Created by: djinn-state
Last modified: Thu, 28 Apr 2022 23:14:35 GMT
Version: 2.0.0
License: MIT
Downloads: 36
Repository: https://github.com/djinn-state/djinn-core

Install

npm install djinn-state
yarn add djinn-state

Djinn-state

npm version
Build Status
Codacy Badge
Codacy Badge



A powerful yet simple application state management.

The Djinn-state was developed with the objective to be less verbose and simple to maintain and scale Javascript applications.

Features

  • Supported in browsers and NodeJs
  • Simple to implement
  • Register and retrieve registered services by just giving the class of the service you want
  • Singleton and scoped services
  • Subscribe and unsubscribe to service state changes

Libraries

Install

npm npm i --save djinn-state

yarn yarn add djinn-state

Using

 // djinn.js
import { Djinn, DjinnService } from 'djinn-state';

const djinn = new Djinn();

// AuthService.js
class AuthService extends DjinnService {
  state = {
    token: '',
  };
  
  authenticate() {
    this.patch({
      token: 'someNewTokenHere',
    });
  }
}

// HttpService.js
class HttpService extends DjinnService {
  initService() {
    super.initService();
    this.authService = djinn.getService(AuthService);
  }
  
  get(url) {
    const token = this.authService.state.token;
    const headers = {
      'Authorize': `Bearer ${token}`,
    };
    
    makeHttpRequest(url, headers);
  }
}

// djinnServices.js
djinn.register(AuthService);
djinn.register(HttpService);
djinn.start();

// myPage.js
const authService = djinn.getService(AuthService);

const onStateUpdate = (update) => {
  console.log(update); // { token: { current: 'someNewTokenHere', previous: '' } }  
};

const unsubscribe = authService.subscribe(onStateUpdate);

authService.authenticate();
// onStateUpdate() called

console.log(authService.state); // { token: 'someNewTokenHere' }

unsubscribe(); // Don't listen to changes anymore

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code