Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

djinn-state

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

djinn-state

Powerful yet simple state machine

  • 2.0.0
  • next
  • latest
  • stable
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
decreased by-53.85%
Maintainers
1
Weekly downloads
 
Created
Source

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.

More information read the docs.

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.getState().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.getState()); // { token: 'someNewTokenHere' }

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

Keywords

FAQs

Package last updated on 03 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc