# djinn-state

> Powerful yet simple state machine

Latest version **2.0.0** (published 2019-05-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install djinn-state
pnpm add djinn-state
yarn add djinn-state
bun add djinn-state
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-05-03 |
| First published | 2018-12-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 26.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Gilmar Quinelato |
| Maintainers | gilmarsquinelato |
| Keywords | javascript, typescript, nodejs, browser, application-state, state-management, services, djinn, @djinn |

## Links

- npm: https://www.npmjs.com/package/djinn-state
- Repository: https://github.com/djinn-state/djinn-core
- Homepage: https://github.com/djinn-state/djinn-core#readme
- Issues: https://github.com/djinn-state/djinn-core/issues
- npm.io page: https://npm.io/package/djinn-state

## Dependencies (1)

- [seamless-immutable](https://npm.io/package/seamless-immutable.md) ^7.1.4

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2019-05-03
- 1.3.1 — 2019-01-04
- 1.3.0 — 2018-12-27
- 1.2.1 — 2018-12-26
- 1.2.0 — 2018-12-26
- 1.1.0 — 2018-12-26
- 1.0.1 — 2018-12-26
- 1.0.0 — 2018-12-23
- 0.1.0 — 2018-12-23
- 0.0.2 — 2018-12-20
- 0.0.1 — 2018-12-20

## README

# Djinn-state

[![npm version](https://badge.fury.io/js/djinn-state.svg)](https://badge.fury.io/js/djinn-state)
[![Build Status](https://travis-ci.com/djinn-state/djinn-state.svg?branch=master)](https://travis-ci.com/djinn-state/djinn-state)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/dc067eab06d44cedbbc2dfceeeb8ec38)](https://www.codacy.com/app/djinn-state/djinn-state?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=gilmarsquinelato/djinn-state&amp;utm_campaign=Badge_Grade)
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/dc067eab06d44cedbbc2dfceeeb8ec38)](https://www.codacy.com/app/djinn-state/djinn-state?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=gilmarsquinelato/djinn-state&amp;utm_campaign=Badge_Coverage)

[![](https://img.shields.io/david/djinn-state/djinn-state.svg)](https://github.com/djinn-state/djinn-state)
[![](https://img.shields.io/npm/dw/djinn-state.svg)](https://www.npmjs.com/package/djinn-state)
[![](https://img.shields.io/bundlephobia/minzip/djinn-state.svg)](https://www.npmjs.com/package/djinn-state)


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](https://djinn-state.js.org).

## 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

* [djinn-state-react](https://github.com/djinn-state/djinn-state-react)
* [djinn-state-history](https://github.com/djinn-state/djinn-state-history)

## Install

npm `npm i --save djinn-state`

yarn `yarn add djinn-state`

## Using

```javascript
// 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
```

---
_Source: https://npm.io/package/djinn-state · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
