# statebox

> Orchestrate Node functions using Amazon States Language

Latest version **0.0.8** (published 2018-03-20) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2018-03-20 |
| First published | 2017-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 103.7 KB |
| Known vulnerabilities | 0 (+14 in 5 direct dependencies) |
| Install scripts | no |
| GitHub stars | 121 |
| Author | Tim Needham |
| Maintainers | timneedham |
| Keywords | fsm, finite, state, machine, aws |

## Links

- npm: https://www.npmjs.com/package/statebox
- Repository: https://github.com/wmfs/tymly
- Homepage: https://github.com/wmfs/tymly#readme
- Issues: https://github.com/wmfs/tymly/issues
- npm.io page: https://npm.io/package/statebox

## Dependencies (8)

- [boom](https://npm.io/package/boom.md) 7.2.0
- [uuid](https://npm.io/package/uuid.md) 3.2.1
- [async](https://npm.io/package/async.md) 2.6.0
- [debug](https://npm.io/package/debug.md) 3.1.0
- [dottie](https://npm.io/package/dottie.md) 2.0.0
- [lodash](https://npm.io/package/lodash.md) 4.17.5
- [jsonpath](https://npm.io/package/jsonpath.md) 1.0.0
- [asl-choice-processor](https://npm.io/package/asl-choice-processor.md) 0.0.5

## 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.0.8 (latest) — 2018-03-20
- 0.0.6 — 2017-10-06
- 0.0.4 — 2017-09-10
- 0.0.3 — 2017-09-10
- 0.0.2 — 2017-09-10

## README

# statebox
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/wmfs/tymly/blob/master/packages/statebox/LICENSE)

> Orchestrate Node functions using [Amazon States Language](https://states-language.net/spec.html)

## Useful links

* https://aws.amazon.com/step-functions/
* http://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html
* http://docs.aws.amazon.com/step-functions/latest/apireference/API_SendTaskSuccess.html


## <a name='install'></a>Install
```bash
$ npm install statebox --save
```

## <a name='usage'></a>Usage

```javascript
const Statebox = require('statebox')
const statebox = new Statebox()

// STEP 1:
// Create some 'module' resources (i.e. Javascript 
// classes with 'run' and optional 'init' methods) 
// that state machines can then refer to...
// -------------------------------------------------

statebox.createModuleResources(
  {
    // Simple module to add two numbers together
    add: class Add {
      run (event, context) {
        context.sendTaskSuccess(event.number1 + event.number2)
      }
    },
    // Simple module to subtract one number from another
    subtract: class Subtract {
      // Init methods are optional, but all allow  
      // resource-instances to be configured...
      init (resourceConfig, env, callback) {
          callback(null)
        }
      run (event, context) {
        context.sendTaskSuccess(event.number1 - event.number2)
      }      
    }
  }
)

// STEP 2:
// Next create a new 'calculator' state
// machine using Amazon States Language...
// ---------------------------------------

const info = statebox.createStateMachines(
  {
    'calculator': {
      Comment: 'A simple calculator',
      StartAt: 'OperatorChoice',
      States: {
        OperatorChoice: {
          Type: 'Choice',
          Choices: [
            {
              Variable: '$.operator',
              StringEquals: '+',
              Next: 'Add'
            },
            {
              Variable: '$.operator',
              StringEquals: '-',
              Next: 'Subtract'
            }
          ]
        },
        Add: {
          Type: 'Task',
          InputPath: '$.numbers',
          Resource: 'module:add', // See createModuleResources()
          ResultPath : '$.result',
          End: true
        },
        Subtract: {
          Type: 'Task',
          InputPath: '$.numbers',
          Resource: 'module:subtract',
          ResultPath : '$.result',
          End: true
        }
      }
    }  
  },
  {}, // 'env': An environment/context/sandbox
  function (err) {
    // All good-to-go!
  }    
)

// STEP 3:
// Start a new execution on a state machine
// ----------------------------------------

statebox.startExecution(
  {
    numbers: {
      number1: 3,
      number2: 2
    },
    operator: '-'
  },  // input
  'calculator', // state machine name
  {}, // options
  function (err, result) {
    // Result object
    // -------------
    //  {
    //    executionName: '01e1e288-9533-11e7-8fec-54d168e2e610',
    //    ctx: {
    //      numbers: {
    //        number1: 3,
    //        number2: 2
    //      },
    //      operator: '-'
    //    },
    //    currentStateName: 'OperatorChoice',
    //    stateMachineName: 'calculator',
    //    status: 'RUNNING',
    //    startDate: '2017-09-10T09:40:22.589Z'
    //  }
  }
)

// STEP 4:
// Look at the results...
// ----------------------
statebox.describeExecution(
  '01e1e288-9533-11e7-8fec-54d168e2e610',
  function (err, result) {
    //  Result object
    //  -------------
    // {
    //   executionName: '01e1e288-9533-11e7-8fec-54d168e2e610',
    //   ctx: {
    //     numbers': {
    //       number1: 3,
    //       number2: 2
    //     },
    //     operator: '-',
    //     result: 1 <--- The important bit :-)
    //   },
    //   currentStateName: 'Subtract',
    //   stateMachineName: 'calculator',
    //   status: 'SUCCEEDED',
    //   startDate: '2017-09-10T09:59:50.711Z'
    // }
  }
)  
```

## <a name='test'></a>Testing

```bash
$ npm test
```

## <a name='license'></a>License
[MIT](https://github.com/wmfs/tymly/packages/statebox/blob/master/LICENSE)

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