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

2.0.0 • Public • Published

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

Package Sidebar

Install

npm i djinn-state

Weekly Downloads

13

Version

2.0.0

License

MIT

Unpacked Size

26.9 kB

Total Files

17

Last publish

Collaborators

  • gilmarsquinelato