# subjective

> Opinionated state management

Latest version **6.3.0** (published 2019-04-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install subjective
pnpm add subjective
yarn add subjective
bun add subjective
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.3.0 |
| Published | 2019-04-30 |
| First published | 2017-11-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Unpacked size | 44.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | otodockal |
| Maintainers | otodockal |
| Keywords | RxJS, state, Angular, React, Redux |

## Links

- npm: https://www.npmjs.com/package/subjective
- Repository: https://github.com/otodockal/subjective
- Homepage: https://github.com/otodockal/subjective#readme
- Issues: https://github.com/otodockal/subjective/issues
- npm.io page: https://npm.io/package/subjective

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 6.3.0 (latest) — 2019-04-30
- 6.2.0 — 2019-04-17
- 6.1.0 — 2019-04-10
- 6.0.0 — 2018-08-20
- 5.0.1 — 2018-07-04
- 5.0.0 — 2018-06-11
- 4.1.0 — 2018-03-29
- 4.0.2 — 2018-03-13
- 4.0.1 — 2018-03-13
- 4.0.0 — 2018-03-12
- 3.1.0 — 2018-03-11
- 3.0.1 — 2018-03-11
- 3.0.0 — 2018-03-10
- 2.2.0 — 2018-03-10
- 2.1.0 — 2018-01-18
- … 6 more at https://npm.io/package/subjective/versions

## README

# Subjective

*   Opinionated state management
*   Type safety by design. Type inference works for both Update and Selector functions.
*   Logging. Inspect where Update function was called.
*   Pause observable stream, if needed.
*   Always receive the whole state, if needed.

## Concepts

### State

```typescript
const state = new Subjective(
    productState,
    productStateFns,
);
```

##### Default Logger

```typescript
const state = new Subjective(
    productState,
    productStateFns,
    // use default Logger (dev only)
    true
);
```

##### Custom Logger

```typescript
const state = new Subjective(
    productState,
    productStateFns,
    // use custom Logger (dev only)
    (updateFnName: string, payload: any, updateFnRef: Function) => {
        // LOG
        const data = JSON.stringify(payload);
        const dataTrimmed = data.substring(0, 80);
        // logging to console
        console.groupCollapsed(
            `%c${fnName}: %c${dataTrimmed}${
                dataTrimmed.length < data.length ? '…' : ''
            }`,
            `color: green; font-weight: 300;`,
            `color: gray; font-weight: 100;`,
        );
        console.log(payload);
        console.log(updateFnRef);
        console.groupEnd();
    }
);
```

[EXAMPLE](https://stackblitz.com/edit/subjective?file=app%2Fcore%2Fstores%2Fproduct%2Fproduct.state.ts)

### Selector function

##### Subscribe to state.filter.type and notify with its value

```typescript
state.select(s => s.filter.type).subscribe();
```

##### Subscribe to state.filter.type and notify with a whole state
```typescript
state.select(s => s.filter.type, true).subscribe();
```

[EXAMPLE](https://stackblitz.com/edit/subjective?file=app%2Flist%2Flist.component.ts)

### Update function

##### Update value of state.filter.type

```typescript
state.update(f => f.updateFilterType, type);
```

##### Update value of state.filter.type and do not notify subscribers
```typescript
state.update(f => f.updateFilterType, type, false);
```

##### Update value of state.filter.type and return updated state
```typescript
const updatedState = state.update(f => f.updateFilterType, type);
```


[EXAMPLE](https://stackblitz.com/edit/subjective?file=app%2Flist%2Flist.component.ts)

## Examples

*   [Observable Service in Angular](examples/ANGULAR.md)
*   [Angular E-commerce App - StackBlitz](https://stackblitz.com/edit/subjective?file=app%2Fcore%2Fstores%2Fproduct%2Fproduct.state.ts)
*   [Testing](examples/TESTING.md)

## NOTES

### Immutable pattern

Always use [immutable pattern](https://glimmerjs.com/guides/tracked-properties) otherwise it will not work. We can't rely on mutations since object reference is always the same.

### Type-safety

Types are always inferred either from state class or payload parameter of the update function.

## FUTURE
I've been thinking of [this new syntax...check it out](EXP.md). 

## Credits

*   [RxJS](https://github.com/ReactiveX/rxjs)
*   [ngrx](https://github.com/ngrx/platform)
*   [Redux](https://github.com/reactjs/redux)

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