# stk

> state management toolkit

Latest version **0.2.10** (published 2017-06-06) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.10 |
| Published | 2017-06-06 |
| First published | 2016-09-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Nikita Dyumin |
| Maintainers | ndyumin |
| Keywords | predictable, functional, reactive, observable, frp, alternative, pure, state, state management, transactions, event sourcing, cqrs, store, model, flux, redux |

## Links

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

## Dependencies (1)

- [symbol-observable](https://npm.io/package/symbol-observable.md) ^1.0.2

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

- 0.2.10 (latest) — 2017-06-06
- 0.2.9 — 2017-06-05
- 0.2.8 — 2017-06-05
- 0.2.7 — 2017-05-31
- 0.2.6 — 2017-05-30
- 0.2.4 — 2017-02-17
- 0.2.3 — 2016-10-12
- 0.2.2 — 2016-10-05
- 0.2.1 — 2016-10-05
- 0.2.0 — 2016-10-04
- 0.1.2 — 2016-09-30
- 0.1.1 — 2016-09-29
- 0.1.0 — 2016-09-29

## README

# State management toolkit


## Basics
`.subscribe(Observer)`

`.subscribe(onNext, onError, onComplete)`
subscribe for state updates

example
```javascript
stk.store(10).subscribe(state => console.log(state));
stk.store(20).subscribe({
    next(state) {
        console.log(state);
    }
});
```

`.dispatch(Event)`
dispatches an event (produced with an eventCreator)
```javascript
const sum = (x, y) => x + y;
const store = stk.store(10);
store.dispatch({
    update: 20,
    reduce: sum
});
store.subscribe(state => console.log(state)); // 10, 30
```

`.createEvent(fnReducer)`
given a reducer returns an event creator that produces events (events can't have side effects)
```javascript
const sum = (x, y) => x + y;
const store = stk.store(10);
const numberEvent = store.createEvent(sum);
numberEvent(20)
store.subscribe(state => console.log(state)); // 10, 30
```

`.createCommand(fnExecutor)`
given an executor function returns a command creator that produces commands (that perform side effects)

`.plug(Observable, reducer)`
'plugs' an observable to the store, so that every value from the observable results into an event (with the given reducer)


## Advanced
`.view(project)`
given a project function (project(Events[], initialState)) produces a view that is a way to observe events flowing in the store

`._eventLog(Observer)`
subscribes an Observer to the raw event log flowing through the store

### Flush strategies:
to avoid array overflowing and control computational cost of project stores are created with `flush` strategies.
A flush strategy specifies a policy for transitioning form an array of events and an initial state to a new initial state (which includes some of the events) and a reduced events array.
`immediateFlushStrategy` flush every event (warning: transactions will not work)
`neverFlushStrategy` no flush at all (good for the cases with a low events number - e.g. less than 10 mln)
`count100kFlushStrategy` flush upon reaching 100 000 events (default for stk)

store(initialState[, flushStrategy]) // flush strategy is specified in a store constructor as an optional 2nd argument
built-in strategies can be found under stk.flushStrategies path.
It is possible to specify custom flush strategy under the following API:
```javascript
function customImmediateFlushStrategy(projectFunction){
    return function (eventsArray, initialState) {
        const newEventsArray = [];
        const newInitialState = projectFunction(eventsArray, initialState);
        return [newEventsArray, newInitialState];
    };
}
```

## Redux DevTools Extension
STK works with Redux Devtools Extension:
- Install the [tools](https://github.com/zalmoxisus/redux-devtools-extension)
- Connect one of the stores to the DevTools:
```javascript
const initial = 0;
const store = stk.store(initial);
stk.devtools.addStore(store, initial);
```

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